C# WINFORM中的combobox.items.add实现像web开发那样,添加显示内容text和实际value值
摘自msdn,詳見http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/c7a82a6a-763e-424b-84e0-496caa9cfb4d/
?public partial class Form1 : Form
{ ??
? // Content item for the combo box ??
? private class Item { ????
? public string Name; ???
?? public int Value; ????
? public Item(string name, int value) { ???
???? Name = name; Value = value; ????
? } ?????
?public override string ToString() { ??
????? // Generates the text shown in the combo box ??????? return Name; ????
? } ?
?? } ??
? public Form1() { ?
???? InitializeComponent(); ???
?? // Put some stuff in the combo box ???
?? comboBox1.Items.Add(new Item("Blue", 1)); ???
?? comboBox1.Items.Add(new Item("Red", 2)); ????
? comboBox1.Items.Add(new Item("Nobugz", 666)); ??
? }
??? private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { ???
?? // Display the Value property ??
??? Item itm = (Item)comboBox1.SelectedItem; ???
?? Console.WriteLine("{0}, {1}", itm.Name, itm.Value); ?
?? }
? }
轉載于:https://www.cnblogs.com/enjoyprogram/archive/2012/04/06/2434170.html
總結
以上是生活随笔為你收集整理的C# WINFORM中的combobox.items.add实现像web开发那样,添加显示内容text和实际value值的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 转存储过程实现分页1
- 下一篇: Timus 1018 树形DP
