locked
Another Textbox Validation Question RRS feed

  • Question

  • Hi, excuse me for going over old content but i cant find an exact match for what i'm trying to do.

    I know you can validate textbox text by using the keypress events. But the form i'm working on has multiple groupboxes with multiple textboxes. So rather than having a sub routine to validate every groupbox, i was wanting to have just the one keypress event that handles the textbox entries, and depending on the textbox calls a specific validation routine.

    Something like this?

    Dim aTextBox As TextBox
    
    Dim keypressverification As Boolean = False
    
    Dim keypressvalue As String
    
    
    
    Private Sub AppDets_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox3.KeyPress, _
    
     TextBox4.KeyPress, TextBox5.KeyPress, TextBox6.KeyPress, TextBox7.KeyPress
    
    
    
     aTextBox = CType(sender, TextBox)
    
     keypressvalue = e.KeyChar
    
    
    
     Select Case aTextBox.Name
    
      Case "Textbox3"
    
       AlphabetVerification()
    
      Case "Textbox7"
    
       AlphabetVerification()
    
      Case "Textbox4"
    
       NumericVerification()
    
      Case "Textbox5"
    
       NumericVerification()
    
      Case "Textbox6"
    
       NumericVerification()
    
     End Select
    
    
    
     If keypressverification = False Then
    
      e.Handled = False
    
     Else : e.Handled = True
    
     End If
    
    
    
    End Sub
    
    
    
    Private Sub AlphabetVerification()
    
    
    
     If Char.IsLetter(keypressvalue) Or Char.IsControl(keypressvalue) Then
    
      keypressverification = False
    
     Else
    
      MessageBox.Show("Invalid Entry, Must Not Be Numeric", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
    
      aTextBox.Focus()
    
      keypressverification = True
    
     End If
    
    
    
    End Sub
    
    
    
    Private Sub NumericVerification()
    
    
    
     If Char.IsNumber(keypressvalue) Or Char.IsControl(keypressvalue) Then
    
      keypressverification = False
    
     Else
    
      MessageBox.Show("Invalid Entry, Must Be Numeric", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
    
      aTextBox.Focus()
    
      keypressverification = True
    
     End If
    
    
    
    End Sub
    
    

    This is a modified version of my original code where i had the verification in the keypress event, but this is just one groupbox control. As i said i want to condense my code so there is just the one verification sub routine for all Groupboxes.

    Thanks for your help in advance,

    Morgannus


    Friday, June 10, 2011 7:46 PM

Answers

  • Hi,

    Is this short enough for you?  >>

    Option Strict On
    Option Explicit On
    Option Infer Off
    
    Public Class Form1
    
      Private Sub AppDets_KeyPress(ByVal sender As Object, _
                     ByVal e As System.Windows.Forms.KeyPressEventArgs) _
                     Handles TextBox3.KeyPress, TextBox4.KeyPress, TextBox5.KeyPress, _
                     TextBox6.KeyPress, TextBox7.KeyPress
    
        TextBoxValidation(sender, e)
    
      End Sub
    
      Private Sub TextBoxValidation(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
    
        'Allowed characters include the BackSpace key so you can delete characters
        'with the DELETE or the BackSpace keys:>>
        Dim allowedCharacters() As Char = _
        ("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" & ChrW(Keys.Back)).ToCharArray
    
        If allowedCharacters.Contains(e.KeyChar) = True Then
          e.Handled = False
        Else
          e.Handled = True
        End If
    
      End Sub
    
    End Class




    Regards,

    John

    Click this link to see how to insert a picture into a forum post.

    Installing VB6 on Windows 7

    XNA is coming to VB.Net




    • Proposed as answer by John Bravo Friday, June 10, 2011 10:55 PM
    • Marked as answer by morgannus Saturday, June 11, 2011 10:48 PM
    Friday, June 10, 2011 10:34 PM

All replies

  • Hi,

    Is this short enough for you?  >>

    Option Strict On
    Option Explicit On
    Option Infer Off
    
    Public Class Form1
    
      Private Sub AppDets_KeyPress(ByVal sender As Object, _
                     ByVal e As System.Windows.Forms.KeyPressEventArgs) _
                     Handles TextBox3.KeyPress, TextBox4.KeyPress, TextBox5.KeyPress, _
                     TextBox6.KeyPress, TextBox7.KeyPress
    
        TextBoxValidation(sender, e)
    
      End Sub
    
      Private Sub TextBoxValidation(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
    
        'Allowed characters include the BackSpace key so you can delete characters
        'with the DELETE or the BackSpace keys:>>
        Dim allowedCharacters() As Char = _
        ("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" & ChrW(Keys.Back)).ToCharArray
    
        If allowedCharacters.Contains(e.KeyChar) = True Then
          e.Handled = False
        Else
          e.Handled = True
        End If
    
      End Sub
    
    End Class




    Regards,

    John

    Click this link to see how to insert a picture into a forum post.

    Installing VB6 on Windows 7

    XNA is coming to VB.Net




    • Proposed as answer by John Bravo Friday, June 10, 2011 10:55 PM
    • Marked as answer by morgannus Saturday, June 11, 2011 10:48 PM
    Friday, June 10, 2011 10:34 PM
  • thats great thanks

    i put in the line

    aTextBox = CType(sender, TextBox)
    


    so i can do a select case statement on the text box, and also split the verification into 2 seperate subs so i can verify just numeric entry in one and alphabet entries in the other.

    and with that it works briliantly thanks

    Saturday, June 11, 2011 10:48 PM