简体   繁体   English

如何检测mdi孩子的控制事件?

[英]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. 我正在创建一个MDI应用程序,在子窗体上是一个DataGrid。 I need to figure out how to subscribe to the DoubleClick event of that DataGrid on the child form. 我需要弄清楚如何在子表单上订阅该DataGrid的DoubleClick事件。

So, let's say Form1 is parent. 所以,让我们说Form1是父母。 Form2 is child. Form2是孩子。 I have opened up Form2 as such: 我打开了Form2:

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. 当用户以该格式(f)双击数据网格时,我需要能够检测到它。 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 您可以使用System.Windows.Forms.Control类中的DataGrid类继承的myDataGrid.DoubleClick

    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). AppDeveloper的答案就是您所需要的,但我感觉您的问题的核心可能是Form2的DataGrid相对于父MDI容器(Form1)的默认可见性。 Form1 can subscribe to any event in Form2's DataGrid, but only if the DataGrid is marked as public (or internal ). Form1可以订阅Form2的DataGrid中的任何事件,但仅限于DataGrid被标记为公共 (或内部 )。 By default, that child control will be marked as private , and therefore only visible to Form2. 默认情况下,该子控件将标记为私有 ,因此仅对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: 因此,如果您只是将DataGrid的访问修饰符从私有更改为公共 ,您应该能够从Form1中侦听其事件,如下所示:

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. 如果你想避免组件之间的紧密耦合,有更好的方法来处理这个问题,但这是你的情况下从A到B的最短路径。

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

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