none
Multiple Textboxes Validation RRS feed

  • Question

  • Is there any way to validate multiple textboxes an easy way.

    Currently I am doing it using this method which is Lengthy approach.

    Private Sub Textboxes_Leave(sender As Object, e As EventArgs) Handles TB1.Leave,
            TB2.Leave, TB3.Leave, TB4.Leave
            Dim TBName As TextBox = CType(sender, TextBox)
            If TBName.Name = TB1.Name And TBName.Text <> "" Then
                If TBName.Text.Trim = TB2.Text.Trim Or TBName.Text.Trim = TB3.Text.Trim Or
                        TBName.Text.Trim = TB_1d2dC4.Text.Trim Then
                    MsgBox("Similarity found.")
                End If
            ElseIf TBName.Name = TB2.Name And TBName.Text <> "" Then
                If TBName.Text.Trim = TB1.Text.Trim Or TBName.Text.Trim = TB3.Text.Trim Or
                            TBName.Text.Trim = TB_4.Text.Trim Then
                    MsgBox("Similarity found.")
                End If
            End If
            ElseIf TBName.Name = TB3.Name And TBName.Text <> "" Then
                If TBName.Text.Trim = TB1.Text.Trim Or TBName.Text.Trim = TB2.Text.Trim Or
                            TBName.Text.Trim = TB_4.Text.Trim Then
                    MsgBox("Similarity found.")
                End If
            End If
    End Sub


    • Edited by Habib ur rehman Friday, October 23, 2020 4:47 PM Removing Ambiguous text.
    Friday, October 23, 2020 4:45 PM

Answers

  • Also try a validation like this:

    Private Sub TextBoxes_Leave(sender As Object, e As EventArgs) Handles TextBox4.Leave, TextBox3.Leave, TextBox2.Leave, TextBox1.Leave
        Dim currentTextBox = CType(sender, TextBox)
        Dim allTextBoxes = {TextBox1, TextBox2, TextBox3, TextBox4}
    
        If currentTextBox.Text <> "" Then
            For Each tb In allTextBoxes
                If tb Is currentTextBox Then Continue For
                If currentTextBox.Text = tb.Text Then
                    MsgBox("Similarity found.")
                End If
            Next
        End If
    End Sub
    



    • Edited by Viorel_MVP Saturday, October 24, 2020 9:09 AM
    • Marked as answer by Habib ur rehman Sunday, October 25, 2020 5:42 AM
    Saturday, October 24, 2020 9:06 AM

All replies

  • Hi Habib,
    you can bind TextBoxes to properties of data object and in data class implement IDataErrorInfo. On Leave events you check only error information indata object.

    --
    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks

    Friday, October 23, 2020 5:55 PM
  • Also try a validation like this:

    Private Sub TextBoxes_Leave(sender As Object, e As EventArgs) Handles TextBox4.Leave, TextBox3.Leave, TextBox2.Leave, TextBox1.Leave
        Dim currentTextBox = CType(sender, TextBox)
        Dim allTextBoxes = {TextBox1, TextBox2, TextBox3, TextBox4}
    
        If currentTextBox.Text <> "" Then
            For Each tb In allTextBoxes
                If tb Is currentTextBox Then Continue For
                If currentTextBox.Text = tb.Text Then
                    MsgBox("Similarity found.")
                End If
            Next
        End If
    End Sub
    



    • Edited by Viorel_MVP Saturday, October 24, 2020 9:09 AM
    • Marked as answer by Habib ur rehman Sunday, October 25, 2020 5:42 AM
    Saturday, October 24, 2020 9:06 AM
  • Thank you guys (Peter Fleischer & Viorel_)

    Was unable to reply soon.


    Habib Ur Rehman


    Sunday, October 25, 2020 5:45 AM