在表单中动态添加多个ComboBox并读取添加的ComboBoxes的输入?

时间:2017-03-17 06:37:15

标签: .net vb.net visual-studio controls

我需要在表单中添加可变数量的ComboBox,稍后我需要读取ComboBox中的输入。

我有这个代码 -

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

我可以添加ComboBoxes。但是我想在稍后单击Button2时读取ComboBox的输入。我不能。我怎样才能做到这一点?

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles 

    Button2.Click
            TextBox1.Text = MyComboBox1.selectedItem
        End Sub

我需要这样的输出。

3 个答案:

答案 0 :(得分:1)

您可以按名称访问控件:

MsgBox(Me.Controls("MyComboBox" & intYourComboBoxNumber).SelectedItem)

答案 1 :(得分:1)

您可以在表单的类中声明List(Of ComboBox);

Private ComboBoxes As New List(Of ComboBox)

您可以做的是在创建时将动态创建的ComboBox添加到该列表中;

ComboBoxes.Add(newCombo)

要稍后再打电话,只要不处理,你可以这样做,例如:

TextBox1.Text = ComboBoxes(0).SelectedItem ' Rather than 1, do 0 - zero-based index.

也;请注意,ComboGen应该是Sub - 而不是Function,因为您始终返回0并且永远不会得到正确的&#34;结果 - 但是,您可以将代码封装在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

答案 2 :(得分:1)

您可以为您创建的每个组合框添加事件处理程序。这样,您可以在运行时轻松使用所有组合框属性。

完整示例:

    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