我正在创建一个程序,当用户从组合框中选择了某些东西时,它会更新列表框,然后在用户选择列表框中提供的选项之一后,旁边的标签它几乎会显示所选项目的描述。
到目前为止,我想出了这段代码:
public MainHub()
{
InitializeComponent();
listbox1.DataSource = choices;
listbox1.DisplayMember = name;
}
private void listbox1_SelectedIndexChanged(object sender, EventArgs e)
{
description_label.Text = valikud.ElementAt(listbox1.SelectedIndex).description;
}
List<dynamic> choices = new List<dynamic>()
{
new
{
name = "Name1",
desciption = "Description1"
},
new
{
name = "Name2",
desciption = "Description2"
}
};
}
}
这只是在列表框中添加名称并附上说明。
如何在组合框中选择项目时,列表框将更新,添加声明的项目,然后声明的列表框项目将各自有自己的描述?
我尝试使用if
语句执行此操作,但没有运气
-Thanks!
答案 0 :(得分:1)
以下是我认为可以做到的。我没有测试过,因为我现在不能这样做,而且我是从头脑中写下来的。我会在下午进行测试,但在此之前,如果我做错了,你可以自己弄清楚(也是评论,我会给出答案,直到我测试它)
所以这是代码:
public class MainHub
{
List<List_Category> category = new List<List_Category>();
List<Items> items = new List<Items>();
public MainHub()
{
InitializeComponent();
category.Add(new List_Category { CategoryIndex = 0, CategoryName = "Swords" }); // Add some category
category.Add(new List_Category { CategoryIndex = 1, CategoryName = "Shields" }); // Add some category
items.Add(new Items { CategoryIndex = 0, ItemName = "Long sword", ItemDesc = "Long sword is heavy sword with pure force" }); //Add some item to swords
items.Add(new Items { CategoryIndex = 0, ItemName = "Short sword", ItemDesc = "Short sword is easy sword for close range fight" }); //Add some item to swords
items.Add(new Items { CategoryIndex = 0, ItemName = "Double swords", ItemDesc = "Double swords are swords with hight mobility" }); //Add some item to swords
items.Add(new Items { CategoryIndex = 1, ItemName = "Light shield", ItemDesc = "Light shield desc" }); //Add some item to shields
items.Add(new Items { CategoryIndex = 1, ItemName = "Heavy shield", ItemDesc = "Heavy shield desc" }); //Add some item to shields
comboBox1.DisplayMember = "CategoryName";
comboBox1.ValueMember = "CategoryIndex";
comboBox1.DataSource = category;
listBox1.DisplayMember = "ItemName";
listBox1.ValueMember = "CategoryIndex";
comboBox1.SelectedIndex = 0;
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
List<Items> tempItems = new List<Items>();
switch (Convert.ToInt32(comboBox1.SelectedValue))
{
case 0:
var item = items.Where(cat => cat.CategoryIndex == 0);
foreach (var i in item)
{
tempItems.Add(new Items { CategoryIndex = i.CategoryIndex, ItemDesc = i.ItemDesc, ItemName = i.ItemName });
}
break;
case 1:
item = items.Where(cat => cat.CategoryIndex == 1);
foreach (var i in item)
{
tempItems.Add(new Items { CategoryIndex = i.CategoryIndex, ItemDesc = i.ItemDesc, ItemName = i.ItemName });
}
break;
default:
break;
}
listBox1.DataSource = tempItems;
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
var asd = listBox1.SelectedItem as Items;
if (asd != null)
{
name_Label.Text = asd.ItemName;
description_label.Text = asd.ItemDesc;
}
}
}
public class List_Category
{
public int CategoryIndex { get; set; }
public String CategoryName { get; set; }
}
public class Items
{
public int CategoryIndex { get; set; }
public String ItemName { get; set; }
public String ItemDesc { get; set; }
}
它可以被优化,但正如我所说我现在不能这样做所以自己尝试一下。如果我犯了一些错误或弹出一些错误,请发表评论,然后我会回答。