c# ComboBox改变活动状态

本文关键字:活动状态 改变 ComboBox | 更新日期: 2023-09-27 18:10:22

我有五个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选中的项目。

c# ComboBox改变活动状态

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
}