• c#(winform)中ComboBox和ListBox添加项和设定预选项完全解决 (转)


    1.

    WinForm下的ComboBox默认是以多行文本来设定显示列表的, 这通常不符合大家日常的应用,

    因为大家日常应用通常是键/值对的形式去绑定它的.

    那么用键值对的形式如何做?

    因为Combox的每一个项的值是一个object, 实际上就是一个键/值对.
    我用的是下面这个类的实例作为它的一个项:

        /// <summary>
        /// ComboBox的项
        /// </summary>
        class ListItem : System.Object
        {
            private string _Value = string.Empty;
            private string _Text = string.Empty;

            /// <summary>
            /// 值
            /// </summary>
            public string Value
            {
                get { return this._Value; }
                set { this._Value=value; }
            }
            /// <summary>
            /// 显示的文本
            /// </summary>
            public string Text
            {
                get { return this._Text; }
                set { this._Text=value; }
            }

            public ListItem(string value, string text)
            {
                this._Value = value;
                this._Text = text;
            }
            public override string ToString()
            {
                return this._Text;
            }

        }

     通过这个类就可以定义ComboBox的值了, 首先我们定义一个ListItem的清单作为ComboBox的数据源:

                List<ListItem> items = new List<ListItem>();
                items.Add(new ListItem("0", "Item_0_Text"));
                items.Add(new ListItem("1", "Item_1_Text"));
                items.Add(new ListItem("2", "Item_2_Text"));
                items.Add(new ListItem("3", "Item_3_Text"));
                items.Add(new ListItem("4", "Item_4_Text"));
                items.Add(new ListItem("5", "Item_5_Text"));
     然后进行相应的设置:

                //将数据源的属性与ComboBox的属性对应
                drpTest.DisplayMember = "Text";        //显示
                drpTest.ValueMember = "Value";        //值
    然后进就可以进行绑定了:

                drpTest.DataSource = items;        //绑定数据
    绑定数据之后, 就可以对其进行默认选择项的设置, 取值等操作:

                drpTest.SelectedValue = "4";        //设定选择项

                //取得当前选择的项
                ListItem selectedItem = (ListItem)drpTest.SelectedItem;
                string value = selectedItem.Value;    //值
                string text = selectedItem.Text;    //显示的文字

    其他操作大家就依样画葫芦吧. 呵呵.

    本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/fcsh820/archive/2009/02/07/3867053.aspx

    2.

    Technorati Tags: winform C#

    在网页中 ,使用起来很方便,但今天在使用C#的时候 ComboBox 居然只能用 value 显示文本,不支持 key 了,往列表中增加大量数据的时候还了得?

    没办法,只能自己动手了,研究了一下 ComboBox ,Add 增加的是 Object ,看来有戏,过程不写了,只放结果,一看就明白:

    自定义 ListItem

    view plaincopy to clipboardprint?

    1. public class ListItem 
    2. public string Key { get; set; } 
    3. public string Value { get; set; } 
    4. public ListItem(string pKey, string pValue) 
    5.     { 
    6. this.Key = pKey; 
    7. this.Value = pValue; 
    8.     } 
    9. public override string ToString() 
    10.     { 
    11. return this.Key; 
    12.     } 

    写入数据的时候:

    view plaincopy to clipboardprint?

    1. ListItem li = new ListItem("名称", "内容"); 
    2. this.combobox1.Items.Add(li); 
    3. // 在此可以增加N个
    4. this.combobox1.Items.Add(li); 
    5. this.combobox1.DisplayMember = "Key";// 对应 ListItem 的 Key
    6. this.combobox1.ValueMember = "Value";// 对应 ListItem 的 Value

    读取数据的时候:

    1. ListItem li = (ListItem)comboBox1.SelectedItem; 
    2. MessageBox.Show(li.Key + li.Value); 

    原文链接:http://mdeve.com/blog/post/204

    ComboBox

  • 相关阅读:
    IDEA安装及破解永久版教程————鹏鹏
    Hive(2)-Hive的安装,使用Mysql替换derby,以及一丢丢基本的HQL
    Hive(1)-基本概念
    ZooKeeper(3)-内部原理
    Hadoop(25)-高可用集群配置,HDFS-HA和YARN-HA
    ZooKeeper(2)-安装和配置
    ZooKeeper(1)-入门
    Hadoop(24)-Hadoop优化
    Hadoop(23)-Yarn资源调度器
    Hadoop(22)-Hadoop数据压缩
  • 原文地址:https://www.cnblogs.com/Godblessyou/p/2028919.html
Copyright © 2020-2023  润新知