Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ComboBox questions

Status
Not open for further replies.

iranor

Programmer
Jun 17, 2004
174
CA
So I have 2 questions about the combobox component. The first one is, how can I add a different color for each item in the combo box? Like the first one the background color is red, the second blue, etc.

The second one, is there a way to associate a value with an item? Like in html there is <option value="red">Red color</option>. Is there a way to do this?

Because if I have 3 items for example, Red, Black, Blue with respectively the ID 1 2 and 3, if I remove Black, I still want blue to have the ID 3...

Thanks in advance.
 
Hi,

1. Set the DrawMode to OwnerDrawFixed and write code in the DrawItem event handler. Have a search here and in google. It isnt difficult.

2. If you bind it to a datasource (e.g. database) then you set the displayMember to a field/variable which will be displayed. Now the hidden value is the valueMember. Set it to a field/variable. To get the value for the selected item, call the .selectedValue property.

As for the removal and the option to keep the value, add the items to the combo and then add the values to an arraylist. If you decide to remove from the combo anything, simply do not remove the .SelectedIndex for the arraylist item collection.


Hope these help.
 
Thanks, i'll have a look for it!

I don't bind it to a database, I add the fields manually throught code. Can I use a value that way?
 
I will try the next myself.
Bind to an object is the key. The object/class should have a text (to be displayed) and a key (value)

 
As you don't use a database, you could place your IDs in the Tag property (or even create your own UserControl (inheriting it fron System.Windows.Forms.ComboBox) and add an ID property to it).


Hope this helps.

[vampire][bat]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top