I need to add variable numbers of ComboBoxes in my form and later I need to read the inputs from the ComboBoxes.
我需要在我的表单中添加可变数量的ComboBox,稍后我需要从ComboBox中读取输入。
I have this code-
我有这个代码 -
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim n As Integer = Int(QsnNo.Text)
Dim i As Integer = 0
Do While i <= n
ComboGen(i)
i = i + 1
Loop
End Sub
Public Function ComboGen(ByVal n As Integer)
Dim newCombo As New ComboBox
With newCombo
.Name = "MyComboBox" & n.ToString
.Left = 10
.Top = 10 + (20 * n) + 20
.Width = 70
.Height = 20
.Items.Add("A")
.Items.Add("B")
.Visible = True
End With
Me.Controls.Add(newCombo)
Return 0
End Function
And I can add ComboBoxes. But I want to read the inputs from the ComboBoxes later when I Click on Button2. That I can't. How can I do that?
我可以添加ComboBoxes。但是我想在稍后单击Button2时读取ComboBox的输入。我不能。我怎样才能做到这一点?
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles
Button2.Click
TextBox1.Text = MyComboBox1.selectedItem
End Sub
I need output like this.
我需要像这样的输出。
3 个解决方案
#1
1
You can add event handler to each combobox you create. This way you can easily use all comboboxes properties at runtime.
您可以为您创建的每个组合框添加事件处理程序。这样,您可以在运行时轻松使用所有组合框属性。
Full example:
完整示例:
Public Class Form1
Dim intTop As Integer = 1
Dim strText As String
Dim cmb As ComboBox
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
AddNewTextBox()
AddHandler cmb.GotFocus, AddressOf cmb_Click
End Sub
Public Function AddNewTextBox() As ComboBox
cmb = New ComboBox
Me.Controls.Add(cmb)
cmb.Top = intTop * 25
cmb.Left = 10
cmb.Text = "ComboBox " & Me.intTop.ToString
intTop = intTop + 1
Return cmb
End Function
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
TextBox1.Text = strText
End Sub
Private Sub cmb_Click(sender As Object, e As EventArgs)
strText = sender.Text
End Sub
End Class
#2
1
You can access controls by its name:
您可以通过其名称访问控件:
MsgBox(Me.Controls("MyComboBox" & intYourComboBoxNumber).SelectedItem)
#3
1
What you can do is declare a List(Of ComboBox)
in your form's class;
你可以做的是在你的表单类中声明一个List(Of ComboBox);
Private ComboBoxes As New List(Of ComboBox)
What you then can do is add your dynamically-created ComboBox to that list when it's created;
您可以做的是在创建时将动态创建的ComboBox添加到该列表中;
ComboBoxes.Add(newCombo)
To call upon this later, as long as it's not disposed, you can do, for example:
要稍后调用它,只要它没有被处理掉,你可以这样做,例如:
TextBox1.Text = ComboBoxes(0).SelectedItem ' Rather than 1, do 0 - zero-based index.
Also; note that ComboGen
should be a Sub
- not a Function
as you're always returning 0
and never getting a "proper" result - however, you could encapsulate your code in a Try/Catch
to return a Boolean, True
if succeeded, False
if not.
也;请注意,ComboGen应该是一个Sub而不是一个函数,因为你总是返回0并且永远不会得到“正确”的结果 - 但是,你可以将你的代码封装在Try / Catch中以返回一个布尔值,如果成功则返回True,如果成功则返回False不。
Public Function ComboGen(ByVal n As Integer) As Boolean
Try ' Start of your Try/Catch block.
Dim newCombo As New ComboBox
With newCombo
.Name = "MyComboBox" & n.ToString
.Left = 10
.Top = 10 + (20 * n) + 20
.Width = 70
.Height = 20
.Items.Add("A")
.Items.Add("B")
.Visible = True
End With
ComboBoxes.Add(newCombo) ' Add your ComboBox to the list.
Me.Controls.Add(newCombo)
Return True
Catch ex As Exception ' Catch your exception.
Return False
End Try ' End the Try/Catch block.
End Function
#1
1
You can add event handler to each combobox you create. This way you can easily use all comboboxes properties at runtime.
您可以为您创建的每个组合框添加事件处理程序。这样,您可以在运行时轻松使用所有组合框属性。
Full example:
完整示例:
Public Class Form1
Dim intTop As Integer = 1
Dim strText As String
Dim cmb As ComboBox
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
AddNewTextBox()
AddHandler cmb.GotFocus, AddressOf cmb_Click
End Sub
Public Function AddNewTextBox() As ComboBox
cmb = New ComboBox
Me.Controls.Add(cmb)
cmb.Top = intTop * 25
cmb.Left = 10
cmb.Text = "ComboBox " & Me.intTop.ToString
intTop = intTop + 1
Return cmb
End Function
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
TextBox1.Text = strText
End Sub
Private Sub cmb_Click(sender As Object, e As EventArgs)
strText = sender.Text
End Sub
End Class
#2
1
You can access controls by its name:
您可以通过其名称访问控件:
MsgBox(Me.Controls("MyComboBox" & intYourComboBoxNumber).SelectedItem)
#3
1
What you can do is declare a List(Of ComboBox)
in your form's class;
你可以做的是在你的表单类中声明一个List(Of ComboBox);
Private ComboBoxes As New List(Of ComboBox)
What you then can do is add your dynamically-created ComboBox to that list when it's created;
您可以做的是在创建时将动态创建的ComboBox添加到该列表中;
ComboBoxes.Add(newCombo)
To call upon this later, as long as it's not disposed, you can do, for example:
要稍后调用它,只要它没有被处理掉,你可以这样做,例如:
TextBox1.Text = ComboBoxes(0).SelectedItem ' Rather than 1, do 0 - zero-based index.
Also; note that ComboGen
should be a Sub
- not a Function
as you're always returning 0
and never getting a "proper" result - however, you could encapsulate your code in a Try/Catch
to return a Boolean, True
if succeeded, False
if not.
也;请注意,ComboGen应该是一个Sub而不是一个函数,因为你总是返回0并且永远不会得到“正确”的结果 - 但是,你可以将你的代码封装在Try / Catch中以返回一个布尔值,如果成功则返回True,如果成功则返回False不。
Public Function ComboGen(ByVal n As Integer) As Boolean
Try ' Start of your Try/Catch block.
Dim newCombo As New ComboBox
With newCombo
.Name = "MyComboBox" & n.ToString
.Left = 10
.Top = 10 + (20 * n) + 20
.Width = 70
.Height = 20
.Items.Add("A")
.Items.Add("B")
.Visible = True
End With
ComboBoxes.Add(newCombo) ' Add your ComboBox to the list.
Me.Controls.Add(newCombo)
Return True
Catch ex As Exception ' Catch your exception.
Return False
End Try ' End the Try/Catch block.
End Function