如何在C#中向UserControl添加事件

如何在C#中向UserControl添加事件

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

问题描述

限时删除!!

我有一个UserControl包含3个标签。我想为它做一个事件,当其中一个标签的文本改变时发生。
我正在使用Visual Studio 2010

解决方案

首先,您需要在课堂内声明事件(与您的方法一起)和构造函数):

  public event EventHandler LabelsTextChanged; 

然后,您需要创建一个方法来处理单个标签的 TextChanged 事件

  private void HandleLabelTextChanged(object sender,EventArgs e)
{
//我们会在一分钟内解释一下这个
this.OnLabelsTextChanged(EventArgs.Empty);
}

在某些地方,可能在您的控件的构造函数中,您需要订阅标签的 TextChanged 事件。

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

现在为$ code> HandleLabelsTextChanged 方法。我们可以直接提高 LabelsTextChanged 然而,.NET框架设计指南说,创建一个 OnEventName 受保护的虚拟方法来为我们提出事件是最佳做法。这样,继承类可以通过覆盖 OnEventName 方法来处理事件,结果是比订阅事件有一个更好的性能。即使您认为您将永远不会覆盖 OnEventName 方法,因此简化了事件提升过程,这是一个很好的习惯。 p>

这是我们的 OnLabelsTextChanged

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

我们必须检查null,因为没有订阅者的事件为空。如果我们尝试提出一个null事件,我们将得到一个 NullReferenceException 。请注意,我们将事件的 EventHandler 复制到本地变量中,然后再检查它为null并提升事件。如果我们这样做:

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

我们将在无效检查和事件提升之间有竞争条件。如果发生事件,事件的订阅者在我们提出事件之前取消订阅,但在我们检查了null之后,将抛出异常。你通常不会遇到这个问题,但最好是习惯写安全的方式。



编辑:这里是否应该放置 public event EventHandler LabelsTextChanged; 行:

 命名空间YourNamespace 
{
class MyUserControl:UserControl
{
//它需要在这里:
public event EventHandler LabelsTextChanged;

...
}
}

是进一步阅读的事件设计框架设计指南。 p>

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;

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

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;

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.

Here's our OnLabelsTextChanged:

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

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.

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

1403页,肝出来的..

09-07 02:25