如何在 C# 中向 UserControl 添加事件? [英] How to add an event to a UserControl in C#?

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

问题描述

我有一个包含 3 个标签的 UserControl.我想为其添加一个事件,当其中一个标签的文本发生更改时会发生该事件.
我使用的是 Visual Studio 2010

I have a UserControl which contains 3 labels. I want to add 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 事件.

Then you need to create a method to handle the individual labels' TextChanged events.

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

某处,可能在您控件的构造函数中,您需要订阅标签的 TextChanged 事件.

Somewhere, probably in your control's constructor, you need to subscribe to the label's TextChanged events.

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,因为没有订阅者的事件是 null.如果我们试图引发一个空事件,我们会得到一个 NullReferenceException.请注意,在检查事件是否为空并引发事件之前,我们将事件的 EventHandler 复制到局部变量.如果我们改为这样做:

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);
}

我们将在无效检查和事件引发之间存在竞争条件.如果恰巧事件的订阅者在我们引发事件之前取消订阅,但在我们检查 null 之后,则会抛出异常.您通常不会遇到此问题,但最好养成以安全方式编写它的习惯.

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.

以下是public event EventHandler LabelsTextChanged;行的放置方式:

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# 中向 UserControl 添加事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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