none
Dynamically add Multiple ComboBoxes in form and read the input of added ComboBoxes?

    Question

  • I need to add variable numbers of ComboBoxes in my form and later I need to read the inputs from the ComboBoxes.

    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
                .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? 

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            TextBox1.Text = MyComboBox1.selectedItem
        End Sub

    I need output like this.



    Friday, March 17, 2017 6:30 AM

Answers

  • The combobox can be accessed by number. An example:

       Dim i = 1

       Dim combo As ComboBox = CType(Me.Controls.Find("MyComboBox" & i, True).First, ComboBox)

       Dim value = combo.SelectedValue


    Organise a loop for i.

    • Marked as answer by Sayom Shakib Friday, March 17, 2017 7:32 AM
    Friday, March 17, 2017 7:18 AM

All replies

  • The combobox can be accessed by number. An example:

       Dim i = 1

       Dim combo As ComboBox = CType(Me.Controls.Find("MyComboBox" & i, True).First, ComboBox)

       Dim value = combo.SelectedValue


    Organise a loop for i.

    • Marked as answer by Sayom Shakib Friday, March 17, 2017 7:32 AM
    Friday, March 17, 2017 7:18 AM
  • Yeah. This worked for me. Thanks 
    Friday, March 17, 2017 7:33 AM
  • Hello,

    I see you have a solution, just wanted to offer another one, has more code but believe it's worth looking at. I used a NumericUpDown so no conversion is needed for string to integer.

    Public Class Form1
        Private theComboBox As ComboBox
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim n As Integer = CInt(NumericUpDown1.Value)
            Dim i As Integer = 0
            Do While i <= n
                ComboGen(i)
                i = i + 1
            Loop
        End Sub
        Public Sub ComboGen(ByVal n As Integer)
            Dim newCombo As New ComboBox
            With newCombo
                .Name = "MyComboBox" & n.ToString
                .Left = 10
                .Top = 10 + (20 * n) + 20
                .Items.Add("A")
                .Items.Add("B")
                .Visible = True
            End With
            AddHandler newCombo.Enter,
                AddressOf ForAllComboBoxesGenderated_Enter
            Controls.Add(newCombo)
        End Sub
        Private Sub ForAllComboBoxesGenderated_Enter(sender As Object, e As EventArgs)
            theComboBox = CType(sender, ComboBox)
        End Sub
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            If theComboBox IsNot Nothing Then
                If Not String.IsNullOrWhiteSpace(theComboBox.Text) Then
                    TextBox1.Text = theComboBox.Text
                Else
                    ' no item selected
                End If
            Else
                ' the user did not select a combo or none have been created
            End If
        End Sub
    End Class
    

    Need to know the name of the selected combo

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        If theComboBox IsNot Nothing Then
            MessageBox.Show(theComboBox.Name)
        End If
    End Sub


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Friday, March 17, 2017 7:57 AM
    Moderator