Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 2010 > Visual Basic 2010 General Discussion
|
Visual Basic 2010 General Discussion For any discussions about Visual Basic 2010 topics which aren't related to a specific Wrox book
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Visual Basic 2010 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 20th, 2011, 12:56 AM
Authorized User
 
Join Date: Mar 2011
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default The Mistery of Windows Form Validation - How do you really validate windows forms?

I have a windows form I need to validate.

Example: I have several text boxes, check boxes and radio buttons I need to validate. I need to validate the text boxes for text(strings) only, no characters. How do I validate multiple text boxes without rewriting the codes.

Finally how do I validate radio buttons and check boxes?

Here is what I have written so far. This idea of validation using conditions is based on standard programming.

However, my code does not work properly.

1. I have to provide a loop so the code continue to fire error messages until the user enter a valid data. Right now if the user enter a number, the form fires an error message, if the user deletes the numbers, enters text and re-enters number the form does not validate. That's because the code must have a loop. How do I loop this code, if there a better way to validate?

2. How do I use what I have already written on multiple form controls with out writing this entire code for every form control?

Here is what I have writing.

Code:
Public Class validDataForm

    Private Sub validDataForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub btnDataSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDataSubmit.Click
        Dim invalidUserData As String
        invalidUserData = "!, @, #, $, %, ^, &, *, (, ), _, +, [, {, ], }, \, =, |, :, ;,>, <, ?, /, .,"

        If IsNumeric(txtDataEntry.Text) Then
            lblErrorDisplay.Text = "Invalid Name!"

        ElseIf txtDataEntry.Text = String.Empty Then
            lblErrorDisplay.Text = "Full Name Required!"

        ElseIf txtDataEntry.Text.Length > 25 Or txtDataEntry.Text.Length < 2 Then
            lblErrorDisplay.Text = "Valid Name Required!"

        ElseIf txtDataEntry.Text = String.Empty Then
            lblErrorDisplay.Text = "Name Required!"

        ElseIf txtDataEntry.Text = invalidUserData Then
            lblErrorDisplay.Text = "Valid Name Required!"

        Else
            lblErrorDisplay.Hide()

        End If

    End Sub

    Private Sub lblUserName_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblUserName.Click

    End Sub

    Private Sub txtDataEntry_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtDataEntry.TextChanged

    End Sub
End Class


Thanks everyone!

NovicePGM
 
Old March 20th, 2011, 07:56 AM
Authorized User
 
Join Date: May 2010
Posts: 70
Thanks: 4
Thanked 6 Times in 6 Posts
Send a message via Yahoo to GeneBuchite
Default Valididation, The Programers Bane!

I would place my validation code in a Sub. then call the sub each time you leave the individual field. ie...
Code:
 
    Private Sub btnDataSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDataSubmit.Click
       

       If RBSexF.Checked = False And rbSexM.Checked = False Then                  ' Validate Radio Buttons
            MsgBox("You Must Select Either MAle Or Female")

            Exit Sub
        End If

        Dim invalidUserData As String


        invalidUserData = "!, @, #, $, %, ^, &, *, (, ), _, +, [, {, ], }, \, =, |, :, ;,>, <, ?, /, .,"

        If IsNumeric(txtDataEntry.Text) Then
            lblErrorDisplay.Text = "Invalid Name!"

        ElseIf txtDataEntry.Text = String.Empty Then
            lblErrorDisplay.Text = "Full Name Required!"

        ElseIf txtDataEntry.Text.Length > 25 Or txtDataEntry.Text.Length < 2 Then
            lblErrorDisplay.Text = "Valid Name Required!"

        ElseIf txtDataEntry.Text = String.Empty Then
            lblErrorDisplay.Text = "Name Required!"

        ElseIf txtDataEntry.Text = invalidUserData Then
            lblErrorDisplay.Text = "Valid Name Required!"

        Else
            lblErrorDisplay.Text = "All Ok"


        End If

    End Sub



    Private Sub txtDataEntry_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtDataEntry.TextChanged

    End Sub



    Private Sub txtDataEntry_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtDataEntry.Validating
        ValidateEntries(txtDataEntry.Text.ToString, "Name")

    End Sub
    Private Sub ValidateEntries(ByVal txt As String, ByVal lbl As String)
        MsgBox(txt)
        Dim invalidUserData As String
        invalidUserData = "!, @, #, $, %, ^, &, *, (, ), _, +, [, {, ], }, \, =, |, :, ;,>, <, ?, /, .,"

        If IsNumeric(txt) Then
            lblErrorDisplay.Text = "Invalid " & lbl & "!"

        ElseIf txt = String.Empty Then
            lblErrorDisplay.Text = lbl & " Required!"

        ElseIf txt.Length > 25 Or txt.Length < 2 Then
            lblErrorDisplay.Text = "Valid " & lbl & " Required! Must be at least Longer than 2 and less than 25!"


        ElseIf txt = invalidUserData Then
            lblErrorDisplay.Text = "Valid " & lbl & " Required!"

        Else
            lblErrorDisplay.Text = "All Ok"


        End If
    End Sub

    Private Sub TextBox1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Leave
        ValidateEntries(TextBox1.Text, "Address")
    End Sub
To Demonstrate I added another TextBox "TextBox1" To The Form

You will need to write new codes for each field that uses different rules.


I Am not sure of your Question of Validating Radio Buttons MAybe something Like This Would Help...
Add 2 Radio Buttons. NAme Them rbSexM & THen The btnValidate Will Validate The Radio Buttons.

Last edited by GeneBuchite; March 20th, 2011 at 08:08 AM.. Reason: I Omitted some info
 
Old March 20th, 2011, 11:04 AM
Authorized User
 
Join Date: Mar 2011
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by GeneBuchite View Post
I would place my validation code in a Sub. then call the sub each time you leave the individual field. ie...
Code:
 
    Private Sub btnDataSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDataSubmit.Click
       

       If RBSexF.Checked = False And rbSexM.Checked = False Then                  ' Validate Radio Buttons
            MsgBox("You Must Select Either MAle Or Female")

            Exit Sub
        End If

        Dim invalidUserData As String


        invalidUserData = "!, @, #, $, %, ^, &, *, (, ), _, +, [, {, ], }, \, =, |, :, ;,>, <, ?, /, .,"

        If IsNumeric(txtDataEntry.Text) Then
            lblErrorDisplay.Text = "Invalid Name!"

        ElseIf txtDataEntry.Text = String.Empty Then
            lblErrorDisplay.Text = "Full Name Required!"

        ElseIf txtDataEntry.Text.Length > 25 Or txtDataEntry.Text.Length < 2 Then
            lblErrorDisplay.Text = "Valid Name Required!"

        ElseIf txtDataEntry.Text = String.Empty Then
            lblErrorDisplay.Text = "Name Required!"

        ElseIf txtDataEntry.Text = invalidUserData Then
            lblErrorDisplay.Text = "Valid Name Required!"

        Else
            lblErrorDisplay.Text = "All Ok"


        End If

    End Sub



    Private Sub txtDataEntry_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtDataEntry.TextChanged

    End Sub



    Private Sub txtDataEntry_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtDataEntry.Validating
        ValidateEntries(txtDataEntry.Text.ToString, "Name")

    End Sub
    Private Sub ValidateEntries(ByVal txt As String, ByVal lbl As String)
        MsgBox(txt)
        Dim invalidUserData As String
        invalidUserData = "!, @, #, $, %, ^, &, *, (, ), _, +, [, {, ], }, \, =, |, :, ;,>, <, ?, /, .,"

        If IsNumeric(txt) Then
            lblErrorDisplay.Text = "Invalid " & lbl & "!"

        ElseIf txt = String.Empty Then
            lblErrorDisplay.Text = lbl & " Required!"

        ElseIf txt.Length > 25 Or txt.Length < 2 Then
            lblErrorDisplay.Text = "Valid " & lbl & " Required! Must be at least Longer than 2 and less than 25!"


        ElseIf txt = invalidUserData Then
            lblErrorDisplay.Text = "Valid " & lbl & " Required!"

        Else
            lblErrorDisplay.Text = "All Ok"


        End If
    End Sub

    Private Sub TextBox1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Leave
        ValidateEntries(TextBox1.Text, "Address")
    End Sub
To Demonstrate I added another TextBox "TextBox1" To The Form

You will need to write new codes for each field that uses different rules.


I Am not sure of your Question of Validating Radio Buttons MAybe something Like This Would Help...
Add 2 Radio Buttons. NAme Them rbSexM & THen The btnValidate Will Validate The Radio Buttons.


Thanks for your reply, I will work with your example and let you know how things turned out.

Thanks again a lot!!


NovicePGM





Similar Threads
Thread Thread Starter Forum Replies Last Post
Windows Forms sambathrajmca Windows Workflow 1 November 27th, 2007 06:25 AM
windows forms lakshmiR .NET Framework 1.x 1 August 31st, 2007 08:32 PM
Help with Windows Forms tycotrix C# 1 January 16th, 2007 11:58 AM
windows forms chandrasekhar ASP.NET 2.0 Professional 2 February 27th, 2006 10:41 PM
Windows forms eyan C# 1 July 1st, 2004 04:10 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.