我有一个菜鸟查询,有没有办法使用单个命令在不同的按钮点击事件上打开不同的表单。我有一个表格中的24个按钮,将使用这些按钮打开24种不同的表格。
所以不要像以下那样做24次:
Private Sub BtnCh1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCh1.Click
FormCh1.Show()
End Sub
Private Sub BtnCh2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCh2.Click
FormCh2.Show()
End Sub
Private Sub BtnCh3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCh3.Click
FormCh3.Show()
End Sub
Private Sub BtnCh4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCh4.Click
FormCh4.Show()
End Sub
可以用一个命令完成吗?
答案 0 :(得分:1)
在表单的加载事件中,在List(Of Form)
Private list As List(Of Form)
Private Sub Me_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.Load
list = New List(Of Form)
list.Add(New Form1())
'
'
'
list.Add(New Form24())
End Sub
使用表单的索引设置按钮的Tag
属性,并将它们全部设置为使用相同的单击事件:
Private Sub btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn.Click
list(CType(sender, Button).Tag).Show()
End Sub
答案 1 :(得分:-1)
将所有处理程序附加到您的方法,然后根据Select Case:
分支行为Private Sub Button_Click_Handler(sender As Object, e As EventArgs) Handles Button66.Click, Button67.Click, Button68.Click
Dim btn As Button = DirectCast(sender, Button)
Select Case btn.Name
Case Button66.Name
Dim f1 As New Form1
f1.Show()
Case Button67.Name
Dim f2 As New Form2
f2.Show()
Case Button68.Name
Dim f3 As New Form3
f3.Show()
End Select
End Sub