Results 1 to 2 of 2

Thread: Looking for a better way to evaluate textbox controls

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    259

    Looking for a better way to evaluate textbox controls

    I have multiple textboxes that should have numeric values and others that need to have some kind of entry added. So far I have been doing it this way but I am wondering if there is a better way. How would you do this?
    VB Code:
    1. Dim strAction As String = txtAction.Text
    2.         Select Case strAction
    3.             Case "A"
    4.                 If IsNumeric(txtEmp.Text) = False Then
    5.                     MsgBox("Employee number must be a numeric value.", _
    6.                           MsgBoxStyle.OKOnly, "Employee Number Error")
    7.                     txtEmp.Focus()
    8.                     Exit Sub
    9.                 End If
    10.                 If IsNumeric(txtDayCode.Text) = False Then
    11.                     MsgBox("This field must be a numeric value 1-7.", _
    12.                         MsgBoxStyle.OKOnly, "Day Code Error")
    13.                     txtDayCode.Focus()
    14.                     Exit Sub
    15.                 End If
    16.                 If (txtJobNumber.Text) = "" Then
    17.                     MsgBox("The job number field can not be left blank.")
    18.                     txtJobNumber.Focus()
    19.                     Exit Sub
    20.                 End If
    21.                 If (txtPC.Text) = "" Then
    22.                     MsgBox("The Pc Field can not be left blank.")
    23.                     txtPC.Focus()
    24.                     Exit Sub
    25.                 End If
    26.                 If (txtTime.Text) = "" Then
    27.                     MsgBox("The time field can not be left blank.")
    28.                     txtTime.Focus()
    29.                     Exit Sub
    30.                 End If
    31.                 If (txtAmount.Text) = "" Then
    32.                     MsgBox("The amount field can not be left blank.")
    33.                     txtAmount.Focus()
    34.                     Exit Sub
    35.                 End If

  2. #2
    Frenzied Member bmahler's Avatar
    Join Date
    Oct 2005
    Location
    Somewhere just west of the Atlantic
    Posts
    1,568

    Re: Looking for a better way to evaluate textbox controls

    You could try adding the type of validation to the tag property and then just loop through the textboxes on the form ie
    For those that must be numeric add numeric to the tag property and those that can not be blank add noNull to the tag property then just loop through like so
    VB Code:
    1. For Each ctl As Control In Me.Controls
    2.             If TypeOf ctl Is TextBox Then
    3.                 Dim tBox As TextBox = DirectCast(ctl, TextBox)
    4.                 Select Case tBox.Tag
    5.                     Case "numeric"
    6.                         Dim intTest As Integer
    7.                         If Not Integer.TryParse(tBox.Text, intTest) Then
    8.                             MessageBox.Show(tBox.Name & " must be numeric")
    9.                             tBox.Focus()
    10.                         End If
    11.                     Case "noNull"
    12.                         If tBox.Text = String.Empty Then
    13.                             MessageBox.Show(tBox.Name & " can not be null")
    14.                             tBox.Focus()
    15.                         End If
    16.                 End Select
    17.             End If
    18.         Next

    or another example is if you want custom error messages, put the error message in the tag property but start the line with an identifier for type like so
    txtEmp.Tag = "N*Employee number must be a numeric value."
    txtJobNumber.Tag = "R*"The job number field can not be left blank."

    where N* signifies a numeric field and R* signifies required (just examples, you could use whatever you want)

    VB Code:
    1. For Each ctl As Control In Me.Controls
    2.             If TypeOf ctl Is TextBox Then
    3.                 Dim tBox As TextBox = DirectCast(ctl, TextBox)
    4.                 Dim type As String = tBox.Tag.ToString.Substring(0, 2)
    5.                 Select Case type
    6.                     Case "N*"
    7.                         Dim intTest As Integer
    8.                         If Not Integer.TryParse(tBox.Text, intTest) Then
    9.                             MessageBox.Show(tBox.Tag.ToString.Replace(type, String.Empty))
    10.                             tBox.Focus()
    11.                         End If
    12.                     Case "R*"
    13.                         If tBox.Text = String.Empty Then
    14.                             MessageBox.Show(tBox.Tag.ToString.Replace(type, String.Empty))
    15.                             tBox.Focus()
    16.                         End If
    17.                 End Select
    18.             End If
    19.         Next
    Last edited by bmahler; Dec 12th, 2006 at 11:42 PM.
    Boooya
    • Visual Studio 2008 Professional
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • Don't forget to rate helpful posts!
    • If you're question was answered please mark your thread [Resolved]


    Code Contributions:
    PHP
    PHP Image Gallery v1.0PHP Image Gallery v2.0
    VB 2005
    Find Computers on a networkSimple License EncryptionSQL Server Database Access dllUse Reflection to Return Crystal ReportDocumentSilently Print PDFGeneric Xml Serailizer


    Useful Links: (more to come)
    MSDN (The first and foremost)MSDN Design Guidelines API Reference • Inno Setup CompilerInno Setup PreprocessorISTool - Fairly easy to use GUI for creating inno setup projects • Connection StringsNAnt -Automated BuildsCruise Control .NET - Frontend for automated builds

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width