我有8个ComboBox具有相似的SelectedIndexChanged块存储List的string和combobox。
private void cmbPItem_SelectedIndexChanged(object sender, EventArgs e)
{
List<string> srchtxt = new List<string> { "Item", "Brand", "Part No.", "Description", "Manufacturer", "Car", "Year", "Made In", cmbPItem.Text };
List<ComboBox> cmb = new List<ComboBox> { cmbPBrand, cmbPPart, cmbPDesc, cmbPMan, cmbPCar, cmbPYr, cmbPMade };
cmbValue(srchtxt, cmb);
}
private void cmbPBrand_SelectedIndexChanged(object sender, EventArgs e)
{
List<string> srchtxt = new List<string> { "Brand", "Item", "Part No.", "Description", "Manufacturer", "Car", "Year", "Made In", cmbPBrand.Text };
List<ComboBox> cmb = new List<ComboBox> { cmbPItem, cmbPPart, cmbPDesc, cmbPMan, cmbPCar, cmbPYr, cmbPMade };
cmbValue(srchtxt, cmb);
}
private void cmbPPart_SelectedIndexChanged(object sender, EventArgs e)
{
List<string> srchtxt = new List<string> { "Part No.", "Item", "Brand", "Description", "Manufacturer", "Car", "Year", "Made In", cmbPPart.Text };
List<ComboBox> cmb = new List<ComboBox> { cmbPItem, cmbPBrand, cmbPDesc, cmbPMan, cmbPCar, cmbPYr, cmbPMade };
cmbValue(srchtxt, cmb);
}
这些是我的活动的 3 个示例块,还有 5 个具有相同的块但排列不同。存储List必须按正确的顺序传入cmbValue函数。
现在我使用 if 语句将其更改为单个事件
private void combobox_SelectedIndexChanged(object sender, EventArgs e)
{
if (cmbPItem.SelectedIndex != -1)
{
List<string> srchtxt = new List<string> { "Item", "Brand", "Part No.", "Description", "Manufacturer", "Car", "Year", "Made In", cmbPItem.Text };
List<ComboBox> cmb = new List<ComboBox> { cmbPBrand, cmbPPart, cmbPDesc, cmbPMan, cmbPCar, cmbPYr, cmbPMade };
cmbValue(srchtxt, cmb);
}
有没有办法简化它以使其更整洁,更易于阅读。由于有可能添加更多ComboBox
子衿沉夜
慕姐8265434
相关分类