具有相同项目的多个组合框。当一个选定的其他人必须无法选择相同的项目。 [英] Multiple combobox with same items. When one selected other must be unable to select same item.

查看:72
本文介绍了具有相同项目的多个组合框。当一个选定的其他人必须无法选择相同的项目。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有五个具有相同成员的组合框,当一个组合框选择一个项目时,其他组合框必须无法选择相同的成员。

任何人都有解决方案。帮助我。



我尝试了什么:



没有任何关于这一点的想法。

解决方案

在Winforms中你可以使用 DataGridView 而不是 ComboBox 并将行 readonly ,请参见此处的示例: C#DataGridView只读列和行 [ ^ ]

这是一种使用标准 ComboBox 的方法: c# - 禁用组合框中的特定项目 - 堆栈溢出 [ ^ ]


因为你有提到ComboBox,我假设你正在开发一个Windows应用程序。这是解决方案。



创建不同的绑定方法以将数据绑定到组合框。然后在每个组合框的SelectedIndexChanged事件中,您需要从其他组合框中删除您不想要的项目。为此,如果要处理List或任何类似的对象,可以使用RemoveAt()方法。删除项目后重新绑定组合框以绑定更新列表。


假设它们具有相同的索引(因为它们确实如此),您可以尝试以下方法:



1.创建一个列表或数组,每次在ComboBox中选择项目时都会保存所有选定的索引,并使用它来检查是否已从另一个ComboBox中选择该项目。



或者如果你不想使用List或Array,



2.使用这些ComboBoxes本身来检查所选项目。



你可以只显示一条消息(例如:MessageBox),或者只是从其他ComboBox中删除已经选择的项目,以便不能再次选择它。

I have Five comboBoxes with same members, when one combobox selects an item, other comboboxes must be unable to select same member.
anyone have solution for this.? help me.

What I have tried:

dont have any idea about this.

解决方案

In Winforms you can use a DataGridView instead of a ComboBox and make the row readonly, see example here: C# DataGridView Read Only Columns and Rows[^]
Here is a way to do it with a standard ComboBox: c# - Disabling particular Items in a Combobox - Stack Overflow[^]


As you have mentioned ComboBox, I assume you are working on a windows application. Here's the solution.

Create different binding methods to bind data to comboboxes. Then on SelectedIndexChanged event of each combobox you need to remove item(s) you don't want from other comboboxes. To do this you can use RemoveAt() method if you are dealing with List or any similar kind of objects. After removing the items rebind the combobox to bind updated list.


Assuming that they have the same indexes (because definitely, they do), you can try the following:

1. Create a List or Array that will hold all the selected index every time you select item in the ComboBox and use it to check if the item is already selected from another ComboBox.

Or if you don't want to use List or Array,

2. Use those ComboBoxes itself to check the selected items.

You can just display a Message (ex: MessageBox) or just remove the already selected item/s from other ComboBoxes so that it cannot be selected again.


这篇关于具有相同项目的多个组合框。当一个选定的其他人必须无法选择相同的项目。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆