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 ...
随机推荐
- POJ 之 Is the Information Reliable?
B - Is the Information Reliable? Time Limit:3000MS Memory Limit:131072KB 64bit IO Format:%I6 ...
- 51nod 1513 && CF570D
题意:给定一棵树,每个节点有一个字母.给定若干个询问,询问某个子树内某一深度的节点是否能将这些节点组合成一个回文串.(深度是以根节点为基准的,不是当前子树根.)数据规模10^5. 神犇题解 子树问题, ...
- NLP-最小编辑距离
最小编辑距离 一 概念 编辑距离(Edit Distance),又称Levenshtein距离,是指两个字串之间,由一个转成另一个所需的编辑操作次数.最小编辑距离,是指所需最小的编辑操作次数. 编辑操 ...
- Spark- 计算每个学科最受欢迎的老师
日志类型 测试数据 http://bigdata.myit.com/zhangsan http://bigdata.myit.com/zhangsan http://bigdata.myit.com/ ...
- ZSetOperations
有序集合,默认按照score升序排列,存储格式K(1)==V(n),V(1)=S(1)(K=key,V=value,S=score) 1.add(K,V,S):添加 2.count(K,Smin,Sm ...
- C++中的右结合性
看到网上的说是,右结合 但是还是从左往右算 // 以下说法是从网上看的,不知道对不 a ? b : c ? d : e 如何进行呢? 它的结合律是从右向左,所以它等效于 a ? b : ( c ? d ...
- 使用CSS3制作酷炫防苹果复选框 自行测试!
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- PL/SQL学习笔记_03_存储函数与存储过程
ORACLE 提供可以把 PL/SQL 程序存储在数据库中,并可以在任何地方来运行它.这样就叫存储过程或函数. 存储函数:有返回值,创建完成后,通过select function() from dua ...
- php导出内容到txt并自动弹出下载文件
php将内容保存到txt文件中,并自动弹出下载文件窗口的方法: $id=array('我爱学习网http://www.5ixuexiwang.com','汇享在线工具箱http://tool.huix ...
- 百度编辑器UEditor配置toolbars工具条功能按钮
两种方式: 1.代码中定义 <script id="container" name="content" type="text/plain&quo ...