Hi ,
I am able to add Label, button,texboxes into Dictionary. However I am getting problem to add values of Combobox into dictionary. Here is how I was trying to do.

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        Try
        Dim myLabel As Label
        Dim myButton As Button
        Dim myRadio As RadioButton
        Dim myCombo As ComboBox = Nothing

       _dic.Clear()

        For Each ctl As Control In Me.Controls

          If TypeOf ctl Is Label Then
                myLabel = CType(ctl, Label)
                _dic.Add(ctl.Name, myLabel.Text)


            ElseIf TypeOf ctl Is Button Then
                myButton = CType(ctl, Button)
                _dic.Add(ctl.Name, myButton.Text)

            ElseIf TypeOf ctl Is RadioButton Then
                myRadio = CType(ctl, RadioButton)
                _dic.Add(ctl.Name, myRadio.Text)

            ElseIf TypeOf ctl Is ComboBox Then                 
                myCombo = CType(ctl, ComboBox)
                _dic.Add(ctl.Name, ComboBox1.SelectedItem)

            End If

        Next


        For Each Item As KeyValuePair(Of String, String) In _dic

            Debug.Print(Item.Key & " " & Item.Value)

        Next

    Catch ex As Exception
        Debug.Print(ex.ToString)
    End Try

End Sub

Would greatly appreciate your help.

Thanks.

Recommended Answers

All 3 Replies

You didn't show us how you declared _dic, or what error message you were getting. In any case, try

_dic.Add(ctl.Name, ComboBox1.Text)

Hi,
Thanks for reply.
With the below code, I am able to add the values into dictionary. However, I want to add the values of Cobmobox from Form Design View and that values should store into the dictionary. _dic.Add(ctl.Name, ComboBox1.Text) is to add values of label,textbox,button etc. I think there should be a different method for Combobox and ListBox

                _dic.Add("1", "One")
                _dic.Add("2", "Two")

                myCombo.DataSource = New System.Windows.Forms.  
                BindingSource(_dic, Nothing)

                myCombo.DisplayMember = "Value"

                myCombo.ValueMember = "Key"

I want to add the values of Cobmobox from Form Design View

You can't add the dictionary as a control. You can only declare it in code which means you can only add items to it at run time.

I think there should be a different method for Combobox and ListBox

What you are doing is adding strings to the dictionary. Why should the dictionary care where the strings come from?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.