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 ...
随机推荐
- vim打开多个文件方式及操作
格式如下: #vim file*.txt 或者 #vim file file2 file3 查看当前编程的是那个文件,在冒号命令行下 :args 命令,类似:file [file2],以中括号里面为当 ...
- Hive- 表
在hive中表的类型:管理表和托管表(外部表). 内部表也称之为MANAGER_TABLE,默认存储在/user/hive/warehouse下,也可以通过location指定:删除表时,会删除表的数 ...
- 在元素标签中写简单的js函数
如: <img id="verifyImg" class="passcode" src="__URL__/verify_c" onCl ...
- php设计模式课程---2、为什么会用到简单工厂设计模式
php设计模式课程---2.为什么会用到简单工厂设计模式 一.总结 一句话总结: 比如调用数据库的语句,如果调用的数据库名字改了,或者调用的数据库类型改了(比如从Mysql用到了Mysqli),那么要 ...
- 常用的MySQL语句写法
常用的MySQL语句写法 MySQL的SQL语句写法,除了那些基本的之外,还有一些也算比较常用的,这里记录下来,以便以后查找. 好记性不如烂笔头,这话说的太有道理了,一段时间不写它,还真容易忘 ...
- linux-常用指令2
命令链接符 ...
- Java企业微信开发_Exception_01_"errcode":60011,"errmsg":"no privilege to access/modify contact/party/agent "
微信企业号增加成员时,返回错误信息: jsonObject:{"errcode":60011,"errmsg":"no privilege to ac ...
- JdbcUtils针对事务问题作出的第三次修改
DAO中的事务 其实在DAO中处理事务真的是“小菜一碟” try{ con.commit(); }catch(Exception e){ con.rollback(); } 但是dao层中只能是对账户 ...
- (转)gcov、lcov与genhtml 使用心得
gcc是linux平台下的C.C++ 编译器 gcov是配合gcc产生覆盖信息报告的工具: lcov是将gcov产生的报告信息,以更直观的方式显示出来工具 基本的使用方法分为4个阶段: (一).gcc ...
- codeforces 653B B. Bear and Compressing(dfs)
题目链接: B. Bear and Compressing time limit per test 2 seconds memory limit per test 256 megabytes inpu ...