您好,登錄后才能下訂單哦!
這期內容當中小編將會給大家帶來有關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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。