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. iOS开发主要参考文档(转载)

    Objective-C,语言的系统详细资料.这是做iOS开发的前题与基础.https://developer.apple.com/library/ios/#documentation/Cocoa/Co ...

  2. 有趣的数-回文数(Palindrome number)

    文章转自http://blog.163.com/hljmdjlln@126/blog/static/5473620620120412525181/ 做LC上的题"Palindrome num ...

  3. win7 通过命令行压缩文件

    1,下载winrar这款软件(可能要正式版) 2,安装完之后在c:\programe files里找到winrar目录,复制winrar目录里的winRar.exe文件到c盘下的windows目录 3 ...

  4. easyui accordion—手风琴格子始终展开和多个格子展开

    来源:http://www.cnblogs.com/tylerdonet/p/3531844.html 始终打开有时候可能会很管用,其实就是一个设置问题.这里就不再介绍引用的资源了,这里只看看html ...

  5. 创建交互式shell脚本对话框

    当你在终端环境下安装新的软件时,你可以经常看到信息对话框弹出,需要你的输入,比如:RHEL/CentOS自带的setup,对话框的类型有密码箱.检查表.菜单等等.他们可以引导你以一种直观的方式输入必要 ...

  6. c++内置函数---7

    原创博客:转载请标明出处:http://www.cnblogs.com/zxouxuewei/ 内置函数 调用函数时需要一定的时间和空间的开销.一般,函数调用包括5部分: 1.程序先执行函 数调用之前 ...

  7. php使用redis存储

    一.Redis扩展模块 # wget https://codeload.github.com/phpredis/phpredis/zip/develop -O phpredis.zip # unzip ...

  8. Codeforces Round #148 (Div. 2)

    A. Two Bags of Potatoes 枚举倍数. B. Easy Tape Programming (待补) C. Not Wool Sequences 考虑前缀异或和. \[answer ...

  9. zookeeper的 目录加密

    import org.apache.zookeeper.CreateMode;import org.apache.zookeeper.WatchedEvent;import org.apache.zo ...

  10. bzoj 1061 志愿者招募(最小费用最大流)

    [Noi2008]志愿者招募 Time Limit: 20 Sec  Memory Limit: 162 MBSubmit: 3792  Solved: 2314[Submit][Status][Di ...