Click事件对于.NET(Windows窗体)用户控制 [英] Click event for .Net (Windows Forms) user control

查看:132
本文介绍了Click事件对于.NET(Windows窗体)用户控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个很简单的问题,但由于某些原因,即使是正确的方式在网上搜索答案躲开我......

This is probably a very simple question, but for some reason, even the right way to web search for the answer eludes me...

我想创建一个用户控件,它由几个标签和进度条。不过,我想整个控制有被提出,无论是点击什么项目控制在一个点击事件。我创建了分配给每个控制HandleClick的过程:

I'm trying to create a user control that consists of a few labels and progress bars. However, I want the entire control to have a "Click" event that is raised no matter what item inside the control is clicked on. I've created a "HandleClick" procedure that is assigned to each control:

    private void HandleClick(object sender, EventArgs e)
    {
        // Call the callback function, if we were asked to
        if (OnClick != null)
        {
            EventArgs ee = new EventArgs();
            OnClick(this, ee);
        }
        else
        {
            MessageBox.Show("OnClick was null!");
        }
    }

的OnClick在这种情况下是一个变量,在控制层定义的:

OnClick in this instance is a variable defined at control level:

    public new event EventHandler OnClick;

现在,这只是正常工作的形式。在一个标签,它显示在MessageBox,然后调用封装形式的事件。其余所有显示的消息框。

Now, this only works properly on the form. On one label it shows the MessageBox, and then calls the event on the enclosing form. All the rest show the message box.

我得到的感觉,这应该是显而易见的,但无奈一下午给我留下的感觉,我失去了一些东西,应该是不言而喻的,但是当我看到它,我会觉得自己像一个完整的小丑.. 。任何人都可以停止咯咯地笑我daftness足够长的时间来开导我在哪里,我已经错了?

I get the feeling that this should be obvious, but an afternoon of frustration has left me feeling I'm missing something that should be self-evident, but when I see it I am going to feel like a complete buffoon... can anyone stop giggling at my daftness long enough to enlighten me where I've gone wrong?

推荐答案

很抱歉这 - 只是把一个答案,以防有人谷歌它...

Sorry about this - just putting an answer on in case someone googles it...

如果你有兴趣,这个帖子帮助解决这个问题:用户控制点击 - Windows窗体 ......基本上,删除HandleClick ,和物业,并以这个来代替:

In case you're interested, this post helped solve it: User Control Click - Windows Forms… Basically, remove HandleClick, and the property and substitute this one instead:

public new event EventHandler Click
{
    add
    {
        base.Click += value;
        foreach (Control control in Controls)
        {
            control.Click += value;
        }
    }
    remove
    {
        base.Click -= value;
        foreach (Control control in Controls)
        { 
            control.Click -= value;
        }
    }
}

这篇关于Click事件对于.NET(Windows窗体)用户控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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