在用户控制中解释自定义事件的代码 [英] Explain code for custom event in user control

查看:162
本文介绍了在用户控制中解释自定义事件的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人给了我这个代码非常好。但我真的很想了解它内部发生了什么。有人可以解释吗代码的每个部分的含义是什么?该代码位于面板内有两个标签的自定义控件中。



另外我看到一些使用add / remove语法的自定义控件事件,这是什么?这里有什么区别?

  public partial class UserControl1:UserControl 
{
public UserControl1()
{
InitializeComponent();
}

public event EventHandler MyCustomClickEvent;

protected virtual void OnMyCustomClickEvent(EventArgs e)
{
//这里,你使用this,所以这是你自己的控件。你也可以
//自定义EventArgs来传递你想要的东西。

if(MyCustomClickEvent!= null)
MyCustomClickEvent(this,e);
}

private void label1_Click(object sender,EventArgs e)
{
OnMyCustomClickEvent(EventArgs.Empty);
}
}


解决方案

我的意见如下。另外,对于更详细的活动,我博客这个概念一会儿回到了整个过程的更多细节。

  public partial class UserControl1:UserControl 
{
//这是用户控件的标准构造函数
public UserControl1()
{
InitializeComponent();
}

//这定义了一个名为MyCustomClickEvent的事件,它是一个通用的
//事件处理程序。 (EventHander是一个委托定义,定义了事件将共享哪些信息的契约
//在这种情况下,EventArgs对象的单个参数
//
public event EventHandler MyCustomClickEvent;


//此方法用于提升事件,当事件应该被引发时,
//此方法将检查是否有任何订阅者,如果有,
//它引发事件
protected virtual void OnMyCustomClickEvent(EventArgs e)
{
//这里,你使用this,所以这是你自己的控件

如果(MyCustomClickEvent!= null)
MyCustomClickEvent(this,e);
}

private void label1_Click(object sender,EventArgs e)
{
OnMyCustomClickEvent(EventArgs.Empty);
}
}


Someone gave me this code that works great. But I would really like to understand what is happening inside it. Could somebody explain please? What is the meaning of each part of the code? The code is inside a custom control which has two labels inside a panel.

Also I've seen some custom control events that use add/remove syntax, what is that for? What is the difference with what is happening here?

public partial class UserControl1 : UserControl
{
    public UserControl1()
    {
        InitializeComponent();
    }

    public event EventHandler MyCustomClickEvent;

    protected virtual void OnMyCustomClickEvent(EventArgs e)
    {
        // Here, you use the "this" so it's your own control. You can also
        // customize the EventArgs to pass something you'd like.

        if (MyCustomClickEvent != null)
            MyCustomClickEvent(this, e);
    }

    private void label1_Click(object sender, EventArgs e)
    {
        OnMyCustomClickEvent(EventArgs.Empty);
    }
}

解决方案

See my comments below. Also for a more detailed event I blogged on this concept a while back where I go into more detail on the entire process.

public partial class UserControl1 : UserControl
{
    //This is the standard constructor of a user control
    public UserControl1()
    {
        InitializeComponent();
    }

    //This defines an event called "MyCustomClickEvent", which is a generic
    //event handler.  (EventHander is a delegate definition that defines the contract
    //of what information will be shared by the event.  In this case a single parameter
    //of an EventArgs object.
    public event EventHandler MyCustomClickEvent;


    //This method is used to raise the event, when the event should be raised, 
    //this method will check to see if there are any subscribers, if there are, 
    //it raises the event
    protected virtual void OnMyCustomClickEvent(EventArgs e)
    {
        // Here, you use the "this" so it's your own control. You can also
        // customize the EventArgs to pass something you'd like.

        if (MyCustomClickEvent != null)
            MyCustomClickEvent(this, e);
    }

    private void label1_Click(object sender, EventArgs e)
    {
        OnMyCustomClickEvent(EventArgs.Empty);
    }
}

这篇关于在用户控制中解释自定义事件的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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