Click here to Skip to main content
15,881,559 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
VB.net
Windows Forms

I need to get the selected item in a ComboBox dynamically. I need to do this dynamically because I will make hundreds of ComboBox, and all of them need to have their own separate control.

Right now, with the following code, I am able to generate 3 different controls (including a ComboBox), and then dynamically delete each control. This works great, but I can't figure out how to get or set information from these controls.

Copy and paste the following code in a windows form, and add a FlowLayoutPanel called "pnlLayout", and a button called BtnAdd

Notice the "'get the selected item in a ComboBox" at the bottom of the code

Public Class Form1

    ReadOnly deleteButtons As List(Of Button)
    ReadOnly textBoxes As List(Of TextBox)
    ReadOnly comboBoxes As List(Of ComboBox)

    Sub New()
        ' This call is required by the designer
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call
        deleteButtons = New List(Of Button)
        textBoxes = New List(Of TextBox)
        comboBoxes = New List(Of ComboBox)
    End Sub

    Private Sub BtnAdd_Click(sender As Object, e As EventArgs) Handles BtnAdd.Click

        Dim elementCount As Integer = deleteButtons.Count

        Dim btn As New Button With {
            .Width = 100,
            .Height = 20,
            .Text = "Delete " & elementCount.ToString
        }
        AddHandler btn.Click, AddressOf BtnDelete
        deleteButtons.Add(btn)

        Dim txt As New TextBox With {
            .Width = 100,
            .Height = 20
        }
        textBoxes.Add(txt)

        Dim cmb As New ComboBox With {
            .Width = 100,
            .Height = 20
        }
        AddHandler btn.Click, AddressOf BtnDelete
        AddHandler cmb.SelectedIndexChanged, AddressOf cmbChanged
        comboBoxes.Add(cmb)

        pnlLayout.Controls.Add(btn)
        pnlLayout.Controls.Add(txt)
        pnlLayout.Controls.Add(cmb)
    End Sub

    Private Sub BtnDelete(sender As Object, e As EventArgs)

        Dim senderButton As Button = DirectCast(sender, Button)
        Dim txt As TextBox = textBoxes(deleteButtons.IndexOf(senderButton))
        Dim cmb As ComboBox = comboBoxes(deleteButtons.IndexOf(senderButton))
        pnlLayout.Controls.Remove(senderButton)
        pnlLayout.Controls.Remove(txt)
        pnlLayout.Controls.Remove(cmb)
    End Sub

    Private Sub cmbChanged(sender As Object, e As EventArgs)

        'get the selected item in a ComboBox

    End Sub
End Class


What I have tried:

I tried playing with the btnDelete and tried using it to get information but it did not work for me

I am also new to coding (I am 17 years old)
Posted
Updated 4-Dec-20 13:20pm
v3
Comments
Jo_vb.net 3-Dec-20 19:34pm    
Your Private Sub cmbChanged has no Handles cmb.SelectedIndexChanged

Private Sub cmbChanged(sender As Object, e As EventArgs) Handles cmb.SelectedIndexChanged

Then you should be able to use the cmb.SelectedIndex
Member 15011091 3-Dec-20 20:18pm    
I have

AddHandler cmb.SelectedIndexChanged, AddressOf cmbChanged

then

Private Sub cmbChanged(sender As Object, e As EventArgs)
'get the selected item in a ComboBox
End Sub

That works for me, for example if I add a

MsgBox("test")

inside the sub, the message appears.. but I dont know what to do from there.

cmb.SelectedIndex does not work unfortunately because I will have hundreds of cmb so it wont know which one to choose
Member 15011091 3-Dec-20 20:22pm    
Maybe there is some way of giving each cmb its own name (cmb.name = "cmb" + elementCount)

but then the problem is that I would need to write
Dim cmb1
Dim cmb2
Dim cmb3

...and so on like hundreds or thousands of times (however many I need)

Unless there is some other way?
CoderzF1 3-Dec-20 23:24pm    
are you adding the comboboxes programmatically?
Member 15011091 4-Dec-20 1:22am    
yes, the ComboBox are being added programmatically

This is just a sample of creating a combobox Dynamically and setting a selection changed event for that combobox. I make use of the Tag property to tell which combobox has been changed. Updated...added delete buttons. this may help as well.

VB
Public Class Form1
    Dim cbos(100) As ComboBox
    Dim btns(100) As Button
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        For i As Integer = 0 To 100
            cbos(i) = New ComboBox
            cbos(i).Width = 150
            cbos(i).Height = 24
            cbos(i).Top = i * 24 + 24
            cbos(i).Tag = "Combobox" & i.ToString
            For ii As Integer = 0 To 10
                cbos(i).Items.Add("Item " & ii.ToString)
            Next
            btns(i) = New Button
            btns(i).Top = i * 24 + 24
            btns(i).Tag = i.ToString
            btns(i).Height = 24
            btns(i).Text = "Delete"
            btns(i).Width = "150"
            btns(i).Left = cbos(i).Left + cbos(i).Width + 8
            AddHandler btns(i).Click, AddressOf btn_Clicked
            AddHandler cbos(i).SelectedIndexChanged, AddressOf cbo_selectionchanged
            Me.Controls.Add(cbos(i))
            Me.Controls.Add(btns(i))
        Next
    End Sub

    Private Sub cbo_selectionchanged(sender As Object, e As EventArgs)
        Dim cbo As ComboBox = sender
        MsgBox(cbo.Tag & " - " & cbo.SelectedIndex.ToString)
    End Sub

    Private Sub btn_Clicked(sender As Object, e As EventArgs)
        Dim btn As Button = sender
        RemoveHandler cbos(btn.Tag).SelectedIndexChanged, AddressOf cbo_selectionchanged
        Me.Controls.Remove(cbos(btn.Tag))
        RemoveHandler btns(btn.Tag).Click, AddressOf btn_Clicked
        Me.Controls.Remove(btns(btn.Tag))
    End Sub
End Class
 
Share this answer
 
v3
I used the following code which worked well:
'get the selected item in a ComboBox
Dim senderCombo As ComboBox = DirectCast(sender, ComboBox)
Dim SelectedItem As String = senderCombo.SelectedItem
Label1.Text = SelectedItem

I added the Label Label1 to test the combos. I added the following code to populate the combos during creation:
For i = 0 To 2
    cmb.Items.Add(elementCount & "-" & i)
Next

I hope this helps!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900