When looking at your code, one thing that is not really an issue as much as having to re-invent the wheel. Your ComboboxItem class looks suspiciously like a Dictionary, so it seems unnecessary to reinvent this class with your ComboboxItem class.
Another issue is your comment…
I need to select an item programatically based on the key but I cannot use the suggestion made here: Set the selecteditem of a
combobox based on key,value pair. because I don't have foreknowledge
of the data items as the ComboBox gets populated at runtime.
If this is true, then how could you possibly know what to select?
The code below used a Dictionary, ComboBox, Textbox and Button. The Dictionary is populated with some data and is then set to the ComboBox’s DataSource. Only the Text is displayed in the ComboBox. On the form the TextBox is used to allow the user to type in a value to change the ComboBox’s selected value to when the Button is clicked. If that key does not exist in the Dictionary then a message is display indicating the index was not found.
Since you say you do not know exactly what you may be looking for, I am curious how you expect to select the proper index. Hope this makes sence.
Dictionary<int, string> comboBoxData = new Dictionary<int, string>();
public Form1() {
InitializeComponent();
comboBoxData = GetDataDictionary();
comboBox1.DataSource = new BindingSource(comboBoxData, null);
comboBox1.DisplayMember = "Value";
comboBox1.ValueMember = "Key";
}
private void SetSelectedIndex(int indexToSelect) {
if (comboBoxData.Keys.Contains(indexToSelect)) {
comboBox1.SelectedIndex = indexToSelect;
}
else {
MessageBox.Show("The supplied key does not exist!");
}
}
private Dictionary<int, string> GetDataDictionary() {
Dictionary<int, string> dictionary = new Dictionary<int, string>();
for (int i = 0; i < 15; i++) {
dictionary.Add(i, "Item name " + i);
}
return dictionary;
}
private void buttonSelectIndex_Click(object sender, EventArgs e) {
if (tbIndexToSelect.Text != "") {
int indexToSelect = comboBox1.SelectedIndex;
if (int.TryParse(tbIndexToSelect.Text, out indexToSelect)) {
SetSelectedIndex(indexToSelect);
}
}
}