SelectedValue  选中的value就是text的值

解决方案 »

  1.   

    我有一个 Dictionary<int, string> 的想让他赋值在CheckedListBox 中每一项items中,就是item显示的是string,也可以获取int的值
    该怎么做?
      

  2.   

    foreach(Dictionary<int,string> dc in mydictionary)
    {
     checklistbox.item.add(dc.key);
    }
      

  3.   

    解决方法,使用隐藏属性,使用实体类更方便!
    完整代码!using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        private void Form1_Load(object sender, EventArgs e)
            {
                List<Person> lst = new List<Person>();
                Person p1 = new Person();
                p1.Id = 1;
                p1.Name = "张三";
                p1.Sex = "先生";
                lst.Add(p1);
                Person p2 = new Person();
                p2.Id = 2;
                p2.Name = "李四";
                p2.Sex = "人妖";
                lst.Add(p2);            //注意:这三个属性为隐藏属性,不会在写代码时提示出来,但能用
                this.checkedListBox1.DataSource = lst;
                this.checkedListBox1.DisplayMember = "Name";
                this.checkedListBox1.ValueMember = "Id";
            }        private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
            {
                Person p = this.checkedListBox1.Items[e.Index] as Person;
                if (p != null)
                {
                    MessageBox.Show("姓名:" + p.Name + " ID:" + p.Id  +" Sex:"+p.Sex);
                }
            }        
        }    public class Person
        {
            public string Name { get; set; }
            public int Id { get; set; }
            public string Sex { get; set; }
        }
    }
      

  4.   

    以上是在VS2008中的,在VS2005中需要对实体类Person重新定义!VS2005中的完整代码using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;namespace WindowsApplication4
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
            {
                Person p = this.checkedListBox1.Items[e.Index] as Person;
                if (p != null)
                {
                    MessageBox.Show("姓名:" + p.Name + " ID:" + p.Id + " Sex:" + p.Sex);
                }
            }        private void Form1_Load(object sender, EventArgs e)
            {
                List<Person> lst = new List<Person>();
                Person p1 = new Person();
                p1.Id = 1;
                p1.Name = "张三";
                p1.Sex = "先生";
                lst.Add(p1);
                Person p2 = new Person();
                p2.Id = 2;
                p2.Name = "李四";
                p2.Sex = "人妖";
                lst.Add(p2);            //注意:这三个属性为隐藏属性,不会在写代码时提示出来,但能用
                this.checkedListBox1.DataSource = lst;
                this.checkedListBox1.DisplayMember = "Name";
                this.checkedListBox1.ValueMember = "Id";
            }
        }    public class Person
        {
            private string m_name;
            public string Name
            {
                get { return m_name; }
                set { m_name = value; }
            }
            private int m_id;
            public int Id
            {
                get { return m_id; }
                set { m_id = value; }
            }
            private string m_sex;
            public string Sex
            {
                get { return m_sex; }
                set { m_sex = value; }
            }
        }
    }