.NET ComboBox Item Tags (SetItemData equivalent)
-
I am trying to set tags for individual Combobox items. In looking through the Combobox doc, I cannot find the equivalent of SetItemData/GetItemData. The tag property is set at the Combobox level, and not at the item level. Can someone point me to an example, or how to set the tags for invidual combobox items. I would like to store database record ids as tags in the combobox. Gaulles Gaulles Gaulles Technologies, Inc. http://www.gaulles.com
-
I am trying to set tags for individual Combobox items. In looking through the Combobox doc, I cannot find the equivalent of SetItemData/GetItemData. The tag property is set at the Combobox level, and not at the item level. Can someone point me to an example, or how to set the tags for invidual combobox items. I would like to store database record ids as tags in the combobox. Gaulles Gaulles Gaulles Technologies, Inc. http://www.gaulles.com
Hi Do this:
public class ComboBoxItem
{
object tag = null;
string text = null;public ComboBoxItem(string text)
{
this.text = text;
}
public object Tag {get {return tag;} set {tag = value;}}public override string ToString(){return text;}
}Now just add these objects to ComboBox.Items. ie
comboBox1.Items.Add( new ComboBoxItem("some text"));
You can set/get the tags via:
object tag = ((ComboBoxItem) comboBox1.Items[i]).Tag; //note cast. [edit] oops made a mistake, correct now :)[/edit]
Hope this helps :) Give them a chance! Do it for the kittens, dear God, the kittens!
-
Hi Do this:
public class ComboBoxItem
{
object tag = null;
string text = null;public ComboBoxItem(string text)
{
this.text = text;
}
public object Tag {get {return tag;} set {tag = value;}}public override string ToString(){return text;}
}Now just add these objects to ComboBox.Items. ie
comboBox1.Items.Add( new ComboBoxItem("some text"));
You can set/get the tags via:
object tag = ((ComboBoxItem) comboBox1.Items[i]).Tag; //note cast. [edit] oops made a mistake, correct now :)[/edit]
Hope this helps :) Give them a chance! Do it for the kittens, dear God, the kittens!
I thought of doing something similar, but because this was already part of MFC (SetItemData/GetItemData), I was digging around to see if it is already part of the .NET framework ComboBox class. I cannot understand it when MS takes away features that were already existing, and used by several applications. Thanks for the info. I will implement it.
-
I thought of doing something similar, but because this was already part of MFC (SetItemData/GetItemData), I was digging around to see if it is already part of the .NET framework ComboBox class. I cannot understand it when MS takes away features that were already existing, and used by several applications. Thanks for the info. I will implement it.
Gaul wrote: I thought of doing something similar, but because this was already part of MFC (SetItemData/GetItemData), You will ussually add an object to ComboBox.Items and then just override the ToString() method in the objects's class. Alternatively, you can display any property (obvously the ToString() method gets called on the Property's reflected type) with ComboBox.ValueMember. NOTE: however there is a bug ,according to me anyways, when the ComboBox gets sorted and retrieving SelectedItem in relation to SelectedValue. Allways makes sure the object is the same. Hope this helps :) Give them a chance! Do it for the kittens, dear God, the kittens!
-
I thought of doing something similar, but because this was already part of MFC (SetItemData/GetItemData), I was digging around to see if it is already part of the .NET framework ComboBox class. I cannot understand it when MS takes away features that were already existing, and used by several applications. Thanks for the info. I will implement it.
Gaul wrote: I cannot understand it when MS takes away features that were already existing, and used by several applications. On the contrary, the functionality is still there; just how you use it is changed. Rather than following the MFC paradigm where you added strings then set related data separately; you just add the data and tell the combobox how to represent it (by setting the
DisplayMember
andValueMember
properties of the combobox, ifDisplayMember
isn't set it defaults to callingToString()
) James "And we are all men; apart from the females." - Colin Davies