vs2008的combox怎么设置value值?

孙月魂 2012-10-26 03:08:39
vs2008里面的combox怎么设置和text对应的value值?找不到有设置的选项啊!
...全文
486 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
sdhsfhgfh 2014-05-27
  • 打赏
  • 举报
回复
孙月魂 2012-10-29
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 的回复:]

onary<string, string>test = new Dictionary<string, string>();
test.Add("1", "A");
test.Add("2", "B");
test.Add("3", "C");
comboBox1.DataSource = new BindingSource(……
[/Quote]
如梦兄又回来了啊
古兰色回忆 2012-10-26
  • 打赏
  • 举报
回复
或者不使用绑定的形式,直接添加

private List<TextValue> list = new List<TextValue>();

/// <summary>
/// 为下拉列表绑定数据
/// </summary>
private void BindComboBox()
{
list.Add(new TextValue("精度", 0));
list.Add(new TextValue("线性度", 1));
list.Add(new TextValue("一致性", 2));
for (int i = 0; i < list.Count; i++)
{
cbxPurpose.Items.Add(list[i]);
}
cbxPurpose.DisplayMember = "Text";
cbxPurpose.ValueMember = "Value";
}
wuyq11 2012-10-26
  • 打赏
  • 举报
回复
onary<string, string>test = new Dictionary<string, string>();
test.Add("1", "A");
test.Add("2", "B");
test.Add("3", "C");
comboBox1.DataSource = new BindingSource(test, null);
comboBox1.DisplayMember = "Value";
comboBox1.ValueMember = "Key";

// string value = ((KeyValuePair<string, string>)comboBox1.SelectedItem).Value;
EmilyOu 2012-10-26
  • 打赏
  • 举报
回复

public class ValueObj
{
public string Text { get; set; }
public string Value { get; set; }

public override string ToString()
{
return this.Text;
}
}


comboBox1.Items.Add(new ValueObj { Text = "AAA", Value = "aaa" });
comboBox1.Items.Add(new ValueObj { Text = "BBB", Value = "bbb" });



private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
dynamic obj = comboBox1.SelectedItem;
MessageBox.Show(obj.Value);
}
古兰色回忆 2012-10-26
  • 打赏
  • 举报
回复
我这个就是直接写在程序里面的啊。并没有读取数据库。如果你需要动态的添加数据也很简单的。你在放两个控件,分别输入text和value,然后点击确定,为PurposeList这个泛型列表再添加一个项,然后重新再给cbx绑定一次就可以了。
孙月魂 2012-10-26
  • 打赏
  • 举报
回复
我再详细的说一下,我的是winform,不需要从数据库取,直接在程序里设置
古兰色回忆 2012-10-26
  • 打赏
  • 举报
回复
楼上正解,我在项目中也用到了。

/// <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";
}
}
宝_爸 2012-10-26
  • 打赏
  • 举报
回复
combobox部分text和value.每个Item都是一个Object.显示时调用obj.ToString().
你可以自定义类,想放什么放什么。只要重载ToString即可。

参考:

http://blog.csdn.net/orientalcherry/article/details/528525

110,579

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧