Search code examples
c#.netdictionarycomboboxsorteddictionary

Combobox selection based on FindStringExact not working as intended


I'm trying to select the item in the Combobox based on the stored value in a Sorteddictionary

String value matching

  comboBoxEdit3.SelectedItem = comboBoxEdit3.FindStringExact(Queries[_ucSetting.StandardSearchID.ToString()] + "(" + _ucSetting.StandardSearchID.ToString() + ")");

enter image description here

Elements in ComboBox

enter image description here

But this produces empty selection in the ComboBox


Solution

  • FindStringExact only returns the index of the first item that matches your string, or -1 if no match is found. You're trying to set the SelectedItem to the index being returned. You should set the SelectedIndex instead:

    comboBoxEdit3.SelectedIndex = comboBoxEdit3.FindStringExact(Queries[_ucSetting.StandardSearchID.ToString()] + "(" + _ucSetting.StandardSearchID.ToString() + ")");