如何使用C#WinForms将单个SelectedIndexChanged事件处理到多个ComboBox?

时间:2018-07-23 14:24:30

标签: c# combobox

我有8个ComboBox,它们具有类似的SelectedIndexChanged块,其中存储着Liststring的{​​{1}}。

combobox

这些是我活动的3个示例块,还有5个具有相同块但排列不同的示例。存储的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); } 必须以正确的顺序传递到List函数中。

现在我使用if语句将其更改为单个事件

cmbValue

有没有一种方法可以简化它,使其更整齐,更易于阅读。由于可以添加更多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); } if (cmbPBrand.SelectedIndex != -1) { 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); } if (cmbPPart.SelectedIndex != -1) { 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); } if (cmbPDesc.SelectedIndex != -1) { List<string> srchtxt = new List<string> { "Description", "Item", "Brand", "Part No.", "Manufacturer", "Car", "Year", "Made In", cmbPDesc.Text }; List<ComboBox> cmb = new List<ComboBox> { cmbPItem, cmbPBrand, cmbPPart, cmbPMan, cmbPCar, cmbPYr, cmbPMade }; cmbValue(srchtxt, cmb); } if (cmbPMan.SelectedIndex != -1) { List<string> srchtxt = new List<string> { "Manufacturer", "Item", "Brand", "Part No.", "Description", "Car", "Year", "Made In", cmbPItem.Text }; List<ComboBox> cmb = new List<ComboBox> { cmbPItem, cmbPBrand, cmbPPart, cmbPDesc, cmbPCar, cmbPYr, cmbPMade }; cmbValue(srchtxt, cmb); } if (cmbPCar.SelectedIndex != -1) { List<string> srchtxt = new List<string> { "Car", "Item", "Brand", "Part No.", "Description", "Manufacturer", "Year", "Made In", cmbPCar.Text }; List<ComboBox> cmb = new List<ComboBox> { cmbPItem, cmbPBrand, cmbPPart, cmbPDesc, cmbPMan, cmbPYr, cmbPMade }; cmbValue(srchtxt, cmb); } if (cmbPYr.SelectedIndex != -1) { List<string> srchtxt = new List<string> { "Year", "Item", "Brand", "Part No.", "Description", "Manufacturer", "Car", "Made In", cmbPYr.Text }; List<ComboBox> cmb = new List<ComboBox> { cmbPItem, cmbPBrand, cmbPPart, cmbPDesc, cmbPMan, cmbPCar, cmbPMade }; cmbValue(srchtxt, cmb); } if (cmbPMade.SelectedIndex != -1) { List<string> srchtxt = new List<string> { "Made In", "Item", "Brand", "Part No.", "Description", "Manufacturer", "Car", "Year", cmbPMade.Text }; List<ComboBox> cmb = new List<ComboBox> { cmbPItem, cmbPBrand, cmbPPart, cmbPDesc, cmbPMan, cmbPCar, cmbPYr }; cmbValue(srchtxt, cmb); } }

2 个答案:

答案 0 :(得分:0)

您可以使用 /* unvisited link */ a:link { color: red; } ​ /* visited link */ a: visited { color: green; } ​ /* mouse over link */ a:hover { color: pink; } ​ /* selected link */ a:active { color: blue; } 对象来确定哪个组合框触发了事件。另外,您可以使用元组和LINQ进行很多清理:

sender

答案 1 :(得分:0)

我会将所有组合框放在一个列表中,并将其存储在from的成员变量中。 然后,您可以将srchtxt中的名称放入comboboxes Tag属性。 然后循环遍历所有组合框,并使用new ComboBox[] { current }.Union(comboboxes.Where(x => x != current)).Select(x => x.Tag as string).Union(new string [] { current.Text }).ToList()生成srchtxt,您将获得一个列表,该列表看起来与您在帖子中使用硬编码创建的列表非常相似。