How does Validation Work in WinForms?

C

Charles Law

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.

I have the OK button set as the AcceptButton and the Cancel button as the
.... well, you've guessed it.

I click OK and the form closes, without going to the Form1_Validating event.
I could validate in the Click event of the OK button, but I can't cancel the
form close from there? This is a simple form, straight out of the box.

Could someone please put me straight?

TIA

Charles
 
C

Cor Ligthert

Hi Charles,

In the event is an e.cancel it has as I thought to be set in the opposite
way than you normaly would think.

The same is also in the form close, which is related to that.

Can you find it, because this is no new subject in this newsgroup and when
you want I will search for you the get it more exact (as I have answered
those) ?

I think you are clever enough to find it with this information.

Cor
 
C

Charles Law

Hi Cor

It wasn't so much the e.Cancel that I was having trouble with, as the fact
that the event doesn't get called.

If I could get the Validating event to fire I would be laughing.

As you suggest, I could put it all in the Close or Closing event, but then I
would have to test to see which button was clicked as I don't want to
validate if the Cancel button is clicked.

Do I need to set some properties in order to get the Validating event to
fire?

Charles
 
C

Cor Ligthert

Hi Charles,

Is this not where you looking for?
Private Sub Form1_Closing(ByVal _
sender As Object, ByVal _
e As System.ComponentModel.CancelEventArgs) _
Handles MyBase.Closing
e.Cancel = False
End Sub

Or is it the validating itself, it fires before or after that a control lost
focus (this depends if it keyboard, mouse or the focus method).

Not clear, ask again?

Cor
 
C

CJ Taylor

This sounds an aweful lot like the problem I've been having with the Closing
event and validation. I think the order is kinda fubared if you use the
Handles clause.

My issue was, I would hit the X button, however, if I was in a text box that
caused validation, that was fired first.

I don't know if this could be classified as a "bug" but it certainly is
annoying. Unfortunatly, I have no solution. The only way I "partially" got
it to work was on the validation event of my textbox I called
Application.DoEvents(), which kinda worked. I'll go back and look at it
more.

-CJ
 
H

Herfried K. Wagner [MVP]

* "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.

I have the OK button set as the AcceptButton and the Cancel button as the
... well, you've guessed it.

I click OK and the form closes, without going to the Form1_Validating event.
I could validate in the Click event of the OK button, but I can't cancel the
form close from there? This is a simple form, straight out of the box.

Don't assign a 'DialogResult' to the buttons/form at design time.
Assign it in the buttons' 'Click' event handlers if you call the form's
'Close' method.
 
C

Charles Law

Hi Cor

Yes and no. I know I can cancel the close if I do it in the Closing event.
What I cannot do is get the Validating event to fire at all. Although you
say that it fires when a control loses focus, it doesn't do that for me.

If I click my OK button, the Form1_Validating event does not fire.

What makes this event fire? Am I expecting the wrong things to happen?

What I am looking for is a single point where I can do all my form
validation when the user attempts to submit the form. If the validation
fails I will put up a message and stop the form from closing. Otherwise, I
will allow it to close. I can do my form validation in the closing event,
but I have no immediate way of determining if the user is trying to submit
the form or is cancelling it. It seems to me that this is what the
Validating event should be for, but I can't get it to fire.

Charles
 
C

Charles Law

Hi C J

It may all be tied up with the same, I'm not sure. I think my problem is
more fundamental than that though. I have just replied to Cor regarding the
failure of the Validating event to fire, which seems to be a more basic
problem.

I have also just seen Herfried's answer so I am going to try that.

Thanks.

Charles
 
C

Cor Ligthert

Hi Charles,
I can do my form validation in the closing event,
but I have no immediate way of determining if the user is trying to submit
the form or is cancelling it. It seems to me that this is what the
Validating event should be for, but I can't get it to fire.

Are you sure it is WinForms and when so, what is a submit of a form?

Or is it something as a dialogbox?

Cor
 
C

Charles Law

Hi Cor

When I say 'submit' I don't mean that this is a web form, only that the
intention of the user is that the contents of the form should be committed.
I realise that 'submit' has a particular significance for web forms, but I
just mean it in its more general sense.

Charles
 
C

Cor Ligthert

Hi Charles,

From the other messages I understood that, however the events as lostfocus
does also not work, and than when I was busy with that I realized me
something what Armin once told to me about focus on a button. I think that
he can bring us back light again, I set his name in the subject, I think he
can tell us.

On the other hand do I not understand why you want to use the validate of a
form for that.
(I thought that the dialogform has a bug in the close event with the
closebox, however that is easy to pass by hiding that on the dialoform,
nobody knows why you did that. However I thought for that Armin was also
the man)

:))

Cor
 
C

Charles Law

Thanks Cor

I have followed Herfried's suggestion and now validate in the button click
event. However, I am still curious why the Validating event does not fire.
Perhaps Armin can tell us.

Charles
 
A

Armin Zingler

Cor Ligthert said:
Hi Charles,

From the other messages I understood that, however the events as
lostfocus does also not work, and than when I was busy with that I
realized me something what Armin once told to me about focus on a
button. I think that he can bring us back light again, I set his name
in the subject, I think he can tell us.

On the other hand do I not understand why you want to use the
validate of a form for that.
(I thought that the dialogform has a bug in the close event with
the closebox, however that is easy to pass by hiding that on the
dialoform, nobody knows why you did that. However I thought for that
Armin was also the man)

:))

Cor

I haven't used it so far, so....

I think the Validating /event/ for a form doesn't make sense. A control is
validated when the Container is a ContainerControl. The Containercontrol
knows which control was the last unvalidated control and does the
validation. As a Form is not the child of a ContainerControl, the validating
event doesn't fire. Above all, the Form doesn't loose the focus. Despite,
you could call the Validate method for the Form, e.g. in the Closing event:

sub .. closing
if me.validate then
'do stuff (like saving or whatever)
else
e.cancel = true
end if
end sub

just my 2c
 
H

Herfried K. Wagner [MVP]

* "Charles Law said:
I have followed Herfried's suggestion and now validate in the button click
event. However, I am still curious why the Validating event does not fire.

Are you sure the button's 'CausesValidation' property is set to 'True'?
 
H

Herfried K. Wagner [MVP]

* "Charles Law said:
Yes, it is.

I have played around with it a lot and I was not able to repro your
problem. 'Validating' is always firing as expected and the form doesn't
close if I set 'e.Cancel' to 'True' in the 'Validating' event handler.
 
A

Armin Zingler

H

Herfried K. Wagner [MVP]

* "Armin Zingler said:
You're saying the the Validating event of the Form fires? When does this
happen? I never caught this event from a Form.

Ops. I never caught that too, I installed a handler for the controls'
'Validating' event ;-).
 
C

Cor Ligthert

Hi Armin,
I haven't used it so far, so....

I think the Validating /event/ for a form doesn't make sense. A control is
validated when the Container is a ContainerControl. The Containercontrol
knows which control was the last unvalidated control and does the
validation. As a Form is not the child of a ContainerControl, the validating
event doesn't fire. Above all, the Form doesn't loose the focus. Despite,
you could call the Validate method for the Form, e.g. in the Closing event:

That was what I meant that you know it.

Thanks

Cor
 

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