Results 1 to 6 of 6

Thread: [RESOLVED] Form validation

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Location
    Lancashire UK
    Posts
    375

    Resolved [RESOLVED] Form validation

    Just a quick question, I have an application that has about 15 textboxes and 5 checkboxes contained in 3 groupboxes. What I want to do is only enable the 'Save' button once all the required information has been added (basically I want to prevent partial submissions).

    I'm using an ErrorProvider to check the textboxes but this only works when the textbox has focus. I'm thinking of using a timer event to check through the textboxes and enable the 'Save' button once all the conditions are met, is this a good way of doing it ?

  2. #2
    Hyperactive Member
    Join Date
    Jan 2002
    Location
    Palermo, Italy
    Posts
    325

    Re: Form validation

    Can't you use "foreach mycontrol in myform" loop?
    Learn, this is the Keyword...

  3. #3
    Junior Member
    Join Date
    Jan 2006
    Posts
    31

    Re: Form validation

    Im not sure exactly wat you mean, but if you want to make sure the textbox isnt empty just use:
    VB Code:
    1. If txtTextBox.Text = "" Then
    2. MsgBox("Please enter some text")
    3. End If

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Form validation

    No that is definitely not a good way of doing it. What you can do is set the Enabled property of the Button to False to begin with. You could then have a method that checks each control for a valid value and sets the Enabled property of the Button accordingly. You could then call this method from the Validated event handler of each control. Here's an example where there are three TextBoxes and the valid condition is that they all have at least one non-white space character in them.
    VB Code:
    1. Private Sub TextBox1_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Validated, _
    2.                                                                                                 TextBox2.Validated, _
    3.                                                                                                 TextBox3.Validated
    4.         Me.SetButtonState()
    5.     End Sub
    6.  
    7.     Private Sub SetButtonState()
    8.         Me.Button1.Enabled = Me.TextBox1.Text.Trim() <> String.Empty AndAlso _
    9.                              Me.TextBox2.Text.Trim() <> String.Empty AndAlso _
    10.                              Me.TextBox3.Text.Trim() <> String.Empty
    11.     End Sub
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Form validation

    Actually, it would probably be a better idea to use the TextChanged event of the TextBoxes and the SelectedIndexChanged event of the ComboBoxes to call the SetButtonState method. That way you don't have to wait unitl focus leaves the last control and the Save Button will be enabled when you're ready to click it, rather than you having to shift focus to enable it.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Location
    Lancashire UK
    Posts
    375

    Re: Form validation

    Thanks for the help and advice

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