Private Sub Form_Shown(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Shown
Dim myTextBox As TextBox
' const declare
Const x0 As Int32 = 20
Const y0 As Int32 = 20
Const w As Int32 = 100
Const h As Int32 = 30
Const hd As Int32 = 10
Const cnt As Int32 = 4
Dim i As Int32 = 0
' 動態生成 TextBox
For i = 0 To cnt - 1
myTextBox = New TextBox()
myTextBox.Text = "button" & i
myTextBox.Left = x0
myTextBox.Top = y0 + i * (h + hd)
myTextBox.Width = w
myTextBox.Height = h
Me.Controls.Add(myTextBox)
AddHandler myTextBox.Click, AddressOf myTextBoxClick ' 交附給函式
Next
End Sub
Private Sub myTextBoxClick(ByVal sender As Object, ByVal e As System.EventArgs)
System.Windows.Forms.MessageBox.Show(CType(sender, TextBox).Text)
End Sub
End Class
Public Class Form
' 動態生成按扭
Private myButton() As Button
Private Sub Form_Shown(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Shown
' const declare
Const x0 As Int32 = 20
Const y0 As Int32 = 20
Const w As Int32 = 100
Const h As Int32 = 30
Const hd As Int32 = 10
Const cnt As Int32 = 4
Dim i As Int32 = 0
ReDim myButton(cnt)
' 動態生成 button
For i = 0 To cnt - 1
myButton(i) = New Button()
myButton(i).Text = "button" & i
myButton(i).Left = x0
myButton(i).Top = y0 + i * (h + hd)
myButton(i).Width = w
myButton(i).Height = h
Me.Controls.Add(myButton(i))
Next
' 進行函式對應
AddHandler myButton(0).Click, AddressOf Func0
AddHandler myButton(1).Click, AddressOf Func1
AddHandler myButton(2).Click, AddressOf Func2
AddHandler myButton(3).Click, AddressOf Func3
End Sub
Public Class Form
' 動態生成按扭
Private myButton() As Button
Private Sub Form_Shown(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Shown
' const declare
Const x0 As Int32 = 20
Const y0 As Int32 = 20
Const w As Int32 = 100
Const h As Int32 = 30
Const hd As Int32 = 10
Const cnt As Int32 = 4
Dim i As Int32 = 0
ReDim myButton(cnt)
' 動態生成 button
For i = 0 To cnt - 1
myButton(i) = New Button()
myButton(i).Text = "button" & i
myButton(i).Left = x0
myButton(i).Top = y0 + i * (h + hd)
myButton(i).Width = w
myButton(i).Height = h
Me.Controls.Add(myButton(i))
AddHandler myButton(i).Click, AddressOf myButtonClick
Next
End Sub
' 委派函式
Private Sub myButtonClick(ByVal sender As Object, ByVal e As System.EventArgs)
System.Windows.Forms.MessageBox.Show(CType(sender, Button).Text)
End Sub
End Class
範例四 - 動態生成 Button (3)
第三版動態生成按鈕改善了二個部份,我們可以借由自己寫的函式,把 Button Name 一次全換掉;除此之外,事實上 myButton 可以不用 array 方式建立,同時也可以不用宣告到全域。
' 設定按鈕名稱
Private Sub SetButtonName(ByRef btnName() as String, ByVal ParamArray Name() As String)
For i As Int32 = 0 To UBound(btnName) - 1
btnName(i) = Name(i)
Next i
End Sub
Private Sub Form_Shown(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Shown
Dim btnName() As String
' const declare
Const x0 As Int32 = 20
Const y0 As Int32 = 20
Const w As Int32 = 100
Const h As Int32 = 30
Const hd As Int32 = 10
Const cnt As Int32 = 4
' 動態生成 button
For i = 0 To cnt - 1
myButton = New Button()
myButton.Text = btnName(i)
myButton.Left = x0
myButton.Top = y0 + i * (h + hd)
myButton.Width = w
myButton.Height = h
Me.Controls.Add(myButton)
AddHandler myButton.click, AddressOf myButtonClick
Next
End Sub
Private Sub myButtonClick(ByVal sender As Object, ByVal e As System.EventArgs)
System.Windows.Forms.MessageBox.Show(CType(sender, Button).Text)
End Sub
End Class
Private Sub SetButtonName(ByRef btnName() As String, ByVal ParamArray Name() As String)
For i As Int32 = 0 To UBound(btnName) - 1
btnName(i) = Name(i)
Next i
End Sub
Private Sub Form_Shown(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Shown
Dim BtnName() As String
' const declare
Const x0 As Int32 = 20
Const y0 As Int32 = 20
Const w As Int32 = 100
Const h As Int32 = 30
Const hd As Int32 = 10
Const cnt As Int32 = 4
' 動態生成 button
For i = 0 To cnt - 1
myButton = New Button()
myButton.Text = BtnName(i)
myButton.Name = "btn_" & i ' 注意,這裡視為是 button ID, 不是 button text
myButton.Left = x0
myButton.Top = y0 + i * (h + hd)
myButton.Width = w
myButton.Height = h
Me.Controls.Add(myButton)
AddHandler myButton.Click, AddressOf myButtonClick
Next
End Sub
Dim btnIndex As Int32
Private Sub myButtonClick(ByVal sender As Object, ByVal e As System.EventArgs)
' 取得是由哪個 button ID 發出來的訊息
btnIndex = Val(CType(sender, Button).Name.Split("_")(1))
' 根據發出來的 button ID 做相對應的 function map,這部份比較麻煩
Select Case btnIndex
Case 0
Func0()
Case 1
Func1()
Case 2
Func2()
Case 3
Func3()
End Select
End Sub
Public Sub Func0()
MsgBox("func0")
End Sub
Public Sub Func1()
MsgBox("func1")
End Sub
Public Sub Func2()
MsgBox("func2")
End Sub
Public Sub Func3()
MsgBox("func3")
End Sub
Public Sub Func4()
MsgBox("func4")
End Sub
End Class