在c#中为字典创建值更改事件 [英] Create Value Changed event for dictionary in c#

查看:205
本文介绍了在c#中为字典创建值更改事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在winform中创建了一个字典,如下所示:

  public  Dictionary< string,int> dicMemory =  new 字典< string,int>(); 



我想在更新时创建ValueChanged事件dicMemory中的值知道哪个值发生了变化,我可以捕获这样的事件:

 dicMemory [  Value_0]。ValueChanged + = Value0_Changed 



 dicMemory [  Value_1]。ValueChanged + = Value1_Changed 



< pre lang =c#> ...



最好的方法是什么。在此先感谢。

解决方案

编写自己的扩展词典类。

继承自Dictionary。

隐藏所有需要的方法(如.Item)并在值发生变化时引发事件。


您可以创建自己的字典,它将继承自字典。在那里你可以实现valueChanged事件和其他事件,然后你就可以做类似的事情:

 CustomDictionary< int,> ;  name =  new  CustomDictionary(); 
name.SomeEvent + = someMethod;


自定义词典示例代码:

 使用系统; 
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Text;

命名空间 DictonaryValueChanged
{
public class ExtendedDictonary< Tkey,Tvalue> :字典< Tkey,Tvalue>
{
public Dictionary< Tkey,Tvalue> Items = new Dictionary< Tkey,Tvalue>();
public ExtendedDictonary(): base (){}
ExtendedDictonary( int capacity): base (capacity){}
//
// 在这里完成所有实施......
//

public event EventHandler ValueChanged;

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

public void AddItem(TKey key,TValue < span class =code-sdkkeyword> value )
{
try
{
Items。添加(键,);
OnValueChanged( this ,EventArgs.Empty);
}
catch (例外情况)
{

抛出 ex;
}

}

//
// 类似地执行更新,删除和值更改检查(注意:值更改可以是使用线程监控)
//
}
}





在代码中使用此词典:



< pre lang =c#> 使用系统;
使用 System.Collections.Generic;
使用 System.ComponentModel;
使用 System.Data;
使用 System.Drawing;
使用 System.Linq;
使用 System.Text;
使用 System.Windows.Forms;

命名空间 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();
}
}
}


I created a dictionary in winform like this:

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:

dicMemory["Value_0"].ValueChanged += Value0_Changed


dicMemory["Value_1"].ValueChanged += Value1_Changed


...


What is the best way to do this. Thanks in advance.

解决方案

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.


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:

CustomDictionary<int,> name = new CustomDictionary();
name.SomeEvent += someMethod;


Custom Dictionary Sample code :

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 :

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();
        }
    }
}


这篇关于在c#中为字典创建值更改事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆