简体   繁体   中英

How to detect a mdi child's control event?

I have been looking online, but cannot find an answer to what I'm trying to do. I am creating a MDI application, and on a child form is a DataGrid. I need to figure out how to subscribe to the DoubleClick event of that DataGrid on the child form.

So, let's say Form1 is parent. Form2 is child. I have opened up Form2 as such:

Form2 f = new Form2 { Text = "Child", MdiParent = this };
f.Show();

When the user doubleclicks on the datagrid in that form (f), I need to be able to detect it. Can anyone point me in the right direction?

(I hope this was explained clearly)

You can use the myDataGrid.DoubleClick inherited by the DataGrid Class from System.Windows.Forms.Control Class

    private void Form_Load()
    {
        myDataGrid.DoubleClick += MyDataGridOnDoubleClick;
    }

    private void MyDataGridOnDoubleClick(object sender, EventArgs eventArgs)
    {
        //throw new NotImplementedException();
    }

AppDeveloper's answer is what you need, but I have a feeling that the heart of your issue might be the default visibility of Form2's DataGrid with respect to the parent MDI container (Form1). Form1 can subscribe to any event in Form2's DataGrid, but only if the DataGrid is marked as public (or internal ). By default, that child control will be marked as private , and therefore only visible to Form2.

So if you just change the access modifier for your DataGrid from private to public , you should be able to listen to its events from Form1 like so:

Form2 childForm = new Form2();
childForm.MdiParent = this;
// Form2.myDataGrid must be public/internal
childForm.myDataGrid.DoubleClick += new EventHandler(MyDataGridOnDoubleClick);
childForm.Show();

There are better ways to handle this if you want to avoid tight coupling between your components, but this is the shortest route from A to B in your case.

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