如何使用addHandler操作,以提高活动 [英] How to raise event using addHandler

查看:833
本文介绍了如何使用addHandler操作,以提高活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我舒服Vb.Net事件和处理程序。
任何人可以帮助我如何在C#创建事件处理程序,提高事件。

I am comfortable with Vb.Net events and handlers. Can anybody will help me with how to create event handlers in c#, and raise events.

推荐答案

开发谁知道只有C#,或仅VB.Net,可能不知道,这是VB.NET和C#之间的较大差异之一。

Developers who know only C#, or only VB.Net, may not know that this is one of the larger differences between VB.NET and C#.

我会的 shamelesssly复制 VB事件的这个漂亮的解释:VB用来连接事件声明语法。 href=\"http://msdn.microsoft.com/en-us/library/6k46st1y.aspx\">把手子句 WithEvents就修改出现在标的变量的声明,如一个按钮。您还可以附加使用的AddHandler 关键字属性的处理程序,并与RemoveHandler删除它们。例如:

I will shamelesssly copy this nice explanation of VB events: VB uses a declarative syntax for attaching events. The Handles clause appears on the code that will handle the event. When appropriate, multiple methods can handle the same event, and multiple events can be handled by the same method. Use of the Handles clause relies on the WithEvents modifier appearing on the declaration of the underlying variable such as a button. You can also attach property handlers using the AddHandler keyword, and remove them with RemoveHandler. For example

Friend WithEvents TextBox1 As System.Windows.Forms.TextBox   

Private Sub TextBox1_Leave(ByVal sender As System.Object, _
  ByVal e As System.EventArgs) Handles TextBox1.Leave
  'Do Stuff '
End Sub

在C#中不能使用声明的语法。您可以使用+ =这是超载行动像VB.Net的AddHandler。下面是从 tster的回答无耻地窃取一个例子:

In C# you can't use the declarative syntax. You use += which is overloaded to act like the VB.Net AddHandler. Here's an example shamelessly stolen from tster's answer:

public MyClass()
{
    InitializeComponent();
    textBox1.Leave += new EventHandler(testBox1_Leave);
}

void testBox1_Leave(object sender, EventArgs e)
{
  //Do Stuff
}

这篇关于如何使用addHandler操作,以提高活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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