java add event handler,如何使用addHandler操作,以提高活动

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.

解决方案

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#.

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

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

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值