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


    • Modifié Habib ur rehman vendredi 23 octobre 2020 16:47 Removing Ambiguous text.
    vendredi 23 octobre 2020 16:45

Réponses

  • 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
    



    samedi 24 octobre 2020 09:06

Toutes les réponses

  • 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

    vendredi 23 octobre 2020 17:55
  • 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
    



    samedi 24 octobre 2020 09:06
  • Thank you guys (Peter Fleischer & Viorel_)

    Was unable to reply soon.


    Habib Ur Rehman


    dimanche 25 octobre 2020 05:45