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

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

参考了一些网上的例子,最终写了一个辅助类用于方便对ComboBox的操作:

用下面这个类的实例作为ComboBox的添加项:

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms; namespace tp7309.Winform
{
public class ListItem
{
public string Key { get; set; }
public string Value { get; set; } public ListItem(string strKey, string strValue)
{
this.Key = strKey;
this.Value = strValue;
}
public override string ToString()
{
return this.Key;
} /// <summary>
/// 根据ListItem中的Value找到特定的ListItem(仅在ComboBox的Item都为ListItem时有效)
/// </summary>
/// <param name="cmb">要查找的ComboBox</param>
/// <param name="strValue">要查找ListItem的Value</param>
/// <returns>返回传入的ComboBox中符合条件的第一个ListItem,如果没有找到则返回null.</returns>
public static ListItem FindByValue(ComboBox cmb, string strValue)
{
foreach (ListItem li in cmb.Items)
{
if (li.Value == strValue)
{
return li;
}
}
return null;
} /// <summary>
/// 根据ListItem中的Key找到特定的ListItem(仅在ComboBox的Item都为ListItem时有效)
/// </summary>
/// <param name="cmb">要查找的ComboBox</param>
/// <param name="strValue">要查找ListItem的Key</param>
/// <returns>返回传入的ComboBox中符合条件的第一个ListItem,如果没有找到则返回null.</returns>
public static ListItem FindByText(ComboBox cmb, string strText)
{
foreach (ListItem li in cmb.Items)
{
if (li.Value == strText)
{
return li;
}
}
return null;
}
}
}

使用前引入命名空间:tp7309.Winform

  添加项:  

cmb1.Items.Add(new ListItem("key1", "value1"));
cmb1.Items.Add(new ListItem("key2", "value2"));

获取选中项:

ListItem li = (ListItem)cmb1.SelectedItem;
ListItem li1 = ListItem.FindByValue(cmb1, "value1");   //根据Key得到选中项
ListItem li2 = ListItem.FindByText(cmb1, "key1");      //根据Value得到选中项
string strKey = li.Key;   //得到选中项Key
string strValue = li.Value;   //得到选中项Value

设置选中项:

cmb1.SelectedIndex = 0;    //根据索引修改选中项
cmb1.SelectedItem = ListItem.FindByValue(cmb1, "value1");   //根据Key得到选中项
cmb1.SelectedItem = ListItem.FindByText(cmb1, "key1");      //根据Value得到选中项

c#(winform)中ComboBox添加Key/Value项、获取选中项、根据Key的更多相关文章

  1. C# WinForm 中ComboBox数据绑定的问题 (转)

    来自:http://blog.sina.com.cn/s/blog_5fb9e26301013wga.html C# WinForm 中ComboBox数据绑定的问题 怎样让WinForm中的Comb ...

  2. [C#]WinForm 中 comboBox控件之数据绑定

    [C#]WinForm 中 comboBox控件之数据绑定 一.IList 现在我们直接创建一个List集合,然后绑定 IList<string> list = new List<s ...

  3. winform中ComboBox实现text和value,使显示和值分开,重写text和value属性

    winform的ComboBox中只能赋值text,显示和值是一样的,很多时候不能满足根本需要,熟悉B/S开发的coder最常用的就是text和value分开的,而且web下DropDownList本 ...

  4. silverlight中 ComboBox绑定数据库,并获取当前选定值

    silverlight中 ComboBox绑定数据库,并获取当前选定值 在silverlight中 用combobox下拉菜单绑定数据库的方法和用DataGrid绑定数据库的方法类似. page.xa ...

  5. winform中ComboBox控件的简单使用

    在开发winform中用到了ComboBox,但是发现和asp.net中的DropDownList差别比我想象中的大. 给ComboBox添加数据总结的有两种方法(绑定数据库在这里不说): 第一种方法 ...

  6. Winform中自定义添加ZedGraph右键实现设置所有Y轴刻度的上下限

    场景 Winforn中实现ZedGraph自定义添加右键菜单项(附源码下载): https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/10 ...

  7. winform中DataGridView添加ComboBox的最终解决方案(点击ComboBox默认显示当前行的内容)

    第一: 数据绑定ComBoBox控件 先在窗体设计时拖一个ComBoBox控件,然后在里面的ITEMS设好你要下拉项,或者从数据库中的表绑定,这个估计都会. 第二: // 将下拉列表框加入到DataG ...

  8. winform中comboBox控件加默认选项的问题

    winform程序设计中,label,TextBox,ComboBox等几个控件几乎是用得最多的,在设计中经常会遇到一些小问题,如:comboBox控件绑定了数据源之后,如何设置默认值? combob ...

  9. WinForm 中ComboBox 绑定总结

    1.DataTable绑定 用DataTable直接绑定,只需要设置DataSource.DisplayMember.ValueMember三个属性即可. this.cmbConsume.DataSo ...

随机推荐

  1. fn

    var avalon = function (el) { return new avalon.init(el) } avalon.init = function (el) { this[0] = th ...

  2. MyBatis对应的xml的数据类型

    MyBatis对应的xml的数据类型 JDBC Type Java TypeCHAR StringVARCHAR StringLONGVARCHAR StringNUMERIC java.math.B ...

  3. dedecms list 实现noflag

    转自:http://blog.sina.com.cn/s/blog_7e53dd2b0101l3kq.html 替换include下arc.listview.class.php即可 经测试可行 但在更 ...

  4. WinEdt和TeXworks编辑LaTeX文件乱码问题

    WinEdt默认使用的是系统编码,windows下可以认为是 GBK编码,而TeXworks默认使用的是UTF8编码,所以要统一这两个编码,才能保证两个文件互相打开不会乱码. 具体方法如下: 一,可以 ...

  5. C# string[,]与string[][]的区别

    对于这两者的区别: 1.入门:string[,]可读可写,而string[][]与string[]相同,不可对第二位进行写操作 static void Main(string[] args) { // ...

  6. Maven构建web项目在Eclipse中部署的几种方法

    目录: 方法一:运用Maven的plugin:jetty来部署web 方法二:运用Eclipse 的Jetty插件直接部署 方法三:运用Run on Server(tomcat)部署 [方法一].运用 ...

  7. ueditor上传图片到七牛云存储(form api,java)

    转:http://my.oschina.net/duoduo3369/blog/174655 ueditor上传图片到七牛云存储 ueditor结合七牛传图片 七牛的试炼 开发前的准备与注意事项说明 ...

  8. boot/setup.S

    !!    setup.S        Copyright (C) 1991, 1992 Linus Torvalds!! setup.s is responsible for getting th ...

  9. HDU 4497 数论+组合数学

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4497 解题思路:将满足条件的一组x,z,y都除以G,得到x‘,y',z',满足条件gcd(x',y' ...

  10. UVa 839 天平

    原题链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...