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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Combobox in C#

Status
Not open for further replies.

yfbf

Programmer
Oct 2, 2004
24
BE
I looking for insert for each Item a Tag.
How can I do this ? Because I finding only a Tag for the entire combobox
 
All the items in a combo box are of type Object. Which means that you can insert objects of any type in there because all objects in C# inherit from Object.

So if you have a need to store more info in a combo box item that what the control will allow, all you do is create a new class (call it "ComboItemEx") that has public properties for the info you want to store. You would also override the ToString method to return the text you want displayed (the .NET Combo control calls ToString to show the values to the user).
Code:
public class ComboItemEx
{
   private String m_name;
   private int m_value;

   public ComboItemEx(String name, int value)
   {
      m_name = name;
      m_value = value;
   }

   ' add your properties here
   ' Name, Value

   public override String ToString()
   {
      return m_name;
   }
}
And then when you load up the combo with your items, you would use instances of your new class instead of what you had been doing.
Code:
MyComboBox.Add(New ComboItemEx("foo", 41));
MyComboBox.Add(New ComboItemEx("bar", 123));
The combo box will have two items named foo and bar, each of which has a "hidden" value which you can retrieve via the SelectedItem property:
Code:
int ZeeValue = MyComboBox.SelectedItem.Value;
Chip H.

____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Hi,

thanks for your response.

I would like to use the same properties like ListViewItem but with ComboBox


ListViewItem lvItem = null;
lvItem = new ListViewItem(curentActionTO.Id.ToString(), 0);
lvItem.SubItems.Add(curentActionTO.Name);
lvItem.SubItems.Add(curentActionTO.Description);
lvItem.SubItems.Add(curentActionTO.Delay.ToString());
lvItem.SubItems.Add(curentActionTO.MatrixInput.Id.ToString());
lvItem.SubItems.Add(curentActionTO.MatrixInput.Mnemonic);
lvItem.SubItems.Add(curentActionTO.MatrixInput.Description);
lvItem.SubItems.Add(curentActionTO.MatrixOutput.Id.ToString());
lvItem.SubItems.Add(curentActionTO.MatrixOutput.Mnemonic);
lvItem.SubItems.Add(curentActionTO.MatrixOutput.Description);
lvItem.Tag = curentActionTO;
this.listViewAction.Items.Add(lvItem);
 
If you wanted to use a list view, why did you title the thread "Combobox in C#" ???

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top