Here is what I advise to do: don't store strings in the combo box items. Who told you that you need to have strings? The
runtime type of the list element is
System.Object
:
http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.objectcollection.add.aspx[
^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.objectcollection.aspx[
^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.objectcollection.add.aspx[
^].
Use any custom object, of some class or structure you can design to encapsulate all the detail on each item required by your functionality. The only problem is: what will be displayed in the UI for each item? The answer is simple: whatever the function
ToString()
returns. So, you have to override
System.Object.ToString()
. For example:
internal class MyComboBoxItem {
internal MyComboBoxItem(string title, int itemNumber ) {
this.Title = title;
this.ItemNumber = itemNumber;
}
internal int ItemNumber { get; set; }
internal string Title { get; private set; }
public override string ToString() {
return Title;
}
}
Now, you can add any number of the objects of the class shown above. During runtime, you will be able to reduce all your switch statement (unsupportable, totally unsupportable) to just a few lines, regardless of the actual number of items:
myComboBox.Items.Add(new MyComboBoxItem("Some item title", someIntegerValueForItemNumber ));
if (myComboBox.SelectedItem != null) {
MyComboBoxItem item = (MyComboBoxItem)myComboBox.SelectedItem;
int itemNum = item.ItemNumber;
}
Are you getting the idea?
—SA