0

I have 10 comboboxes with players. In each combobox must be selected one player. Now, I must validate their so, that only one unique player can be selected in combobox.

For example:

combobox1 - Anna

combobox2 - Anna (too)

But if Anna is selected I don't want choose she in another combobox. Or I can show error message on click button "Start Game", that Anna is selected in two comboboxes. The main thing it must be validate. I have only one Idea how I ca validate this and it's not the best way.

if cmbPlayer1.SelectedValue = cmbPlayer2.SelectedValue Or
   cmbPlayer1.SelectedValue = cmbPlayer2.SelectedValue Or
   ...
   cmbPlayer1.SelectedValue = cmbPlayer10.SelectedValue

and so for each of ten combobox.

How I can do it better?

Emma W.
  • 215
  • 1
  • 6
  • 20
  • The answer in [this question](https://stackoverflow.com/questions/18303897/test-if-all-values-in-a-list-are-unique) is quite useful. You could put each value into a collection and test that way. – A Friend Feb 09 '18 at 09:00
  • 1
    Personally, I would tend to filter the remaining lists when a selection is made, thus ensuring that the user can't possibly select duplicates. It's a bit more code but it's a better UX. Prevention is generally better than cure. – jmcilhinney Feb 09 '18 at 09:03
  • Can you put the names in one list box and set the Selection Mode so the user can make 10 selections in on list box? No duplicates. – Mary Feb 09 '18 at 09:36

2 Answers2

1

Create List of all selected value of value.

List<Player> players=new List<Player>
palyers.add(cmbPlayer1.SelectedValue) 

upto 10 Players.

Then check unique values

palyers.Distinct().Count()==10
A. S. Mahadik
  • 585
  • 1
  • 5
  • 17
0

How bout this

I tried with one combobox with bunch of names inside,and one listbox to show 10 players that have selected on the combobox. i make a condition like this :

If you select the same name it will shows an message "Multiple name detected". Else if you didn't do that you'll able to add another name to listbox.

So i make the "prevention" condition when selecting names,not when the button pressed.

Try to add a listbox and combobox,fill the listbox value with names. And make the name appear on listbox when you select it.

And code like this on the combobox

  Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
    If Not ListBox1.Items.Contains(ComboBox1.Text) Then
        ListBox1.Items.Add(ComboBox1.Text)
    Else
        MsgBox("Multiple name detected")
    End If
End Sub

Hope this will help you,and sorry for my bad english.

ai.msa
  • 1
  • 1