winform中的ComboBox同时设置text和value的方法
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());
就可以看到输出效果了!!!
楼上正解,我在项目中也用到了。
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的方法的更多相关文章
- C# Winform中的ComboBox控件绑定数据库项目作为列表内容
//初始化院区下拉列表,使用了Oracle数据库中的表项目 try { //string connString = "User=system;Password=manager;Data So ...
- PCB Winform中的WebBrowser扩展拖放(拖拽)功能 实现方法
我们在Winform支持网页通常增加WebBrowser控件实现,相当于内嵌浏览器浏览网页使用, 而此WebBrowser默认情况是文件拖入功能是不支持的, 如何才能支持呢.在这里介绍如何实现方法 一 ...
- iOS-UITextField中给placeholder动态设置颜色的四种方法
思路分析: 0.自定义UITextField 1.设置占位文字的颜色找-->placeholderColor,结果发现UITextField没有提供这个属性 2.在storyboard/xib中 ...
- C# WinForm 中ComboBox数据绑定的问题 (转)
来自:http://blog.sina.com.cn/s/blog_5fb9e26301013wga.html C# WinForm 中ComboBox数据绑定的问题 怎样让WinForm中的Comb ...
- 怎样在winform中上传图片
http://jingyan.baidu.com/article/b7001fe157d6b60e7382dd7f.html 因为WinForm都是运行在本地的,而我们的网站一般都是布署在服务器上,运 ...
- 01、Windows Store APP 设置页面横竖屏的方法
在 windows phone store app 中,判断和设置页面横竖屏的方法,与 silverlight 中的 Page 类 不同,不能直接通过 Page.Orientation 进行设置.而是 ...
- winform中ComboBox实现text和value,使显示和值分开,重写text和value属性
winform的ComboBox中只能赋值text,显示和值是一样的,很多时候不能满足根本需要,熟悉B/S开发的coder最常用的就是text和value分开的,而且web下DropDownList本 ...
- c#(winform)中ComboBox添加Key/Value项、获取选中项、根据Key
WinForm下的ComboBox默认是以多行文本来设定显示列表的, 这通常不符合大家日常的应用, 因为大家日常应用通常是键/值对的形式去绑定它的. 参考了一些网上的例子,最终写了一个辅助类用于方便对 ...
- [C#]WinForm 中 comboBox控件之数据绑定
[C#]WinForm 中 comboBox控件之数据绑定 一.IList 现在我们直接创建一个List集合,然后绑定 IList<string> list = new List<s ...
随机推荐
- java深入探究03
1.Tomcat 我们的浏览器其实就是一个Socket客户端能接受Socket服务端发来的消息 一:市面上的web服务器 javase的规范,包含IO流,线程,集合,socket编程.... WebL ...
- 算法(Algorithms)第4版 练习 1.5.2
0 1 2 3 4 5 6 7 8 9 10 components 9 0 0 1 2 3 4 5 6 7 8 9 components 3 4 0 1 2 4 5 6 7 8 0 8 compone ...
- Sobel导数
Sobel 导数 目标 本文档尝试解答如下问题: 如何使用OpenCV函数 Sobel 对图像求导. 如何使用OpenCV函数 Scharr 更准确地计算 核的导数. 原理 Note 以下内容来自于 ...
- Source not found The source attachment does not contain the source for the file MethodBeforeAdvice.class
- POJ 1504,ZOJ 2001,UVA 713, Adding Reversed Numbers,错误,已找到错误
------------------------------------------------------------ 以此题警告自己: 总结, 1.在数组的使用时,一定别忘了初始化 2.在两种情况 ...
- double 与0比较时有个精度问题,有时需精确到小数点后面几位,例如与>0.0001,而不能与>0比较
double 与0比较时有个精度问题,有时需精确到小数点后面几位,例如与>0.0001,而不能与>0比较 例如杭电1408 盐水的故事 ac #include<iostream> ...
- Python基础-redis模块使用
redis是一个数据库,他的数据全都是存放在内存里面的,redis每秒能支持30w次的读写,存放有两种格式,一种string类型,一种是hash类型 一,操作string类型 r=redis.Redi ...
- BEC listen and translation exercise 44
But over the past 70 years or so, there's been a massive increase in one type of crime which was wha ...
- redis学习--Hashes数据类型
本文转自:http://www.cnblogs.com/stephen-liu74/archive/2012/03/19/2352932.html 一.概述: 我们可以将Redis中的Hashes类型 ...
- Xposed模块开发学习记录
Xposed模块相关API可以参考在线文档: https://api.xposed.info/reference/packages.html 入门教程可以参考: https://github. ...