Gav,
One way you can do this is to create a class for the combo item. For
example,
public class MyComboBoxItem
{
private string _name;
private int _value;
public MyComboBoxItem(string name, int value)
{
_name = name;
_value = value;
}
public override string ToString()
{
return _name;
}
}
Now, add this to your combo box. For example,
comboBox1.Items.Add(new MyComboBoxItem("Test", 134));
The item will display in the combo box based on how you implemented
ToString(). In this case, the name is displayed. But, when the object is
selected, you have this object, which may contain as much data as you need.
In many cases you will already have a data class that has all the data in it
you need, but the ToString() method does not display what you want. If you
are not using the ToString() method, and it makes sense, add an override as
shown above to that class, and just stick your data class in the ComboBox.
Or, you can create a class that hold your data class with just the method to
override ToString(), and a method to get at your data class, like a Value
property.
Hope this helps...
Frisky
"Gav" <ga************@nospam.portakabin.com> wrote in message
news:da**********@newsreaderg1.core.theplanet.net. ..
"rossum" <ro******@coldmail.com> wrote in message
news:nl********************************@4ax.com... On Thu, 7 Jul 2005 13:19:13 +0100, "Gav"
<ga************@nospam.portakabin.com> wrote:
I am using VS 2005 and am trying to add items to a combo box using C#. I
know how to add simple text items but I am trying to add a value and some
text ie.
Value Text
A First Text
B Second Text
I've seen examples of ListItem but I can't seem to get this to work, can
anybody offer any advice.
thanks
Gavin
Convert the value to a string, concatenate it with the text that you
want and add the whole thing to your combo box.
rossum
But that will make the value visable, that is not what I want to do. I
thought there was a way to have a value and a bit of text seperate. ie
value
is what I use in the program to determine what was chosen and text is
what is visable to the user.
Gav
The ultimate truth is that there is no ultimate truth