如何从类中访问要在Form中使用的事件处理程序

时间:2016-02-27 02:42:40

标签: c#

我有一个继承自DataGridView的类,我的EventArgs与下面的代码相同:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace MyTest
{
    class Test1:System.Windows.Forms.DataGridView
    {
        private void Test1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Click");

        }

    }
}

然后我有一个表单,我想在我的表单中使用Test1。所以我将Test1从Toolbox拖到我的表单。我想要的是当我运行我的表单并单击Test1时它会显示消息" CellMousClick"但是当我运行我的表格时,它并没有显示任何内容。谢谢

1 个答案:

答案 0 :(得分:1)

在Test1类中创建一个构造函数并附加一个事件处理程序,例如:

public Test1()
{
  this.CellMouseClick += new DataGridViewCellMouseEventHandler(this.Test1_Click);
}
相关问题