MDI表单点击事件 [英] MDI form click event

查看:54
本文介绍了MDI表单点击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有包含TablayoutPannel,picturebox,子表单的mdi表单。

当我点击MDI picturebox时它会触发picturebox点击事件。它不会触发MDI表单点击事件。

当用户点击MDI图片框控件时如何触发MDI form_click事件?

I have mdi form containing TablayoutPannel,picturebox,child form.
When i clicks on MDI picturebox it fires picturebox click event.It will not fires MDI form click event.
how to fire MDI form_click event when user clicks on MDI picturebox control?

推荐答案

我很犹豫向你展示这个hack,因为我确信它不是一个好的依赖的东西,但是,既然你想要它...



...编辑...版本2:



这是获取MdiClient的更清洁方式:
I am hesitant to show you this hack, since I am convinced it's not a good thing to rely on, but, since you want it ...

... edit ... version 2:

Here's a "cleaner way" to get the MdiClient:
private MdiClient TheMdiClient;

 private void Form1_Load(object sender, EventArgs e)
 {
     foreach (Control theControl in this.Controls)
     {
         // cannot use the 'is operator here !
         if (theControl as MdiClient != null)
         {
             TheMdiClient = theControl as MdiClient;
             break;
         }
     }

     // error handling
     if (TheMdiClient == null)
     {
         throw new NullReferenceException("There is no MDIClient !");
     }
}

...结束编辑...版本2



版本1:

private void Form1_Load(object sender, EventArgs e)
{
    // whatever other things you do in the Load Event

    Control theMdiClient = this.Controls[this.Controls.Count - 1];

    theMdiClient.Click += theMdiClient_Click;
}

private void theMdiClient_Click(object sender, EventArgs e)
{
    // for testing only
    MessageBox.Show("clicked on the MDI Parent");

    // whatever you want to do on a click on the MdiParent Form
}

MdiClient是一个特殊类型的无名控件,由MDI Win Form App自动创建:它在设计时不可见。

The MdiClient is a nameless Control of a special Type which is created automatically by an MDI Win Form App: it is not "visible" at design-time.


你真的不想 - 或者至少你不应该这样。

将表单Click事件处理程序代码放到一个单独的方法上并从两个Click事件处理程序中调用它(如果您想要子表单中的表单单击处理程序)或创建一个新的事件(具有合适的名称)并从表单单击和PictureBox点击事件(如果您希望它在MDI父表单中处理)发出信号



如果你真的,真的必须,你可以在特定控件(并且表单是控件)上引发Click事件 - 但这不是一个好主意:

You don't really want to - or at least you shouldn't.
Either put the form Click event handler code onto a separate method and call it from both Click event handlers (if you want the Form Click handler within the child form) or create a new Event (with a suitable name) and signal it from both the Form Click and the PictureBox click events (if you want it handled in the MDI Parent form)

You can raise a Click event on a specific control (and a form is a control) if you really, really must - but it's not a good idea:
this.InvokeOnClick(this, new EventArgs());


我已经解决了这个问题如下:



AssignClickEvent(Control cParent )函数递归地将click事件分配给它的所有子控件。

以MDI格式调用此函数Load事件&在MdiChildActivate事件中。

MdiChildActivate事件在每个子负载上被调用。



I have solved this problem as follws:

AssignClickEvent(Control cParent) function assigns click event to all its child controls recursively.
Call this function in MDI form Load event & in MdiChildActivate event.
MdiChildActivate event gets called on each child load.

private void frmMDI_Load(object sender, EventArgs e) //Form Load event
       {
               //Call function to assign click event to all Child Controls ***
               AssignClickEvent(this);
               //***************************************
       }

 //(Recursive function) ***************
       public void AssignClickEvent(Control cParent)
       {
           foreach (Control c in cParent.Controls)
           {
               if (c.Controls.Count != 0)
                   AssignClickEvent(c);

               c.Click += new EventHandler(c_Click);
           }
       }

       void c_Click(object sender, EventArgs e)
       {
          MessageBox.Show("Mouse click event at MDI");
       }

      //Fires when MDI child is shown ******************
       private void frmMDI_MdiChildActivate(object sender, EventArgs e)//Registered event
       {
           //Call AssignClickEvent function to assign click event to all Child Controls ***************
           if (this.ActiveMdiChild != null)
               AssignClickEvent(this.ActiveMdiChild);
           //***************************************
       }


这篇关于MDI表单点击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆