How does Validation Work in WinForms?

  • Thread starter Thread starter Charles Law
  • Start date Start date
Hi guys

I see the thread disappearing off to the right, so I hope I have picked a
good point to respond.

Maybe I have picked up on a misapprehension regarding the validation events.
I had assumed that the Validating event for the form would fire when the
form was validating, and it makes sense to me that it should occur when the
dialog result is OK, or Yes, or some such, that is, when the user takes some
action that will cause the form to be affirmed (I avoid the word
'submitted').

If not, it begs the question "When does it occur?".

I think that if I understood that then the rest might make sense. There is,
perhaps, a pattern that Microsoft has adopted, but I don't think I know it.

Charles
 
In the closing event, set the Cancel property of the form to True or
false, as required, and validate using the Validating event proc.

with regards,


J.V.Ravichandran
- http://www.geocities.com/
jvravichandran
- http://www.411asp.net/func/search?
qry=Ravichandran+J.V.&cob=aspnetpro
- http://www.southasianoutlook.com
- http://www.MSDNAA.Net
- http://www.csharphelp.com
- http://www.poetry.com/Publications/
display.asp?ID=P3966388&BN=999&PN=2
- Or, just search on "J.V.Ravichandran"
at http://www.Google.com
 
Charles Law said:
Could someone please explain to me, in words of one syllable or less, how I
get the Validating event to fire for a form.

I have a form with one text box, and two buttons: OK and Cancel.

Charles,

I may be missing the point, but it seems to me that performing the
validation in the OK button click event is the most eficient way to do
this. Here is a sample I used to verify my own thoughts (this assumes
you use the showdialog method for the data input form):

Private Sub ButtonOK_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles ButtonOK.Click
'Validate text data
If Me.TextBoxEmail.Text.IndexOf("@") = -1 Then
MessageBox.Show("Invalid Email Address.")
Exit Sub
Else
Me.DialogResult = DialogResult.OK
Me.Close()

End If
End Sub
 
Hi Charlie

I'm sure you're not missing the point, and quite probably the most efficient
place is the OK_Click event, but ...

I have now implemented the solution by using the OK_Click event in much the
way you suggest. I am still left pondering, though, how and when the Form1
Validating event is ever called. It's just not happening for me. Whatever I
seem to do, it does not fire. I probably won't use it now for my current
situation, but I am assuming that it should fire at some point, and when it
does I think I would like to be there.

I would be grateful if someone could post the most trivial of examples where
this event fires. Then I could probably begin to understand what makes it
tick.

Charles
 
Charles Law said:
Hi Charlie

I'm sure you're not missing the point, and quite probably the most
efficient place is the OK_Click event, but ...

I have now implemented the solution by using the OK_Click event in
much the way you suggest. I am still left pondering, though, how and
when the Form1 Validating event is ever called. It's just not
happening for me. Whatever I seem to do, it does not fire. I probably
won't use it now for my current situation, but I am assuming that it
should fire at some point, and when it does I think I would like to
be there.

I would be grateful if someone could post the most trivial of
examples where this event fires. Then I could probably begin to
understand what makes it tick.

Maybe the "right" group
microsoft.public.dotnet.framework.windowsforms[.controls]
has an answer. I'd still say that the validating event is there to validate
a control when it looses the focus.

--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 
Back
Top