我有问题,我找不到答案。基本上我有一个ComboBox和一个列表框。我需要的是当我从ComboBox中选择项目时,例如名为“Label”的项目,“Font”,“ForeColor”等项目将出现在Listbox中。我怎么做?我试过这样做:
listBox1.Items.Clear();
if (comboBox1.SelectedIndex == 3)
{
listBox1.Items.Add("Fonts");
}
......但什么也没发生。
答案 0 :(得分:0)
您需要利用ComboBox的SelectedIndexChanged事件。从那里,您将知道在ComboBox中选择了哪个项目,然后将该项目添加到ListBox。
我做对了吗?
答案 1 :(得分:0)
当组合框中的所选项目发生更改时,您希望更改列表框的内容。因此,您需要为组合框添加事件处理程序。您需要处理SelectedIndexChanged
事件。
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
listBox1.Items.Clear();
switch (comboBox1.SelectedIndex)
{
case 0:
// add some items to the list box
break;
case 1:
// add some other items to the list box
break;
// etc. etc.
}
}
这与您问题中的代码非常相似。大概你的代码不起作用,因为它附加到错误的事件处理程序。