Click here to Skip to main content
15,897,704 members
Please Sign up or sign in to vote.
4.00/5 (4 votes)
See more:
I created a dictionary in winform like this:
C#
public Dictionary<string, int> dicMemory = new Dictionary<string, int>();

I want to create ValueChanged event when I update value in dicMemory to know which value changed,and I can catch event like this:
C#
dicMemory["Value_0"].ValueChanged += Value0_Changed

C#
dicMemory["Value_1"].ValueChanged += Value1_Changed

C#
...

What is the best way to do this. Thanks in advance.
Posted
Comments
Maciej Los 6-Aug-15 6:38am    
I'm afraid that Dictionary class does not provide functionality to add custom event while value is changed.

Write your own extended Dictionary class.
Inherit from Dictionary.
Shadow all the needed Methods (like .Item) and raise your event when a value gets changed.
 
Share this answer
 
You can create your own dictionary, that will inherit from Dictionary. There you can implement valueChanged event and others and then you would be able to do something like:
C#
CustomDictionary<int,> name = new CustomDictionary();
name.SomeEvent += someMethod;
 
Share this answer
 
Custom Dictionary Sample code :
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DictonaryValueChanged
{
    public class ExtendedDictonary<Tkey,Tvalue> : Dictionary<Tkey,Tvalue>
    {
        public Dictionary<Tkey,Tvalue> Items = new Dictionary<Tkey,Tvalue>();
        public ExtendedDictonary():base(){}
        ExtendedDictonary(int capacity) : base(capacity) { }
        //
         // Do all your implementations here...
        //

        public event EventHandler ValueChanged;

        public void OnValueChanged(Object sender,EventArgs e)
        {
            EventHandler handler = ValueChanged;
            if (null != handler) handler(this, EventArgs.Empty);
        }

        public void AddItem(TKey key, TValue value)
        {
            try
            {
                Items.Add(key, value);
                OnValueChanged(this, EventArgs.Empty);
            }
            catch (Exception ex)
            {
                
                throw ex;
            }
            
        }

        //
          // Similarly Do implementation for Update , Delete and Value Changed checks (Note: Value change can be monitored using a thread)
        //
    }
}


Using this Dictionary in your code :

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DictonaryValueChanged
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            ExtendedDictonary<string,int> dicMemory = new ExtendedDictonary<string,int>();
            dicMemory.ValueChanged += new EventHandler(DictonaryOnValuedChanged);

            dicMemory.AddItem("Hai", 1);
        }

        private void DictonaryOnValuedChanged(Object sender, EventArgs e)
        {
            string test = sender.ToString();
        }
    }
}
 
Share this answer
 
v5
Comments
Member 11735196 6-Aug-15 10:24am    
Thank you for your help, but I want to create the events for each TKey of Dictionary like this:

dicMemory["Key_0"].ValueChanged += new EventHandler(Key0_ValueChanged)
dicMemory["Key_1"].ValueChanged += new EventHandler(Key1_ValueChanged)
dicMemory["Key_2"].ValueChanged += new EventHandler(Key2_ValueChanged)

I want to raise event of Tkey which I needed when TValue gets changed, not all Tkey
Member 11735196 6-Aug-15 10:32am    
Do you understand me??
sreeyush sudhakaran 7-Aug-15 3:33am    
Then you can modify the eventArgs and do like this
if(e.KeyValue == Key0 || e.KeyValue == Key1 || e.KeyValue == Key2)
{
// DO some thing here
}

This will a meaningful I think so far
It is very easy by using below
dicMemory += new EventHandler(dicMemoryEvent);



private void dicMemoryEvent(Object sender, EventArgs e)
        {
            string test = sender.ToString();
        }
 
Share this answer
 
Comments
Member 11735196 6-Aug-15 10:29am    
Thank you for your help, but I want to create the events for each TKey of Dictionary like this:

dicMemory["Key_0"].ValueChanged += new EventHandler(Key0_ValueChanged)
dicMemory["Key_1"].ValueChanged += new EventHandler(Key1_ValueChanged)
dicMemory["Key_2"].ValueChanged += new EventHandler(Key2_ValueChanged)

I want to raise event of Tkey which I needed when TValue gets changed, not all Tkey. Do you understand me?
I want to create the events for each TKey of Dictionary like this:
C#
dicMemory["Key_0"].ValueChanged += new EventHandler(Key0_ValueChanged)
dicMemory["Key_1"].ValueChanged += new EventHandler(Key1_ValueChanged)
dicMemory["Key_2"].ValueChanged += new EventHandler(Key2_ValueChanged)

Private void Key0_ValueChanged(Object sender, EventArgs e)
    {
       // do something here
    }

Private void Key1_ValueChanged(Object sender, EventArgs e)
    {
       // do something here
    }

Private void Key2_ValueChanged(Object sender, EventArgs e)
    {
       // do something here
    }

when Value_0 changed, Key0_ValueChanged method implement
when Value_1 changed, Key1_ValueChanged method implement
when Value_2 changed, Key2_ValueChanged method implement
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900