解决combobox没有item的特别tag的问题
由于combobox没有类似datagridview的items下的tag,我用了shortstringdirectory解决,在csdn上提问了一下,发觉牛人的一个另一写法,拓展了我的这个程序。。。
ComboBox没有到具体一个item的Tag属性。
你可以使用一个结构体或对象添加到到Items,以此来扩充Item的属性。这样像Tag属性就容易实现了。
你只需重写结构体或类的ToString方法。比如:
1 struct itemEx
2 {
3 public object Tag;
4 public string Text;
5 public itemEx(object tag, string text)
6 {
7 this.Tag = tag;
8 this.Text = text;
9 }
10
11 public override string ToString()
12 {
13 return this.Text;
14 }
15 }
2 {
3 public object Tag;
4 public string Text;
5 public itemEx(object tag, string text)
6 {
7 this.Tag = tag;
8 this.Text = text;
9 }
10
11 public override string ToString()
12 {
13 return this.Text;
14 }
15 }
1 itemEx item = new itemEx(test, "test");
2 combobox1.Items.Add(item);
2 combobox1.Items.Add(item);
//取值:
1 itemEx item =(itemEx) this.combobox1.SelectedItem;
2 item.Tag;
2 item.Tag;