如何让内部控件不覆盖主控件的事件? [英] How can I make the inner controls don't cover the main control's events?

查看:22
本文介绍了如何让内部控件不覆盖主控件的事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UserControl,其中包含一些内部控件.像这样:

I have a UserControlwhich contains some inner controls inside it. something like this:

现在当我在我的项目中使用它时,我希望我的控件的每个点在被点击时引发相同的点击事件,就像其他控件一样.

Now when I use it in my project, I want every point of my control raise the same click event if clicked, just like other controls.

但问题是:我的处理程序在另一个项目中,仅在我单击背景上的某处而不是按钮 1 或标签 1 时处理单击事件.

but the problem is: my handler in another project, handles click events just when I click somewhere on the background, not on the button1 or label1.

我可以通过设置内部控件中的每个事件为主控件引发另一个事件来解决它,但这有点奇怪.

I can solve it by setting every event in inner controls raise another event for the main control, but it is a bit odd.

如何让内部控件不覆盖主控件的事件?

推荐答案

我建议利用 UserControl 的 Load 事件来迭代项目中的所有子控件,并以编程方式为每个子控件添加一个处理程序鼠标移动事件.这足以处理添加的任何新子控件.例如;

I'd suggest leveraging the UserControls Load event to iterate over all sub controls in your project and add a handler programmatically for each MouseMoved event. This would be robust enough to handle any new sub controls added. E.g;

    private void UserControl1_Load(object sender, EventArgs e)
    {
        foreach (Control c in Controls)
        {
            c.MouseMove += Control_Move;
        }
    }

    protected void Control_Move(object sender, MouseEventArgs e)
    {
        // do stuff here
    }

并且不要忘记将 UserControl 的 MouseMove 事件挂钩到相同的 Control_Move 方法

And don't forget to hook your UserControl's MouseMove event to the same Control_Move method

这篇关于如何让内部控件不覆盖主控件的事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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