为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

C#
炎炎设计 2021-10-24 14:02:10
我有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
查看完整描述

2 回答

?
子衿沉夜

TA贡献1828条经验 获得超3个赞

您可以使用该sender对象来确定哪个组合框触发了该事件。此外,您可以使用元组和 LINQ 来清理它:


private void combobox_SelectedIndexChanged(object sender, EventArgs e)

{

    List<(ComboBox Cmb, string Text)> items = new List<(ComboBox, string)

    {

        (cmpItem, "Item"), (cmbPBrand, "Brand"), (cmbPPart, "Part No."),

        (cmbPDesc, "Description"), (cmbPMan, "Manufacturer"), (cmbPCar, "Car"),

        (cmbPYr, "Year"), (cmbPMade, "Made In")

    };


    List<string> srchtxt = (new[] { items.First(i => i.Cmb == sender).Text })

        .Concat(items.Where(i => i.Cmb != sender).Select(i => i.Text))

        .ToList();


    List<ComboBox> cmb = items

        .Where(i => i.Cmb != sender)

        .Select(i => i.Cmb)

        .ToList();


    cmbValue(srchtxt, cmb);

}


查看完整回答
反对 回复 2021-10-24
?
慕姐8265434

TA贡献1813条经验 获得超2个赞

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


查看完整回答
反对 回复 2021-10-24
  • 2 回答
  • 0 关注
  • 174 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信