Hi,
I would like to to to store the Name and Items of ComboBox into dictionary. With the below code,I am able to store them. However, Name of ComboBox is as counting ComboBox10 to ComboBox1n. I would like to store Name of ComboBox as ComboBox1,ComboBox2 rather than ComboBox10 to ComboBox1n for my futher propose.

It's not working with _dic.Add(ctl.Name, myCombo.Items(j).ToString) so I have to loop as _dic.Add(ctl.Name & j.ToString, myCombo.Items(j).ToString).

Would Appreciate your help and suggestion.

                 If TypeOf ctl Is ComboBox Then
                    myCombo = CType(ctl, ComboBox)
                    For j As Integer = 0 To myCombo.Items.Count - 1
                     _dic.Add(ctl.Name & j.ToString, myCombo.Items(j).ToString)
                 Next

Output
ComboBox10 One
ComboBox11 two
ComboBox12 Three

ComboBox20 Four
ComboBox21 Five
ComboBox22 Six

Dani AI

Generated

A clean way to keep each ComboBox's original name while storing all of its items is to make the dictionary value a collection instead of trying to use duplicate keys. That preserves names like ComboBox1 and ComboBox2 exactly as wanted, and avoids the need to append indexes as did. It also makes access straightforward when you need all items for a given ComboBox later.

Example approach (WinForms): build a Dictionary(Of String, List(Of String)) where the key is the control name and the value is the ordered list of items. The helper below also handles ComboBoxes nested inside panels or group boxes.

Dim map As New Dictionary(Of String, List(Of String))()

For Each cbx As ComboBox In GetAllControls(Me).OfType(Of ComboBox)()
    Dim items = cbx.Items.Cast(Of Object)().Select(Function(o) o.ToString()).ToList()
    map(cbx.Name) = items
Next

' read back
For Each kvp In map
    Console.WriteLine(kvp.Key)
    For Each itm In kvp.Value
        Console.WriteLine("  " & itm)
    Next
Next

Private Function GetAllControls(root As Control) As IEnumerable(Of Control)
    Dim results As New List(Of Control)()
    For Each c As Control In root.Controls
        results.AddRange(GetAllControls(c))
        results.Add(c)
    Next
    Return results
End Function

Notes and troubleshooting:

  • Use List(Of String) to preserve item order; use map(cbx.Name).Add(...) to append later. Check If Not map.ContainsKey(name) before adding.
  • If you actually need duplicate key entries in sequence (not grouped), use a List(Of KeyValuePair(Of String,String)) or ToLookup/ILookup instead of a Dictionary.
  • If this is an ASP.NET scenario (tags suggest it), replace ComboBox semantics with your server control (DropDownList) and persist the map server-side or in ViewState/JSON as needed.
  • If a ComboBox has complex objects, call .ToString() or extract the property you need when populating the list.

Recommended Answers

All 5 Replies

Try

Public Class Form1

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

        Dim dic As New Dictionary(Of String, String)

        For Each cbx As ComboBox In Me.Controls.OfType(Of ComboBox)()

            For i As Integer = 0 To cbx.Items.Count - 1
                dic.Add(cbx.Name & "-" & i, cbx.Items(i))
            Next

        Next

        'display the dictionary in a listbox for debug

        For Each key As String In dic.Keys
            ListBox1.Items.Add(key & ": " & dic(key))
        Next

    End Sub

End Class

This is also counting as
ComboBox1-0 One
ComboBox1-1 Two
ComboBox1-2 Three

I need Original name of ComboBox
e.g

ComboBox1 one
ComboBox1 Two
ComboBox1 Three

ComboBox2 Four
ComboBox2 Five
ComboBox2 Six

Hi
The dictionary does not allow duplicate key to be inserted in it.
However, I need to store the same name of the Combobox with it's Items for my future propose.
Is there a way to insert duplicate Keys into dinctionary?

You are correct. The dictionary does not allow duplicate keys, however, in my example there are no duplicate keys. I populated twp comboboxes with

the
quick
brown
fox

and the output is

ComboBox2-0: the
ComboBox2-1: quick
ComboBox2-2: brown
ComboBox2-3: fox
ComboBox1-0: the
ComboBox1-1: quick
ComboBox1-2: brown
ComboBox1-3: fox

You see that I avoided the duplicates by adding the index value to the key. Note the "-" separator so that you can recover the exact name of the combobox.

Thank you very much. I resolved my problem.

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.