none
Validation RRS feed

  • Question

  • Hi people, I am designing an application for students registration with no database, the student will enter their details,then press the next button and it should take them to the next form and hide the first form.The validation must be done before moving to the next form. I am have a problem with the validation. It doesn't finish validating everything, It validates the textbox and doesnt allow me to go back and type the required information.I need it to check all the validation before it moves to the next form without errors. Please assist... I have attached my code

     If txtSrname.Text = String.Empty Then
                MsgBox("Please enter your Surname", MsgBoxStyle.OkOnly)
            Else
                txtSrname.Focus()
            End If

            sname = txtSrname.Text
            fname = txtName.Text
            initials = txtInitials.Text
            marital = cmdstatus.SelectedItem
            gender = cmbGender.SelectedItem
            racial = cmdRacial.SelectedItem
            citizen = cmdcitizen.SelectedItem
            said = txtID.Text

            'The below will hide form 1 and show form 2
            Form2.Show()
            Me.Hide()


    Monday, June 18, 2018 5:00 PM

All replies

  • Hi

    Is this a school assignment?

    Firstly, your For .....Next block :

    Do you really mean that if 'txtSrname.Text' is NOT empty then re-focus the same TextBox?

    You say you have attached your code - you certainly haven't shown any full code - which event handlers are you using, and what code in them (as an example)?


    Regards Les, Livingston, Scotland

    Monday, June 18, 2018 5:33 PM
  • Check this:

       If txtSrname.Text.Trim = String.Empty Then

          txtSrname.Focus()

          MsgBox("Please enter your Surname", MsgBoxStyle.OkOnly)

          Return

       End If
       . . .

    Repeat it for the other controls.



    • Edited by Viorel_MVP Monday, June 18, 2018 5:48 PM
    Monday, June 18, 2018 5:47 PM
  • Validation is typically best performed by handling the Validating event on each control.

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Monday, June 18, 2018 8:27 PM
    Moderator
  • First off you really should consider a wizard approach rather than navigating form to form. The following code sample will provide you with a wizard control.

    In regards to validating, Reed offer a great approach. See the following Microsoft article

    User Input Validation in Windows Forms, review data binding, event-driven validation and the sections following those sections. Make sure to review "Closing the form and overriding validation".


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Monday, June 18, 2018 11:46 PM
    Moderator
  • If txtSrname.Text = String.Empty Then
                MsgBox("Please enter your Surname", MsgBoxStyle.OkOnly)
                        txtSrname.Focus()

                        Exit Sub

            End If

            sname = txtSrname.Text
            fname = txtName.Text
            initials = txtInitials.Text
            marital = cmdstatus.SelectedItem
            gender = cmbGender.SelectedItem
            racial = cmdRacial.SelectedItem
            citizen = cmdcitizen.SelectedItem
            said = txtID.Text

            'The below will hide form 1 and show form 2
            Form2.Show()
            Me.Hide()

    Tuesday, June 19, 2018 8:51 AM