将不同类中的列表添加到C#时触发事件

时间:2016-06-22 17:40:20

标签: c# events

我在一个处理数据库函数的类中有一个静态列表。如果任何方法抛出异常,我将异常详细信息添加到列表中。我想在项目添加到列表时随时触发事件,并在另一个类的MessageBox中显示消息,在本例中为Windows Forms类。如果将数据库类中的静态列表添加到?

,那么在Forms类中处理事件的最佳方法是什么
class DatabaseClass
{
    // Static list for messages.
    public static List<string> messages = new List<string>();

    public static DataTable GetOrders()
    {
        var dt = new DataTable();
        var sql = "select * from orders";

        using (SqlConnection conn = new SqlConnection(ConString.Orders()))
        using (SqlCommand cmd = conn.CreateCommand())
        {
            try
            {
                cmd.CommandText = sql;
                conn.Open();
                dt.Load(cmd.ExecuteReader());
            }
            catch (Exception ex)
            {
                // Want to trigger an event when the list is added to.
                messages.Add("Problem getting orders. " +
                    ex.ToString());
            }
        }
        return dt;
}

public partial class Form1 : Form
{  
    // When the event in the DatabaseClass is triggered, 
    // display a message box with the message from the list. 
}

2 个答案:

答案 0 :(得分:2)

您可以使用System.ComponentModel命名空间中的BindingList<T>类代替List<T>类,因为它包含您要查找的事件:

class DatabaseClass
{
  // Static list for messages.
  public static BindingList<string> Messages = new BindingList<string>();

然后在您的表单中,您可以简单地听取任何不断变化的事件:

public partial class Form1 : Form
{
  public Form1() {
    InitializeComponent();
    DatabaseClass.Messages.ListChanged += Messages_ListChanged;
  }
}

void Messages_ListChanged(object sender, ListChangedEventArgs e) {
  if (e.ListChangedType == ListChangedType.ItemAdded) {
    MessageBox.Show(DatabaseClass.Messages[e.NewIndex].ToString());
  }
}

答案 1 :(得分:1)

您只需在EventHandler内定义DatabaseClass,并在设置列表时调用它。然后连接到事件:

class DatabaseClass
{
    public static event EventHandler ErrorsChanged;

    private static List<string> messages = new List<string>();
    public static List<string> Messages
    {
        get
        {
            return messages;
        }
        set
        {
            messages = value;
            ErrorsChanged(messages, EventArgs.Empty);
        }
    }
}

public partial class Form1 : Form
{
    public Form1()
    {
        DatabaseClass.ErrorsChanged += new EventHandler(ErrorItemsChanged);
    }

    private void ErrorItemsChanged(Object sender, EventArgs e)
    {
        // This line will be entered if the list has changed.
    }
}

如果您想变得很酷,可以通过该事件延长List<T>

public class ListEx<T> : List<T>
{
    public event EventHandler OnItemChange;

    public new void Add(T item)
    {
        base.Add(item);
        OnItemChange(this, EventArgs.Empty);
    }
}

然后以这种方式使用它:

public class DatabaseClass
{
    // Use extended List here.
    public static ListEx<string> messages = new ListEx<string>();
}

public partial class Form1 : Form
{
    public Form1()
    {
        DatabaseClass.messages.OnItemChange += new EventHandler(ItemChanged);
    }

    public void ItemChanged(Object sender, EventArgs e)
    {
        // Will be raised.
    }
}