相忘于江湖

不抛弃,不放弃... 请给我勇敢,改变可以改变的;请给我坚强,接受不可以改变的;请给我智慧,分辨这两者。

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

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

 

posted on 2011-04-26 09:38  playman0211  阅读(988)  评论(0编辑  收藏  举报