开发者

Winforms combobox compare

开发者 https://www.devze.com 2023-01-09 09:32 出处:网络
Am using two combobox in my C# .net windows application like combo开发者_StackOverflow社区box1 & combobox2(two combobox contain same items) if am choosing any items in combobox1i need to hide that

Am using two combobox in my C# .net windows application like combo开发者_StackOverflow社区box1 & combobox2(two combobox contain same items) if am choosing any items in combobox1 i need to hide that value in combobox2.


The simplest solution is to handle SelectedItemChanged on the first combo box and re-generate the list of Items for the second combo box so that the selected item in the first is excluded. This should perform alright if you don't have a large number of items. An additional boundary case to consider:

  • If you later change the selection in the first combo box to match what was selected in the second combo box, you might want to clear and reset the second combo box. You can handle this case in the same SelectedItemChanged event handler on the first combo box.


I guess you want the same index to be selected in the second combobox as you select in the first.

So on the SelectedIndexChanged event of combobox1 add this code:

combobox2.SelectedIndex = combobox1.SelectedIndex;


The easiest but perhaps not the cleanest:

Just repopulate combobox2 based on what's not in combobox1 every time an item is selected in combobox1.


You can use Contains to check if one have the other and do a loop to check all items.

 for (int i = 0; i < ListBox1.Items.Count; i++)
            {
                if (ListBox2.Items.Contains(ListBox1.Items[i]))
                {
                        ListBox1.Items.RemoveAt(i);
                        i--;
                }
            }
0

精彩评论

暂无评论...
验证码 换一张
取 消