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

我有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


炎炎设计
浏览 175回答 2
2回答

子衿沉夜

您可以使用该sender对象来确定哪个组合框触发了该事件。此外,您可以使用元组和 LINQ 来清理它:private void combobox_SelectedIndexChanged(object sender, EventArgs e){&nbsp; &nbsp; List<(ComboBox Cmb, string Text)> items = new List<(ComboBox, string)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; (cmpItem, "Item"), (cmbPBrand, "Brand"), (cmbPPart, "Part No."),&nbsp; &nbsp; &nbsp; &nbsp; (cmbPDesc, "Description"), (cmbPMan, "Manufacturer"), (cmbPCar, "Car"),&nbsp; &nbsp; &nbsp; &nbsp; (cmbPYr, "Year"), (cmbPMade, "Made In")&nbsp; &nbsp; };&nbsp; &nbsp; List<string> srchtxt = (new[] { items.First(i => i.Cmb == sender).Text })&nbsp; &nbsp; &nbsp; &nbsp; .Concat(items.Where(i => i.Cmb != sender).Select(i => i.Text))&nbsp; &nbsp; &nbsp; &nbsp; .ToList();&nbsp; &nbsp; List<ComboBox> cmb = items&nbsp; &nbsp; &nbsp; &nbsp; .Where(i => i.Cmb != sender)&nbsp; &nbsp; &nbsp; &nbsp; .Select(i => i.Cmb)&nbsp; &nbsp; &nbsp; &nbsp; .ToList();&nbsp; &nbsp; cmbValue(srchtxt, cmb);}

慕姐8265434

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