combobox1.Items.Add()怎样可以添加文本和值 呢?

解决方案 »

  1.   

    combobox1.Items.Add("男");但是我希望“男”这项对应的值为"1"...
      

  2.   

    DisplayMember 属性 (用于显示)
    ValueMember 属性  (值)
      

  3.   

    尽管 ComboBox 通常用于显示文本项,您仍可向 ComboBox 添加任何对象。通常,ComboBox 中某对象的表示形式为该对象的 ToString 方法返回的字符串。如果希望改为显示该对象的成员,请选择通过将 DisplayMember 属性设置为相应成员的名称来显示的成员。也可以选择该对象的成员,此成员通过设置 ValueMember 属性表示该对象返回的值。
      

  4.   

    凑合看看
     private void button2_Click(object sender, EventArgs e)
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("性别");
                dt.Columns.Add("1/0");
                DataRow dr = dt.NewRow();
                dr["性别"] = "男";
                dr["1/0"] = "1";
                dt.Rows.Add(dr);
                comboBox1.DataSource = dt;
                comboBox1.DisplayMember="性别";
                comboBox1.ValueMember= "1/0";
                comboBox1.SelectedIndexChanged += new EventHandler(comboBox1_SelectedIndexChanged);
            }        void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                //throw new Exception("The method or operation is not implemented.");
                MessageBox.Show(this.comboBox1.SelectedValue.ToString());
            }
      

  5.   

    尽管 ComboBox 通常用于显示文本项,您仍可向 ComboBox 添加任何对象。 using System;
    using System.Windows.Forms;class Form1 : Form
    {
      Form1()
      {
        ComboBox combobox1 = new ComboBox();
        combobox1.Parent   = this;
        combobox1.Items.Add(new Sex(1));   // 值为1,显示为 "男"
        combobox1.Items.Add(new Sex(0));   // 值为0,显示为 "女"
      }
      
      static void Main()
      {
        Application.Run(new Form1());
      }
    }struct Sex
    {
      public int sex;
      
      public Sex(int sex)
      {
        this.sex = sex;
      }
      
      public override string ToString()
      {
        return sex == 0 ? "女" : "男";
      }
    }
      

  6.   

    1、定义一个类Public Class cboTextValueItem
        Private _text As String
        Private _value As Integer    Public Sub New(ByVal text As String, ByVal value As Integer)
            _text = text
            _value = value
        End Sub    Public Property Text() As String
            Get
                Return _text
            End Get
            Set(ByVal value As String)
                _text = value
            End Set
        End Property    Public Property Value() As Integer
            Get
                Return _value
            End Get
            Set(ByVal value As Integer)
                _value = value
            End Set
        End Property    
            '重要
            Public Overrides Function ToString() As String
            Return _text
        End Function

    End Class
    2、填充ComboBoxdim tv as new cboTextValueItem("男",1)
    ComboBox1.items.add(tv)
    3、获取选择项dim tv as cboTextValueItem=trycast(ComboBox1.SelectedItem,cboTextValueItem)if not isnothing(tv)
       '通过tv的Text属性和Value属项来取得选中的项目内容
    endif
      

  7.   

               
    comboBox1.Items.Insert(0, new ComboBoxItem("0","请选择"));
    comboBox1.Items.Insert(1, new ComboBoxItem("1","根目录"));  internal class ComboBoxItem:Object
            {
                private string value;
                private string text;
                public ComboBoxItem(string _value, string _text)
                {
                    value = _value;
                    text = _text;
                }            public string Text
                {
                    get { return text; }
                }            public string Value
                {
                    get { return value; }
                }            public override string ToString()
                {
                    return text;
                }        }
      

  8.   

    using System;
    using System.Windows.Forms;class Form1 : Form
    {
      Form1()
      {
        ComboBox combobox1 = new ComboBox();
        combobox1.Parent   = this;
        combobox1.Items.Add(new Sex(1));   // 值为1,显示为 "男"
        combobox1.Items.Add(new Sex(0));   // 值为0,显示为 "女"
        combobox1.SelectedIndex = 0;
        
        Button button1 = new Button();
        button1.Parent = this;
        button1.Text   = "看看选择了什么";
        button1.Top    = 50;
        button1.Width  = 120;
        button1.Click += delegate
        {
          Sex selected = (Sex)combobox1.SelectedItem;
          MessageBox.Show(string.Format("选择的值是:{0}\r\n显示为:{1}", selected.sex, selected));
        };
      }
      
      static void Main()
      {
        Application.Run(new Form1());
      }
    }struct Sex
    {
      public int sex;
      
      public Sex(int sex)
      {
        this.sex = sex;
      }
      
      public override string ToString()
      {
        return sex == 0 ? "女" : "男";
      }
    }
      

  9.   

    我想加更多的项和值,能不能把显示项和值放到一个二维数组里,然后加载到comboBox中啊???
      

  10.   

                    Dim itm As New DataTable("myList")
                    itm.Columns.Add(New DataColumn("text", GetType(String)))
                    itm.Columns.Add(New DataColumn("value", GetType(Integer)))
                    itm.Rows.Add("a", 1)
                    itm.Rows.Add("b", 2)
                    CombPZ.DisplayMember = "text"
                    CombPZ.ValueMember = "value"
                    CombPZ.DataSource = itm
      

  11.   

    8 楼 guyschaos :
       你的方法绑上去可以,可是如果我有从数据库里读出了性别,在怎么显示出对应的名字啊?
       
         
           
      

  12.   

    comboBox1.Items.Insert(0, new ComboBoxItem("0","请选择"));
    lz应该是想要知道这一句吧,,
      

  13.   

    您好:
    您这个绑定能执行吗??我的代码跟你的差不多啊,但是每次执行绑定操作,就跳到 Indexchange函数中了??
      

  14.   


    我只是把绑定操作放在了 
    form_load 函数中了。
      

  15.   

    string[] wdList = new string[] {"北风","东北偏北风","东北风","东北偏东风","东风","东南偏东风","东南风","东南偏南风","南风",
                    "西南偏南风","西南风","西南偏西风","西风","西北偏西风","西北风","西北偏北风"};
                for (int i = 0; i < wdList.Length; i++)
                {
                    comboBox1.Items.Insert(i, new ComboBoxItem(i + 1, wdList[i]));
                }
    为什么我的程序提示错误:
    错误1 找不到类型或命名空间名称“ComboBoxItem”(是否缺少 using 指令或程序集引用?)
      

  16.   

    而在以前的程序里是没错误的,查MSDN,使用using System.Windeows.Controls;提示错误 1 命名空间“System.Windows”中不存在类型或命名空间名称“Controls”(是缺少程序集引用吗?)
      

  17.   

    纠正一点错误:
    for (int i = 0; i < wdList.Length; i++)
      {
      comboBox1.Items.Insert(i, new ComboBoxItem((i+1).ToString(), wdList[i]));
      }