TextBox Reverse Validation

Alex Moy 21 Reputation points
2021-01-16T00:20:30.423+00:00

So I have an odd question. I have 10 TextBoxes on my form. TextBox1 through to TextBox10. I would like to know how I would throw up a message if any 2 (or more) of these boxes contain the same information.

In other words I want validation that every box contains something different, else "Please check input"

Obviously I can check 2 boxes together (If TextBox1.Text = TextBox2.Text then...) but I want to check all 10 against all 10.

Any help will be greatly appreciated.

Developer technologies Windows Forms
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 122.5K Reputation points
    2021-01-16T08:58:16.23+00:00

    Try one of solutions:

    Dim textboxes = {TextBox1, TextBox2, TextBox3, TextBox4, TextBox5,
                     TextBox6, TextBox7, TextBox8, TextBox9, TextBox10}
    
    Dim strings = From tb In textboxes
                  Where Not String.IsNullOrWhiteSpace(tb.Text)
                  Select tb.Text
    
    Dim has_duplicates As Boolean = strings.Distinct.Count <> strings.Count
    
    If has_duplicates Then MsgBox("Please check input")
    

    If you do not want to exclude empty textboxes, then remove the Where condition.

    In order to access textboxes by name, you can also consider Me.Controls.Find.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.