Adding tag or euivenlt to combobox item?
-
Hi Is it possible to to add Tag or data of some kind to a combobox item? no the whole combobox a single Item inside . Thanks
Have a nice Day
Hi, ComboBox (and ListBox) collect items of any type you choose, not just string. So you can create a special item class to hold whatever you want, and add instances of that class to your ComboBox. Now you should use ComboBox.DrawMode=OwnerDrawFixed and do the item painting yourself using the ComboBox.DrawItem event. :)
Luc Pattyn try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }
-
Hi, ComboBox (and ListBox) collect items of any type you choose, not just string. So you can create a special item class to hold whatever you want, and add instances of that class to your ComboBox. Now you should use ComboBox.DrawMode=OwnerDrawFixed and do the item painting yourself using the ComboBox.DrawItem event. :)
Luc Pattyn try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }
-
Hi Is it possible to to add Tag or data of some kind to a combobox item? no the whole combobox a single Item inside . Thanks
Have a nice Day
Doesn't a combo box have a display item and data item property ? Or is that just a combo box in a web page ?
Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
-
Doesn't a combo box have a display item and data item property ? Or is that just a combo box in a web page ?
Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
-
Good news, you dont need OwnerDraw, it suffices to provide a ToString() method. Example:
// the item class:
class cbi {
public int i;
public string text;
public cbi(int i, string text) { this.i=i; this.text=text; }
public override string ToString() { return text+" "+i; }
}// inside the Form class:
public Form1() {
InitializeComponent();
cb.Items.Add(new cbi(1, "a"));
cb.Items.Add(new cbi(2, "b"));
cb.Items.Add(new cbi(3, "c"));
cb.SelectedIndexChanged+=new EventHandler(cb_SelectedIndexChanged);
}void log(string s) {Console.WriteLine(s);}
void cb_SelectedIndexChanged(object sender, EventArgs e) {
cbi item=(cbi)cb.SelectedItem;
// now you can access any member/property of item:
if(item==null) log("item=null");
else log("item i="+item.i);
}:)
Luc Pattyn try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }
-
Hi, this is an example of the technique Christian refered to:
class cbi {
private int val; public int MyVal { get { return val; } }
private string name; public string MyName { get { return name; } }public cbi(int i, string text) { this.val=i; this.name=text; }
}
public Form123() {
InitializeComponent();
ArrayList list=new ArrayList();
list.Add(new cbi(1, "a"));
list.Add(new cbi(2, "b"));
list.Add(new cbi(3, "c"));
cb.DataSource=list;
cb.DisplayMember="MyName";
cb.ValueMember="MyVal";
cb.SelectedIndexChanged+=new EventHandler(cb_SelectedIndexChanged);
}void log(string s) {Console.WriteLine(s);}
void cb_SelectedIndexChanged(object sender, EventArgs e) {
cbi item=(cbi)cb.SelectedItem;
if(item==null) log("null");
else {
int i=(int)cb.SelectedValue;
log("i="+i);
}
}It uses an ArrayList (or any other collection) as the source of all ComboBox data; each item must now have two properties, one giving the text to display, one giving the value to be returned by ComboBox.SelectedValue :)
Luc Pattyn try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }
-
Hi, this is an example of the technique Christian refered to:
class cbi {
private int val; public int MyVal { get { return val; } }
private string name; public string MyName { get { return name; } }public cbi(int i, string text) { this.val=i; this.name=text; }
}
public Form123() {
InitializeComponent();
ArrayList list=new ArrayList();
list.Add(new cbi(1, "a"));
list.Add(new cbi(2, "b"));
list.Add(new cbi(3, "c"));
cb.DataSource=list;
cb.DisplayMember="MyName";
cb.ValueMember="MyVal";
cb.SelectedIndexChanged+=new EventHandler(cb_SelectedIndexChanged);
}void log(string s) {Console.WriteLine(s);}
void cb_SelectedIndexChanged(object sender, EventArgs e) {
cbi item=(cbi)cb.SelectedItem;
if(item==null) log("null");
else {
int i=(int)cb.SelectedValue;
log("i="+i);
}
}It uses an ArrayList (or any other collection) as the source of all ComboBox data; each item must now have two properties, one giving the text to display, one giving the value to be returned by ComboBox.SelectedValue :)
Luc Pattyn try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }
-
You're welcome.
Luc Pattyn try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }