zoukankan      html  css  js  c++  java
  • 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);  

       }

      }

  • 相关阅读:
    hero
    今年暑假不AC
    Who's in the Middle
    A Simple Problem with Integers
    I hate it
    敌兵布阵
    Ordering Tasks
    Points on Cycle
    食物链
    c++ 14.0下载地址
  • 原文地址:https://www.cnblogs.com/enjoyprogram/p/2434170.html
Copyright © 2011-2022 走看看