不错呦!smile@林凯西,确保“准备文件”中的几个文件都有安装,S...您好,看了您这篇帖子觉得很有帮助。但是有个问题想请...我的修改过了怎么还被恶意注册呢 @jjjjiiii 用PJ快9年了,主要是A...PJ3啊,貌似很少有人用PJ了,现在不是WP就是z...@332347365,我当时接入时错误码没有-10...楼主,ChkValue值应为-103是什么意思呢?...大哥 你最近能看到我发的信息,请跟我联系,我有个制...
ComboBox手工添加数据示例
编辑:dnawo 日期:2009-10-12
ComboBox.Items.Add参数是object类型的对象,如果用数据绑定可以很容易地实现Text和Value的分别初始化(见《ComboBox绑定数据源示例》一文),但如果要手工添加数据项,就有点困难了,总结了网上方法,有以下两种:
1.使用KeyValuePair
2.自定义类MyListItem
MyListItem类:
添加数据项并读取示例:
1.使用KeyValuePair
复制内容到剪贴板
程序代码

//引用命名空间System.Collections.Generic
comboBox1.Items.Add(new KeyValuePair<string, string>("aaa", "1"));
comboBox1.Items.Add(new KeyValuePair<string, string>("bbb", "2"));
comboBox1.Items.Add(new KeyValuePair<string, string>("ccc", "3"));
comboBox1.DisplayMember = "key";
comboBox1.ValueMember = "value";
//获取值
MessageBox.Show(((KeyValuePair<string, string>)comboBox1.SelectedItem).Key + "," + ((KeyValuePair<string, string>)comboBox1.SelectedItem).Value);
comboBox1.Items.Add(new KeyValuePair<string, string>("aaa", "1"));
comboBox1.Items.Add(new KeyValuePair<string, string>("bbb", "2"));
comboBox1.Items.Add(new KeyValuePair<string, string>("ccc", "3"));
comboBox1.DisplayMember = "key";
comboBox1.ValueMember = "value";
//获取值
MessageBox.Show(((KeyValuePair<string, string>)comboBox1.SelectedItem).Key + "," + ((KeyValuePair<string, string>)comboBox1.SelectedItem).Value);
2.自定义类MyListItem
MyListItem类:
复制内容到剪贴板
程序代码

public class MyListItem
{
private string _text;
private string _value;
public MyListItem(string text, string value)
{
_text = text;
_value = value;
}
public string Text
{
get { return _text; }
}
public string Value
{
get { return _value; }
}
//必须
public override string ToString()
{
return _text;
}
}
{
private string _text;
private string _value;
public MyListItem(string text, string value)
{
_text = text;
_value = value;
}
public string Text
{
get { return _text; }
}
public string Value
{
get { return _value; }
}
//必须
public override string ToString()
{
return _text;
}
}
添加数据项并读取示例:
复制内容到剪贴板
程序代码

comboBox1.Items.Add(new MyListItem("aaa", "1"));
comboBox1.Items.Add(new MyListItem("bbb", "2"));
comboBox1.Items.Add(new MyListItem("ccc", "3"));
//获取值
MessageBox.Show(((MyListItem)comboBox1.SelectedItem).Text + "," + ((MyListItem)comboBox1.SelectedItem).Value);
comboBox1.Items.Add(new MyListItem("bbb", "2"));
comboBox1.Items.Add(new MyListItem("ccc", "3"));
//获取值
MessageBox.Show(((MyListItem)comboBox1.SelectedItem).Text + "," + ((MyListItem)comboBox1.SelectedItem).Value);






评论: 0 | 引用: 0 | 查看次数: 5540
发表评论
请登录后再发表评论!