简体   繁体   中英

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:

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. 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

Create a constructor in the Test1 class and attach an event handler, eg:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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