C# 如何为ComboBox的项目添加值,以便像这样选择ComboBox。SelectedIndex=myInt?
假设我有以下项目:C# 如何为ComboBox的项目添加值,以便像这样选择ComboBox。SelectedIndex=myInt?,c#,.net,winforms,C#,.net,Winforms,假设我有以下项目: comboBox.Items.Add("Access"); // make it equal to 31 comboBox.Items.Add("Create"); // make it equal to 34 comboBox.Items.Add("Delete"); // make it equal to 36 comboBox.Items.Add("Modify"); // make it equal to
comboBox.Items.Add("Access"); // make it equal to 31
comboBox.Items.Add("Create"); // make it equal to 34
comboBox.Items.Add("Delete"); // make it equal to 36
comboBox.Items.Add("Modify"); // make it equal to 38
现在,我打电话
comboBox.SelectedIndex = 34; // want to "Create" item has been chosen
做这件事最简单的方法是什么?您想使用而不是。索引只是一个计数0,1,2,3。。。。可以指定该值。不幸的是,winforms不像ASP.NET那样具有ListItem类,因此您必须编写自己的:
public class cbxItem
{
public string text {get;set;}
public int id {get;set;}
public override string ToString()
{
return text;
}
// you need this override, else your combobox items are gonna look like
// "Winforms.Form1 + cbxItem"
}
然后将项目添加到组合框中,如下所示:
cbxItem item = new cbxItem();
item.text = "Access";
item.id = 31;
comboBox.Items.Add(item);
public class WorkItem
{
public int Key { get; set; }
public string Value { get; set; }
public WorkItem()
{
}
public override string ToString()
{
return Value;
}
}
要获取id或值或以您希望的方式调用它,请执行以下操作:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
var cbxMember = comboBox1.Items[comboBox1.SelectedIndex] as cbxItem;
if (cbxMember != null) // safety check
{
var value = cbxMember.id;
}
}
这在很大程度上取决于数据的管理方式 如果您的项目在您的程序中不会被修改,您可以简单地使用字典作为映射表
comboBox.Items.Add("Access"); // make it equal to 31
comboBox.Items.Add("Create"); // make it equal to 34
comboBox.Items.Add("Delete"); // make it equal to 36
comboBox.Items.Add("Modify"); // make it equal to 38
Dictionary<int, int> mapTable = new Dictionary<int, int>();
mapTable.Add(31, 0);
mapTable.Add(34, 1);
mapTable.Add(36, 2);
mapTable.Add(38, 3);
您甚至可以将此逻辑放入从ComboBox继承的类中,以获得更好的抽象。您需要添加比简单字符串更复杂的内容来完成所需的操作。如果您只需要一个int和一个关联的字符串,那么您可以使用KeyValuePair,但任何自定义对象都可以使用。然后需要在组合框上设置DisplayMember和ValueMember,以便它正确显示。哦,使用SelectedValue或SelectedMember代替SelectedIndex 这是你的补充:
comboBox.Items.Add(new KeyValuePair<int, string>(){ Key = 34, Value = "Access"});
是的,一个自定义对象会使这个语句更简单,但概念是一样的
comboBox.Items.Add(new WorkItem { Key = 31, Value = "Access" });
comboBox.Items.Add(new WorkItem { Key = 34, Value = "Create" });
comboBox.Items.Add(new WorkItem { Key = 36, Value = "Delete" });
comboBox.Items.Add(new WorkItem { Key = 38, Value = "Modify" });
selectItemByKey(34);
您需要添加此方法:
private void selectItemByKey(int key)
{
foreach (WorkItem item in comboBox.Items)
{
if (item.Key.Equals(key))
comboBox.SelectedItem = item;
}
}`
像这样的课程:
cbxItem item = new cbxItem();
item.text = "Access";
item.id = 31;
comboBox.Items.Add(item);
public class WorkItem
{
public int Key { get; set; }
public string Value { get; set; }
public WorkItem()
{
}
public override string ToString()
{
return Value;
}
}
为什么不通过适当的SelectedIndex获取它们呢?SelectedValue只有在使用数据绑定时才起作用。或者至少,这是过去的情况。你是对的,你需要设置一个数据源使其工作。类似的问题与中的要求不完全相同,但如果我想按值搜索关键字?这样:var=mapTable[1],它将返回34?不,不会。但是,您可以使用每个combox框项的tag属性来存储相应的值。