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. Sed Regular Expression

    Today I also used Sed to do some relatively complex job. So I used regular expression. However, the ...

  2. Python4周 入职培训的过程

    这是我在过去几家公司招聘到工程师,Python入职培训的过程.时间分为4周,全部自学,仅提供大纲.适用于Web方向:1.Week1:读完<简明Python教程>,适应Python开发环境2 ...

  3. 三步搞定ISO/GHO安装系统 - imsoft.cnblogs

    高清互动安装系统附件:重装系统视频教程.7z

  4. Windows上的文件合并命令

    从Linux转到Windowns后,发现很多好用的shell命令都没有了,但实际情况是Windows一样有DOS时代的命令窗口,在CLI年代用DOS的人也要干活. 比如,今天想将几个单独的sql文件整 ...

  5. UI学习笔记---第一天

    一.iOS概述 iOS是Apple公司的移动操作系统,主要⽤用于iPhone.iPad.iPad Mini.iPod Touch等移动产品. 借助iOS,我们可以开发视频类.美图类.新闻类.⾳乐类.团 ...

  6. CentOS云服务器数据盘分区和格式化

    1. 查看数据盘信息 登录CentOS云服务器后,可以使用“fdisk -l”命令查看数据盘相关信息. 使用“df –h”命令,无法看到未分区和格式化的数据盘,只能看到已挂载的. [root@VM_7 ...

  7. 137. Single Number II

    Given an array of integers, every element appears three times except for one. Find that single one. ...

  8. openjudge 大师兄,师傅被妖怪抓走啦

    描述 孙悟空听到沙僧大喊一句:“大师兄,师傅被妖怪抓走啦!”于是孙悟空直追白骨精而去.孙悟空在一条长度为L的森林小路上飞奔,上面有L+1个整点,依次为0,1,2……L.白骨精会使用一种大范围的攻击法术 ...

  9. iOS字符串拆分

    NSString* poStr = @"local-8009-1408082245-8.wav"; NSArray* poArr1 = [naviAddressAndPort co ...

  10. Html与CSS布局技巧

    一.单列布局 1.水平居中:(注:下面各个实例中实现的是child元素的对齐操作,child元素的父容器是parend元素) 1-1:使用inline-block和text-align实现: .par ...