简体   繁体   English

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

[英]How to access the event handler from the class to use in the Form

I have a class that inherit from DataGridView and I have a EventArgs same as code below: 我有一个继承自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");

        }

    }
}

Then I have a form and I want to use the Test1 in my form. 然后,我有一个表单,我想在表单中使用Test1。 So I drag Test1 from Toolbox to my form.What I want is when I run my form and click on Test1 it will show the message "CellMousClick" but when I run my form it doesn't show anything.Thanks 所以我将Test1从Toolbox拖到窗体上。我想要的是,当我运行窗体并单击Test1时,它将显示消息“ CellMousClick”,但是当我运行窗体时,它不显示任何内容。

Create a constructor in the Test1 class and attach an event handler, eg: 在Test1类中创建一个构造函数,并附加一个事件处理程序,例如:

public Test1()
{
  this.CellMouseClick += new DataGridViewCellMouseEventHandler(this.Test1_Click);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM