C#ComboBox更改活动状态

时间:2015-08-30 17:49:15

标签: c# winforms combobox

我有五个ComboBox控件,当在ComboBox 1中选择一个项目时,会在ComboBox 2中创建一个新列表,依此类推下至ComboBox 5。我需要根据最新选择的项目更改活动状态。这些项目不是通过SelectedIndexChanged事件选择的,而是通过按钮单击事件处理程序选择的。我一直在研究一些代码,但已陷入僵局。这是代码:

///////////combo1//////////////////////
int selectedIndex1 = comboBox1.SelectedIndex;
Object selectedItem1 = comboBox1.SelectedItem;                                
if (selectedItem1.ToString() == "Computers")
{    
    selectedItem1 = "32";
    request.categoryId = selectedItem1.ToString();    
}

///////////combo2//////////////////////
int selectedIndex2 = comboBox2.SelectedIndex;
Object selectedItem2 = comboBox2.SelectedItem;            
if (selectedItem2.ToString() == "Laptop")
{
    selectedItem2= "1772";
    request.categoryId = selectedItem2.ToString();
}

问题在于,当我执行此操作时,它不会使用ComboBox 2所选项目,也不会取消ComboBox 1所选项目。我需要它取代之前的ComboBox所选项目。

1 个答案:

答案 0 :(得分:1)

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
 Object selectedItem1 = comboBox1.SelectedItem;
 comboBox2.Enabled = true;
 comboBox3.Enabled = false;
 comboBox4.Enabled = false;
 comboBox5.Enabled = false;

 if(selectedItem1.ToString() == "Computers")
 {
   //update data source for comboBox2 with the relevant data for "Computers"
 }

 //remove data for comboBox3 4 and 5 witch are now disabled
}

重复这个想法。