|
-
Feb 13th, 2006, 08:03 AM
#1
Thread Starter
Hyperactive Member
[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 ?
-
Feb 13th, 2006, 08:14 AM
#2
Hyperactive Member
Re: Form validation
Can't you use "foreach mycontrol in myform" loop?
Learn, this is the Keyword...
-
Feb 13th, 2006, 08:15 AM
#3
Junior Member
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:
If txtTextBox.Text = "" Then
MsgBox("Please enter some text")
End If
-
Feb 13th, 2006, 08:15 AM
#4
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:
Private Sub TextBox1_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Validated, _
TextBox2.Validated, _
TextBox3.Validated
Me.SetButtonState()
End Sub
Private Sub SetButtonState()
Me.Button1.Enabled = Me.TextBox1.Text.Trim() <> String.Empty AndAlso _
Me.TextBox2.Text.Trim() <> String.Empty AndAlso _
Me.TextBox3.Text.Trim() <> String.Empty
End Sub
-
Feb 13th, 2006, 08:17 AM
#5
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.
-
Feb 13th, 2006, 11:36 AM
#6
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|