Form validate prior to close

C

Carl Gilbert

Hi

I was looking to validate a windows form with numerous text boxes.

I have tried using the ErrorProvider and the validaing event of each textbox
but I want to find a way to enable a button once all components on the form
satisfy a valid state.

From what I can gather, the ErrorProvider is not fired until focus moves off
the component.

Therefore, the button would be enable on form load as no components are
invalid.

Regards, Carl.
 
E

EMW

To say "prior to close" in that case you could use the Closing Event of the
form.

In this event you can set closing to false if validation of the textboxes is
not ok.

rg.
Eric
 
C

Carl Gilbert

I was aiming to disable the button as soon as the form loads and then
enable it once all components are valid. The only problem with this is
that all the components are valid at form load therefore the button will
be enabled.
 
S

Scott

Well, this solution doesn't use the ErrorProvider, but I find that it does
what you need.

Create a function that returns a boolean value and place your validation
code in it. Then on the TextChanged event of a text box (you can use the
same event for all text boxes by adding them to the "Handles" list in the
function declaration) set the enabled state of the button to the return
value of the function. Something like the following considering that your
button is "Button1" and you have 3 text boxes "TextBox1", "TextBox2", and
"TextBox3" and you're simply checking for text longer than 2 characters....


Private Sub TextBox_TextChanged(ByVal sender As Object, ByVal e As
System.EventArgs) _
Handles TextBox1.TextChanged, TextBox2.TextChanged,
TextBox3.TextChanged
Button1.Enabled = validateText()
End Sub

Private Function validateText() As Boolean
Dim b As Boolean = True

If TextBox1.Text.Length < 3 Then b = False
If TextBox2.Text.Length < 3 Then b = False
If TextBox3.Text.Length < 3 Then b = False

Return b
End Function

You can then enable/disable the button in the form load event and it will
change when the text of any textbox changes.

Scott
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top