C#代码示例如下:
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
转换后的VB.NET代码:
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
附录:
Visual Basic 语言概念 |
如何:编写事件处理程序 |
构造事件处理程序的方法取决于它与事件的关联方式。创建事件处理程序的标准方式是将 Handles 关键字与 WithEvents 关键字一起使用。Visual Basic 提供了第二种方式来处理事件:即 AddHandler 语句。AddHandler 和 RemoveHandler 允许您动态地启动和停止特定事件的事件处理。两种途径都可以使用,但不应该在同一个事件中同时使用 WithEvents 和 AddHandler。
用 WithEvents 处理事件
WithEvents 关键字允许创建可在事件处理程序中与 Handles 子句一同使用的类级或模块级对象变量。
用 WithEvents 和 Handles 子句处理对象
-
创建包含事件的简单类。
Visual Basic 复制代码 Class Class1 Public Event AnEvent(ByVal EventNumber As Integer) End Class
-
在即将进行事件处理的类或模块中,用 WithEvents 关键字声明事件源的对象变量,如下面的示例所示:
Visual Basic 复制代码 Public WithEvents ClassInst As Class1
-
在代码编辑器中,从左侧的“类名”下拉列表中选择刚声明的 WithEvents 变量。
-
从右侧的“方法名称”下拉列表中选择要处理的事件。代码编辑器用一个 Handles 子句创建空事件处理程序过程。
注意 这一步是可选的。只要创建的过程是子例程,有与处理中的事件匹配的正确参数列表,有指定正在处理的事件的 Handles 子句,就可以手动创建事件处理程序过程。
-
使用提供的参数给事件处理程序过程添加事件处理代码。以下代码提供了一个示例:
Visual Basic 复制代码 Public Sub ClassInst_AnEvent(ByVal EventNumber As Integer) _ Handles ClassInst.AnEvent MsgBox("Received event number: " & CStr(EventNumber)) End Sub
用 AddHandler 处理事件
可以用 AddHandler 语句动态地将事件与事件处理程序过程连接。
用 AddHandler 处理事件
-
创建一个子例程以处理事件,如下面的示例所示:
Visual Basic 复制代码 Public Sub EHandler(ByVal EventNumber As Integer) MsgBox("Received event number " & CStr(EventNumber)) End Sub
-
声明类的对象变量,该变量是您要处理的事件源。不像 WithEvents 变量,此变量可以是过程中的本地变量。例如:
Visual Basic 复制代码 Public Sub TestAddHandler() Dim CI As New Class1 End Sub
-
用 AddHandler 语句指定事件发送器的名称,用 AddressOf 语句提供事件处理程序的名称。例如,将以下代码添加到
TestAddHandler
子例程的末尾:Visual Basic 复制代码 AddHandler CI.AnEvent, AddressOf EHandler
只要支持处理中的事件的正确参数,任何过程都能用作事件处理程序。
使用 RemoveHandler 停止处理事件
可以使用 RemoveHandler 语句动态地断开事件与事件处理程序过程之间的连接。
使用 RemoveHandler 停止处理事件
-
用 RemoveHandler 语句指定事件发送器的名称,用 AddressOf 语句提供事件处理程序的名称。RemoveHandler 语句的语法将会始终与用于启动事件处理的 AddHandler 语句紧密匹配。例如:
Visual Basic 复制代码 RemoveHandler CI.AnEvent, AddressOf EHandler
处理自基类继承的事件
派生类(继承某个基类特征的类)能用 HandlesMyBase 语句处理它们的基类所引发的事件。
处理来自基类的事件
-
通过给事件处理程序过程的声明行添加 Handles MyBase.eventname 语句来声明派生类中的事件处理程序,其中的 eventname 是基类中正在处理的事件的名称。例如:
Visual Basic 复制代码 Public Class BaseClass Public Event BaseEvent(ByVal i As Integer) ' Place methods and properties here. End Class Public Class DerivedClass Inherits BaseClass Sub EventHandler(ByVal x As Integer) Handles MyBase.BaseEvent ' Place code to handle events from BaseClass here. End Sub End Class