Hey, im just wondering if there was any easy were to make sure that an entry havent been used twice in a selection of ComboBoxes.

Heres the start for what id have to do if there isnt.

Private Sub ComboBox9_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox9.SelectedIndexChanged
        If ComboBox9.Text = ComboBox1.Text Or ComboBox2.Text Or ComboBox3.Text Or ComboBox4.Text Or ComboBox5.Text Or ComboBox6.Text Or ComboBox7.Text Or ComboBox8.Text Then
            MessageBox.Show("You have used this key twice, please go back and change one of these asignments.", "Key Assignment", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
    End Sub

A simpler approach would be to iterate through all the comboboxes on your form (or control that holds combo boxes e.g. a Panel), build up a collection of these comboboxes then compare them accordingly. The following code contains two methods, AreSelectedComboBoxValuesDuplicated and GetComboBoxes.

GetComboBoxes does exactly what it says on the tin and gets a controls child combobox controls, it is recursive so it will check within the controls child controls and within their controls etc.

Once we have this list of combo boxes we can then check to see if any contain the same value. AreSelectedComboBoxValuesDuplicated simply loops through all combo boxes we have found and compares each found combobox against all the others, the search IS case sensitive. The method will then return a flag indicating whether any values were duplicated or not.

Private Function AreSelectedComboBoxValuesDuplicated(parent As Control) As Boolean
        ' Get all combo boxes
	Dim comboBoxes As IList(Of ComboBox) = GetComboBoxes(parent)
	For i As Int32 = 0 To comboBoxes.Count - 1
                ' Get current combobox
		Dim comboBox As ComboBox = comboBoxes(i)
		For k As Int32 = 0 To comboBoxes.Count - 1
			If k <> i Then
                                ' If the combobox to compare to is not the same one then check value
				If [String].Compare(comboBox.Text, comboBoxes(k).Text, False) = 0 Then
					Return True
				End If
			End If
		Next
	Next
	Return False
End Function

Private Function GetComboBoxes(parent As Control) As IList(Of ComboBox)
	Dim comboBoxes As IList(Of ComboBox) = New List(Of ComboBox)()
	For Each child As Control In parent.Controls
                ' If the control is a combobox then add it to list else check its children
		If child.[GetType]() Is GetType(ComboBox) Then
			comboBoxes.Add(TryCast(child, ComboBox))
		Else
			If child.HasChildren Then
				Dim innerComboBoxes As IList(Of ComboBox) = GetComboBoxes(child)
				For Each innerComboBox As ComboBox In innerComboBoxes
					comboBoxes.Add(innerComboBox)
				Next
			End If
		End If
	Next
	Return comboBoxes
End Function

To use the above methods you could perform something similar to...

' Check if values are duplicated, MyPanel is a panel control that contains combo boxes
Dim duplicated As Boolean = AreSelectedComboBoxValuesDuplicated(MyPanel)
IF duplicated Then
  MessageBox.Show("Warning selected values were duplicated!")
End If
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.