您好,登录后才能下订单哦!
这期内容当中小编将会给大家带来有关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
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中如何使用事件通道了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。