komodosp
komodosp

Reputation: 3656

Assign a non-sequential index to items in a ComboBox

Is there a way of programmatically assigning an index to an item in a ComboBox, as I add the items, rather than using the default sequential 0-based index?

e.g. consider the HTML equivalent, the <select> box...

<select name="test">
  <option value="0">Zero</option>
  <option value="3">Three</option>
  <option value="4">Four</option>
  <option value="324">Sixteen Score and Four</option>
</select>

I'm trying to simulate the "value" attribute in a combo box in c#.

I know the alternative is to set up another array which maps the default Index to "my" index, but this seems somewhat inelegant, seeing as this will have to be accessed in numerous places...

Upvotes: 2

Views: 169

Answers (1)

adv12
adv12

Reputation: 8551

You can define a simple class that has a Value property and a Text property (for example), and add items of that type (rather than strings) to the ComboBox. Just make sure the class's ToString method returns what you want displayed in the ComboBox.

Upvotes: 3

Related Questions