大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
这期内容当中小编将会给大家带来有关VB.NET中如何使用事件通道,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
濮阳网站建设公司成都创新互联,濮阳网站设计制作,有大型网站制作公司丰富经验。已为濮阳上1000+提供企业网站建设服务。企业网站搭建\成都外贸网站建设公司要多少钱,请找那个售后服务好的濮阳做网站的公司定做!
C#代码示例如下:
public delegate void showMsg
EventHandler(string msg);
class msg
{
public event showMsgEvent
Handler showMsgEvent;
public string execMsg(string msg){
if (showMsgEvent != null)
{
showMsgEvent(msg + "," +
DateTime.Now.ToString());}
return string.Empty;
}
}
===调用===
int i = 3;
msg[] myMsg;
private void button2_Click
(object sender, EventArgs e){
myMsg = new msg[i];
for(int j=0;j
{
myMsg[j]=new msg();
myMsg[j].showMsgEvent +=new
showMsgEventHandler(Form3_
showMsgEvent);}
}
void Form3_showMsgEvent (string msg)
{
MessageBox.Show(msg);
}
private void button3_Click
(object sender, EventArgs e){
for (int j = 0; j < i; j++)
{
myMsg[j].execMsg("good "
+ j.ToString());}
}
转换后的VB.NET事件通道代码:
Delegate Sub showMsgEventHandler
()Sub showMsgEventHandler
(ByVal msg As String)Class msgClass msg
Public name As String
Event msgEvent As showMsg
EventHandlerPublic Sub showMsg()Sub showMsg
(ByVal msg As String)MsgBox(msg + DateTime.Now.
ToString() + " ," + name)End Sub
End Class===调用===
Dim i As Integer = 0
Dim myContain As New List(Of msg)
Private Sub Button2_Click(
)Sub Button2_Click(ByVal sender
As System.Object, ByVal e As
System.EventArgs) Handles Button2.ClickFor i = 0 To 3 Step 1
Dim myMsg As New msg
myMsg.name = "name" & i.ToString()
myContain.Add(myMsg)
AddHandler myMsg.msgEvent,
New showMsgEventHandler(AddressOf
responseMsg)Next i
End Sub
Private Sub responseMsg()Sub
responseMsg(ByVal msg As String)MsgBox(msg)
End Sub
Private Sub Form1_Load()Sub Form1_Load
(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles MyBase.LoadEnd Sub
Private Sub Button3_Click()Sub
Button3_Click(ByVal sender As
System.Object, ByVal e As System.
EventArgs) Handles Button3.ClickFor i = 0 To 3 Step 1
Dim myMsg As New msg
myMsg = myContain.Item(i)
myMsg.showMsg("good ")
Next i
End Sub
上述就是小编为大家分享的VB.NET中如何使用事件通道了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注创新互联行业资讯频道。