我有一个VB表单,可以在运行时动态创建名为1,2,3和4的4个组合框。问题在于,当涉及到访问它们时,我认为最好的方法是执行以下操作,但当然这根本不起作用,任何想法?
谢谢, 萨姆。
Public Class Form1
Dim x As Integer
Dim y As Integer
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
x = 4
y = 0
Dim MyLocationX As Integer = 25
Dim MyLocationY As Integer = 25
Do While y <> x
Dim DropDownlist As New ComboBox
DropDownlist.Name = x
DropDownlist.Location = New Point(MyLocationX, MyLocationY)
Me.Controls.Add(DropDownlist)
y = y + 1
MyLocationY = MyLocationY + 30
Loop
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim z as Integer = 0
Do While z <> x
Dim z As New ComboBox
MsgBox(z.SelectedValue)
z++
Loop
End Sub
结束班
答案 0 :(得分:0)
比我的第一个建议更强大:
Public Class Form1
Private _comboBoxes(3) As ComboBox
Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
Dim MyLocationX As Integer = 25
Dim MyLocationY As Integer = 25
For i As Integer = 1 To 4
Dim DropDownlist As New ComboBox()
DropDownlist.Name = i.ToString()
DropDownlist.Location = New Point(MyLocationX, MyLocationY)
_comboBoxes(i - 1) = DropDownlist
Me.Controls.Add(DropDownlist)
MyLocationY = MyLocationY + 30
Next
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
For i As Integer = 1 To 4
MessageBox.Show(_comboBoxes(i - 1).SelectedValue)
Next
End Sub
End Class
答案 1 :(得分:0)
我会用AddHandler做不同的事情。因为这样你可以避免使用按钮来收集值。只需要一个包含所有值的列表,如果有更改,则更改列表中的值。 我还想要我在代码末尾添加的按钮
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
x = 4
y = 0
Dim MyLocationX As Integer = 25
Dim MyLocationY As Integer = 25
Do While y <> x
Dim DropDownlist As New ComboBox
DropDownlist.Name = x
DropDownlist.Location = New Point(MyLocationX, MyLocationY)
'------
AddHandler DropDownlist.SelectedIndexChanged, AddressOf controlValueChanged
'-------
Me.Controls.Add(DropDownlist)
y = y + 1
MyLocationY = MyLocationY + 30
Loop
End Sub
Private Sub controlValueChanged(sender As System.Object, e As System.EventArgs)
'This event is fired when you change the selection in one of your comboboxes
Dim cbo As combobox= sender 'Sender is the combobox that you change its selection
'Do whatever you like with cbo
End Sub
不要忘记清除自定义事件(非托管内存)
Private Sub removeControlValueChangedEvents()
'Call that Sub when your form is closed
For each cbo as combobox in Me.Controls
RemoveHandler DirectCast(cbo , ComboBOx).SelectedIndexChanged, AddressOf controlValueChanged
Next
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
For each cbo as combobox in Me.Controls
MessageBox.Show(cbo.SelectedValue)
Next
End Sub
答案 2 :(得分:0)
您还可以使用Controls.Find方法在Forms ControlCollection中找到您的ComboBox
Public Class Form1
Dim maxDropDowns, y As Integer
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
maxDropDowns = 4
y = 0
Dim MyLocationX As Integer = 25
Dim MyLocationY As Integer = 25
Do While y < maxDropDowns
Dim DropDownlist As New ComboBox
DropDownlist.Name = (y + 1).ToString
DropDownlist.Location = New Point(MyLocationX, MyLocationY)
Me.Controls.Add(DropDownlist)
y = y + 1
MyLocationY = MyLocationY + 30
Loop
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim cntrls() As Control
For z = 1 To maxDropDowns
cntrls = Controls.Find(z.ToString, True)
If cntrls.Count > 0 Then
MsgBox(CType(cntrls(0), ComboBox).SelectedValue)
End If
Next
End Sub
End Class
或者您可以使用Dictionary,您有许多不同的选项,这取决于您想要做什么,我的首选是创建一个控件数组分配常见的事件处理程序并拉出控制,从中引发事件发件人对象。
Public Class Form1
Dim maxDropDowns, y As Integer
Dim myControls As Dictionary(Of Integer, ComboBox) = New Dictionary(Of Integer, ComboBox)
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
maxDropDowns = 25
y = 0
Dim MyLocationX As Integer = 25
Dim MyLocationY As Integer = 25
Do While y < maxDropDowns
Dim DropDownlist As New ComboBox
DropDownlist.Name = (y + 1).ToString
DropDownlist.Location = New Point(MyLocationX, MyLocationY)
myControls.Add(y, DropDownlist)
Me.Controls.Add(DropDownlist)
MyLocationY = MyLocationY + 30
y = y + 1
Loop
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
For z = 0 To maxDropDowns - 1
MsgBox(myControls(z).SelectedValue)
Next
End Sub
End Class