这期内容当中小编将会给大家带来有关VB.NET中如何使用事件通道,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。C#代码示例如下:public delegate void sh
这期内容当中小编将会给大家带来有关VB.NET中如何使用事件通道,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
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<i;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
EventHandler
Public 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.Click
For 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.Load
End Sub
Private Sub Button3_Click()Sub
Button3_Click(ByVal sender As
System.Object, ByVal e As System.
EventArgs) Handles Button3.Click
For i = 0 To 3 Step 1
Dim myMsg As New msg
myMsg = myContain.Item(i)
myMsg.showMsg("good ")
Next i
End Sub
上述就是小编为大家分享的VB.NET中如何使用事件通道了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注编程网精选频道。
--结束END--
本文标题: VB.NET中如何使用事件通道
本文链接: https://lsjlt.com/news/291522.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0