如何将事件添加到在C#用户控件? [英] how to add an event to a UserControl in C#?

查看:153
本文介绍了如何将事件添加到在C#用户控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含3个标签,一个用户控件。我想为它的一个事件,当一个标签的文本改变发生。
我使用Visual Studio 2010

I have a UserControl which contains 3 labels. I want to make an event for it, which occurs when the text of one of the labels changed. I am using Visual Studio 2010

推荐答案

首先,你需要在你的类中声明的事件(你旁边方法和构造函数):

First, you need to declare the event within your class (alongside your methods and constructors):

public event EventHandler LabelsTextChanged;

然后你需要创建来处理各个标签框TextChanged 事件的方法。

private void HandleLabelTextChanged(object sender, EventArgs e)
{
    // we'll explain this in a minute
    this.OnLabelsTextChanged(EventArgs.Empty);
}

某处,也许在你的控件的构造函数,你需要订阅标签的框TextChanged 事件。

myLabel1.TextChanged += this.HandleLabelTextChanged;
myLabel2.TextChanged += this.HandleLabelTextChanged;
myLabel3.TextChanged += this.HandleLabelTextChanged;

现在的 HandleLabelsTextChanged 方法。我们可以募集 LabelsTextChanged 直接;但是,.NET框架设计指南说,它是创建一个的OnEventName 受保护的虚拟方法,以提高该事件对我们来说是最好的做法。这样,继承类可以通过重写的OnEventName 法,果然比订阅事件一点更好的表现处理的事件。即使你认为你将永远不会覆盖的OnEventName 的方法,这是一个好主意,反正做的习惯得到的,因为它简化了事件的提高过程。

Now for the HandleLabelsTextChanged method. We could raise LabelsTextChanged directly; however, the .NET framework design guidelines say that is it a best practice to create an OnEventName protected virtual method to raise the event for us. That way, inheriting classes can "handle" the event by overriding the OnEventName method, which turns out to have a little better performance than subscribing to the event. Even if you think you will never override the OnEventName method, it is a good idea to get in the habit of doing it anyway, as it simplifies the event raising process.

下面是我们的 OnLabelsTextChanged

protected virtual void OnLabelsTextChanged(EventArgs e)
{
    EventHandler handler = this.LabelsTextChanged;
    if (handler != null)
    {
        handler(this, e);
    }
}

我们必须检查空,因为没有用户的事件为null。如果我们试图提出一个空事件,我们会得到一个的NullReferenceException 。请注意,我们检查它空和引发事件之前复制事件的事件处理程序来一个局部变量。如果我们做了,而不是像这样:

We have to check for null because an event without subscribers is null. If we attempted to raise a null event, we would get a NullReferenceException. Note that we copy the event's EventHandler to a local variable before checking it for null and raising the event. If we had instead done it like this:

if (this.LabelsTextChanged != null)
{
    this.LabelsTextChanged(this, e);
}

我们将有无效检查和事件提高之间的竞争条件。如果正巧,订户到事件本身退订之前我们提出的事件,但我们检查后空,一个异常将被抛出。你通常会遇到这样的问题,但最好是在写它的安全方法的习惯得到的。

We would have a race condition between the nullity check and the event raising. If it just so happened that the subscribers to the event unsubscribed themselves just before we raised the event but after we checked for null, an exception would be thrown. You won't normally encounter this issue, but it is best to get in the habit of writing it the safe way.

编辑:这里是如何的公共事件的EventHandler LabelsTextChanged; 行应放在:

Here is how the public event EventHandler LabelsTextChanged; line should be placed:

namespace YourNamespace
{
    class MyUserControl : UserControl
    {
        // it needs to be here:
        public event EventHandler LabelsTextChanged;

        ...
    }
}

这里都在活动设计的进一步阅读的框架设计指南。

Here are the framework design guidelines on event design for further reading.

这篇关于如何将事件添加到在C#用户控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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