winform中的ComboBox不能像webform中的dropdownlist控件一样,在属性中可以同时设置text和value值,可以通过编写一个新类来实现这个功能。

1、首先在form1中添加一个新类ComboBoxItem:

public class ComboBoxItem
  {
   private string _text=null;
   private object _value=null;
   public string Text{get{return this._text;} set{this._text=value;}}
   public object Value{get {return this._value;} set{this._value=value;}}
   public override string ToString()
   {
    return this._text;
   }
  }

2、在Form1_Load函数中添加:

ComboBoxItem newitem = new ComboBoxItem();
   newitem.Text = "abc";
   newitem.Value = "1";
   comboBox1.Items.Add(newitem);

3、在comboBox1_SelectedIndexChanged中添加:

ComboBoxItem myItem = (ComboBoxItem)comboBox1.Items[comboBox1.SelectedIndex];
    MessageBox.Show(myItem.Value.ToString());

就可以看到输出效果了!!!

楼上正解,我在项目中也用到了。

C# code?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
    /// <summary>
    /// ComboBox的Item
    /// </summary>
    public class TextValue
    {
        public TextValue() { }
 
        public TextValue(string inText, int inValue)
        {
            this.Text = inText;
            this.Value = inValue;
        }
 
        private string _text;
        private int _value;
        /// <summary>
        /// 文本
        /// </summary>
        public string Text
        {
            set this._text = value; }
            get return this._text; }
        }
        /// <summary>
        /// 值
        /// </summary>
        public int Value
        {
            set this._value = value; }
            get return this._value; }
        }
    }
 
 public partial class TempLateWave : Form
{
   List<TextValue> PurposeList = new List<TextValue>();
    public TempLateWave()
    {
        InitializeComponent();
        PurposeList.Add(new TextValue("精度", 0));
        PurposeList.Add(new TextValue("线性度", 1));
        PurposeList.Add(new TextValue("一致性", 2));
        cbx.DataSource = PurposeList;   //cbx 就是ComboBox控件
        cbx.DisplayMember = "Text";
        cbx.ValueMember = "Value";
    }
}

winform中的ComboBox同时设置text和value的方法的更多相关文章

  1. C# Winform中的ComboBox控件绑定数据库项目作为列表内容

    //初始化院区下拉列表,使用了Oracle数据库中的表项目 try { //string connString = "User=system;Password=manager;Data So ...

  2. PCB Winform中的WebBrowser扩展拖放(拖拽)功能 实现方法

    我们在Winform支持网页通常增加WebBrowser控件实现,相当于内嵌浏览器浏览网页使用, 而此WebBrowser默认情况是文件拖入功能是不支持的, 如何才能支持呢.在这里介绍如何实现方法 一 ...

  3. iOS-UITextField中给placeholder动态设置颜色的四种方法

    思路分析: 0.自定义UITextField 1.设置占位文字的颜色找-->placeholderColor,结果发现UITextField没有提供这个属性 2.在storyboard/xib中 ...

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

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

  5. 怎样在winform中上传图片

    http://jingyan.baidu.com/article/b7001fe157d6b60e7382dd7f.html 因为WinForm都是运行在本地的,而我们的网站一般都是布署在服务器上,运 ...

  6. 01、Windows Store APP 设置页面横竖屏的方法

    在 windows phone store app 中,判断和设置页面横竖屏的方法,与 silverlight 中的 Page 类 不同,不能直接通过 Page.Orientation 进行设置.而是 ...

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

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

  8. c#(winform)中ComboBox添加Key/Value项、获取选中项、根据Key

    WinForm下的ComboBox默认是以多行文本来设定显示列表的, 这通常不符合大家日常的应用, 因为大家日常应用通常是键/值对的形式去绑定它的. 参考了一些网上的例子,最终写了一个辅助类用于方便对 ...

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

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

随机推荐

  1. Ansible Ad-Hoc命令集

    Ad-Hoc Ad-Hoc就是 “临时命令”, 从功能上讲 Ad-Hoc跟Ansible-playbook都差不多,Ansible提供了两种完成任务的方式: Ad-Hoc命令集与Ansible-pla ...

  2. poj 2356 Find a multiple【鸽巢原理 模板应用】

    Find a multiple Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6651   Accepted: 2910   ...

  3. springmvc对象映射个数超过256个

    /** 由于Spring在接受前台传入的List时,就会出现256的IndexOutOfBoundsException异常 设置setAutoGrowCollectionLimit为1024 @par ...

  4. Openldap- 大机群身份验证服务

    无论在哪个行业,数据安全永远都是摆在首要地位.尤其是在大数据行业上,谁掌握了数据,谁就有可能成为下个亿万富豪的环境中,数据安全更为重要.大数据的安全可以从哪些地方入手,首先可以在身份验证上面入手.在大 ...

  5. windows下安装配置nginx

    下载nginx-1.0.11.zip, 解压到到nginx目录下 D:\nginx\conf 修改conf下的nginx.conf文件, 默认是80端口,若该端口被占则可以修改 listen 8073 ...

  6. Git_错误_02_error: src refspec master does not match any

    现象:在一个目录下初始化仓库之后,就开始push到github,结果出现了这个错误. 错因:初始化仓库之后,并没有使用git add,git commit 命令将文件添加到git仓库中,所以仓库为空, ...

  7. Java丨jsoup网络爬虫模拟登录思路解析

    直奔主题: 本篇文章是给有jsoup抓包基础的人看的...... 今天小编给大家写一篇对于jsoup抓包时需要输入验证码的解决方法之一.大神就绕道,嘿嘿~ 任何抓包的基础都是基于Http协议来进行这个 ...

  8. BZOJ5314: [Jsoi2018]潜入行动

    BZOJ5314: [Jsoi2018]潜入行动 https://lydsy.com/JudgeOnline/problem.php?id=5314 分析: 裸树形背包,设\(f[x][i][0/1] ...

  9. bzoj 1833: [ZJOI2010]count 数位dp

    题目: 给定两个正整数a和b,求在[a,b]中的所有整数中,每个数码(digit)各出现了多少次. 题解 设\(f[i][j]\)表示长度为\(i\)的所有合法数字中有多少数码\(j\) 设\(g[i ...

  10. GCC泛型宏

    在JAVA和CPP这种OOP语言中,都有泛型类,在C语言可以用宏定义实现泛型函数. main.c #include <stdio.h> #define min(x, y) ({ \ typ ...