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);  

   }

  }

你可能感兴趣的:(combobox)