Vb.net动态控制数组的"Sub of button_Click"程序如何编写?
How to code the "Sub of button_Click" program of dynamic control array in Vb.net?
我已经使用 VB6 很多年了。最近,我正在尝试使用 vb.net.
下面是VB6中的按钮数组和代码,效果很好。
Private Sub CommandColor_Click(Index As Integer)
If Index = 0 Then CommandColor(Index).BackColor = vbRed
If Index = 1 Then CommandColor(Index).BackColor = vbGreen
If Index = 2 Then CommandColor(Index).BackColor = vbYellow
End Sub
- 当我点击
CommandColor(0)
时,它的颜色变为红色。
- 当我点击
CommandColor(1)
时,它的颜色变为绿色。
- 当我点击
CommandColor(2)
时,它的颜色变为黄色。
现在,我正尝试在 VB2010 中做同样的事情。下面的代码是我在建立动态控制数组时完成的。但是,我不知道如何在动态按钮数组中编写“button_Click 的 Sub”代码。任何人都可以帮助我吗?提前致谢。
Public Class Form1
Dim CommandColor(2) As Button
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim i As Integer
For i = 0 To 2
CommandColor(i) = New Button
CommandColor(i).Text = "CommandColor" + Trim(Str(i))
CommandColor(i).ForeColor = Color.Black
CommandColor(i).Left = 50
CommandColor(i).Width = 250
CommandColor(i).Height = 50
CommandColor(i).Top = 50 + 66 * i
CommandColor(i).TextAlign = System.Drawing.ContentAlignment.MiddleCenter
CommandColor(i).Font = New Font(FontFamily.GenericSansSerif, 19, FontStyle.Regular)
CommandColor(i).BackColor = Color.Silver
Me.Controls.Add(CommandColor(i))
Next
End Sub
End Class
很简单,下面会有所帮助,
AddHandler CommandColor(i).Click, AddressOf SubName
以下是您的代码所需的修改,
Public Class Form1
Dim CommandColor(2) As Button
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim i As Integer
For i = 0 To 2
CommandColor(i) = New Button
CommandColor(i).Name = "btn" & (i+1)
CommandColor(i).Text = "CommandColor" + Trim(Str(i))
CommandColor(i).ForeColor = Color.Black
CommandColor(i).Left = 50
CommandColor(i).Width = 250
CommandColor(i).Height = 50
CommandColor(i).Top = 50 + 66 * i
CommandColor(i).TextAlign = System.Drawing.ContentAlignment.MiddleCenter
CommandColor(i).Font = New Font(FontFamily.GenericSansSerif, 19, FontStyle.Regular)
CommandColor(i).BackColor = Color.Silver
AddHandler CommandColor(i).Click, AddressOf Button_click
Me.Controls.Add(CommandColor(i))
Next
End Sub
Private Sub Button_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim btn As Button = DirectCast(sender, Button)
Select btn.Name
Case "btn1"
btn.BackColor = Color.Red
Case "btn2"
btn.BackColor = Color.Green
Case "btn3"
btn.BackColor = Color.Yellow
Else
btn.BackColor = Color.Silver
End Select
End Sub
End Class
我已经使用 VB6 很多年了。最近,我正在尝试使用 vb.net.
下面是VB6中的按钮数组和代码,效果很好。
Private Sub CommandColor_Click(Index As Integer)
If Index = 0 Then CommandColor(Index).BackColor = vbRed
If Index = 1 Then CommandColor(Index).BackColor = vbGreen
If Index = 2 Then CommandColor(Index).BackColor = vbYellow
End Sub
- 当我点击
CommandColor(0)
时,它的颜色变为红色。 - 当我点击
CommandColor(1)
时,它的颜色变为绿色。 - 当我点击
CommandColor(2)
时,它的颜色变为黄色。
现在,我正尝试在 VB2010 中做同样的事情。下面的代码是我在建立动态控制数组时完成的。但是,我不知道如何在动态按钮数组中编写“button_Click 的 Sub”代码。任何人都可以帮助我吗?提前致谢。
Public Class Form1
Dim CommandColor(2) As Button
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim i As Integer
For i = 0 To 2
CommandColor(i) = New Button
CommandColor(i).Text = "CommandColor" + Trim(Str(i))
CommandColor(i).ForeColor = Color.Black
CommandColor(i).Left = 50
CommandColor(i).Width = 250
CommandColor(i).Height = 50
CommandColor(i).Top = 50 + 66 * i
CommandColor(i).TextAlign = System.Drawing.ContentAlignment.MiddleCenter
CommandColor(i).Font = New Font(FontFamily.GenericSansSerif, 19, FontStyle.Regular)
CommandColor(i).BackColor = Color.Silver
Me.Controls.Add(CommandColor(i))
Next
End Sub
End Class
很简单,下面会有所帮助,
AddHandler CommandColor(i).Click, AddressOf SubName
以下是您的代码所需的修改,
Public Class Form1
Dim CommandColor(2) As Button
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim i As Integer
For i = 0 To 2
CommandColor(i) = New Button
CommandColor(i).Name = "btn" & (i+1)
CommandColor(i).Text = "CommandColor" + Trim(Str(i))
CommandColor(i).ForeColor = Color.Black
CommandColor(i).Left = 50
CommandColor(i).Width = 250
CommandColor(i).Height = 50
CommandColor(i).Top = 50 + 66 * i
CommandColor(i).TextAlign = System.Drawing.ContentAlignment.MiddleCenter
CommandColor(i).Font = New Font(FontFamily.GenericSansSerif, 19, FontStyle.Regular)
CommandColor(i).BackColor = Color.Silver
AddHandler CommandColor(i).Click, AddressOf Button_click
Me.Controls.Add(CommandColor(i))
Next
End Sub
Private Sub Button_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim btn As Button = DirectCast(sender, Button)
Select btn.Name
Case "btn1"
btn.BackColor = Color.Red
Case "btn2"
btn.BackColor = Color.Green
Case "btn3"
btn.BackColor = Color.Yellow
Else
btn.BackColor = Color.Silver
End Select
End Sub
End Class