Items in toolstripcombobox

pisceswzh

Well-known member
Joined
Mar 19, 2007
Messages
96
Programming Experience
1-3
Hi,

In the toolstripcombobox tool I can use

VB.NET:
Me.toolstripcombobox.Items.Add("abc")

to add a new item into the toolstripcombobox.

Now the problem is for the item in this toolstripcombobox, I want to add a tag for each item in it. e.g. for the "abc" I've just added, there might be an ID in the database that refer to it. So everytime the user select "abc", I can get the ID of the item the user have selected.

Is there a way to do that? Thanks!
 
Yes and no. The ComboBox class offers no way to do this without binding your data. First you need a list of items containing both values. This list could be a DataTable with a column for each of the values or it could be an array or collection of class or structure instances with a property for each value. You then assign the name of the column or property you want displayed to the DisplayMember property, the name of the column or property containing the keys to the ValueMember property and the list itself to the DataSource property. Now, when the user makes a selection you get the corresponding key from the SelectedValue property.

You should also be aware that a ToolStripComboBox is NOT a ComboBox. It is a ToolStripControlHost customised to host a ComboBox control. That control is exposed via the ComboBox property.
VB.NET:
With myToolStripComboBox.ComboBox
    .DisplayMember = "Name"
    .ValueMember = "ID"
    .DataSource = myDataTable
End With
 
Back
Top