winForm ComboBox

1:winForm ComboBox 控件默认值绑定及只可选择不可输入设定处理

最经需要开发一个winForm应用程序,里面用到了ComboBox 空间,首先遇到的问题是不知道如何绑定Text和Value到ComboBox 控件;其次是绑定到ComboBox控件上的内容居然可以修改,让人茫然;最后就是ComboBox选择的值不能够在代码中取得,郁闷啊。经过在网上资料的收集整理先将问题整理如下:

1、ComboBox中同时绑定Text和Value

    首先要定义一个简单数据存放的类,定义如下:

  1. public class MyItem  
  2.         {  
  3.             public MyItem(string Text, string Value)  
  4.             {  
  5.                 this.Text = Text;  
  6.                 this.Value = Value;  
  7.             }  
  8.             public MyItem() {  
  9.                 this.Text = "http://www.my400800.cn";  
  10.                 this.Value = "http://blog.my400800.cn";  
  11.             }  
  12.             private string _Text;  
  13.             public string Text  
  14.             {  
  15.                 get { return _Text; }  
  16.                 set { _Text = value; }  
  17.             }  
  18.             private string _Value;  
  19.             public string Value  
  20.             {  
  21.                 get { return _Value; }  
  22.                 set { _Value = value; }  
  23.             }  
  24.             public override bool Equals(System.Object obj)  
  25.             {  
  26.                 if (this.GetType().Equals(obj.GetType()))  
  27.                 {  
  28.                     MyItem that = (MyItem)obj;  
  29.                     return (this.Text.Equals(that.Value));  
  30.                 }  
  31.                 return false;  
  32.             }  
  33.             public override int GetHashCode()  
  34.             {  
  35.                 return this.Value.GetHashCode(); ;  
  36.             }  
  37.         }  

   ComboBox控件数据添加代码如下:

           其中 cbl是ComboBox控件的一个实例:

         

  1. cbl.Items.Clear();  
  2.             cbl.DisplayMember = "Text";  
  3.             cbl.ValueMember = "Value";  
  4.             cbl.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;  
  5.             List<MyItem> items = new List<MyItem>();  
  6.             MyItem item = new MyItem();  
  7.             item.Text = "男";  
  8.             item.Value = "1";  
  9.             items.Add(item);  
  10.             item = new MyItem();  
  11.             item.Text = "女";  
  12.             item.Value = "2";  
  13.            items.Add(item);  
  14.             item = new MyItem();  
  15.             item.Text = "未知";  
  16.             item.Value = "3";  
  17.             items.Add(item);  
  18.             //将数据源的属性与ComboBox的属性对应   
  19.             cbl.DisplayMember = "Text";        //显示   
  20.             cbl.ValueMember = "Value";        //值   
  21.             cbl.DataSource = items;  
  22.    
  23.             cbl.SelectedValue = "3";  

2、ComboBox控件上的内容不可修改设定方法

  1. cbl.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;  

3、ComboBox选择取得

 

  1. combox_Department.SelectedValue  

在上面的数据绑定大ComboBox是不要用下面的方式,不然的话用cbl.SelectedValue取不到值。

           MyItem item = new MyItem();           

           item.Text = "男";
            item.Value = "1";
            items.Add(item);
            item = new MyItem();

           cbl.Items.Add(items);

转载:http://blog.csdn.net/ljl_xyf/article/details/4551687

public class TestInfo
  {
  string _strName;
  object _objValue;
  public string GetName
  {
  get { return _strName; }
  set { this._strName = value; }
  }

问题:
http://topic.csdn.net/u/20100709/09/6576e409-3402-4a58-ab08-ea96a8c4ef9b.html
先定义这个类
然后
Dictionary<string,string> d = new Dictionary<string,string>();
d.add("HHAA000000AAAC00","人事課");//后面一样的加
List<TestInfo> t = new List<TestInfo>();
最后
Foreach(KeyValuePare<string,string> s in d)
{
  TestInfo ti = new TestInfo();//就是上面的那个类
  ti.GetName = s.Value;
  ti.GetValue = s.Key;
  t.add(ti);
}

this.comboBox1.DataSource = t;
this.comboBox1.ValueMember = "GetValue";
this.comboBox1.DisplayMember = "GetName";


最后 ,如果你要得到对应的编码的话
this.ComboBox1.SelectedValue就能得到了

List<KeyValuePair<object, string>> dit = new List<KeyValuePair<object, string>>();
dit.Add(new KeyValuePair<object, string>(1, "厦门"));
 dit.Add(new KeyValuePair<object, string>(2, "上海"));
   
  this.comboBox1.DataSource = dit;
  this.comboBox1.DisplayMember = "Value";
  this.comboBox1.ValueMember = "Key";

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值