You can but this is wrong approach.
The right approach is:
don't store strings in the list of the ComboBox, store instances of some class or structure:
class ItemHelper {
internal ItemHelper(string name, int id) {
this.fName = name; this.fId = id;
}
public override void ToString() {
return string.Format("{0}:{1}", this.fId, this.fName);
}
internal Name { get { return fName; } }
internal Id { get { return fId; } }
string fName;
int fId;
}
Why
ToString
is overridden? To define how the item is presented in the string form when added to the list (of
ComboBox
or any other list, also a
TreeView
).
This way, you can access to the
Id
directly, without any string tricks, in a reliable way. Well, you will need to cast the item you need to
ItemHelper
.
—SA