各位好:
我在視窗上拉了50個button,
但是vb.net無法像vb,可以命名成button(0)~button(49)
而是會產生成button1、button2....button50
但是這樣要如何控制呢?
每個button的功能都是 msgbox (index)
假設我按了button5, 則會輸出 5
假設我按了button7, 則會輸出 7 以此類推,
如果用vb的話,
可以直接
Private Sub button_Click(Index As Integer)
msgbox(Index)
End Sub
以上,這樣就完成了!!
但是vb.net似乎無法以這樣的方式,
必須要指定每個button click的動作,
如
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
msgbox 1
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
msgbox 2
End Sub
....還有40多個
Private Sub Button49_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button49.Click
msgbox 49
End Sub
Private Sub Button50_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button50.Click
msgbox 50
End Sub
這樣有1000個button的話,
不就很麻煩嗎?
是否可以用迴圈控制?