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

   }

  }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM