Steve98796

I have a form with a text control on it.

I am handling the Validating event in C#.

If I tab or click on another control, my validating code is called ok.

I also understood that if the form was closed, the validating event was fired and my validating code would be called.

However, this is not happening and if i enter some text into the control and click the forms X to close it, the validating event does not seem to be fired.

Have I misunderstood this or do I need to do something to make this happen

Thanks

Steve

Edit:

Update

I am displaying the form using myform.ShowDialog();

If I test this with Application.Run(new myform()) , it works but in this case the form is not my main form so i dont want this.



Re: Windows Forms General Validating event when closing form

Koray Samsun

Event occurs after textbox loose focus. So if you select another control lets say a button event is fired. But after that if you close no event fired. Use formClosing event to validate on close

void Form1_FormClosing(object sender, FormClosingEventArgs e)

{

//this is also works but a bad way

//CancelEventArgs c = new CancelEventArgs(false);

//textBox1_Validating(null, c);

textBox1.Focus();// let it get focus

button1.Focus(); // and than loose focus

}

Note that infact you dont need to validate on close, because textbox got no focus, so nothing changed, no need to revalidate. Anyway if you want the code above will help you





Re: Windows Forms General Validating event when closing form

Steve98796

Koray

Thanks for the reply but i think you have misunderstood my problem.

I only mentioned the button to prove my validating code was called when my textbox lost the focus by me clicking the button. The button is irrelevant to the problem really.

If the textbox has the focus and I close the form, the Validating event is supposed to get fired but it does not. This is my problem.

This seems to be when the form is shown using myform.ShowDialog(); If the form is shown using Application.Run(new myform()) then all is as expected.

However this is not may main form and I need to show it using ShowDialog().

Why under this case does the Validating event not fire when a textbox has the focus but the form is closed.

Thanks

Steve





Re: Windows Forms General Validating event when closing form

Koray Samsun

Yes i see that too. If the form is modal then none of the controls in it does not leave focus on form close.( Implemented textBox.Leave event) So 'no leave' means still have focus for textbox and validating event does not fired.

Really confusing, you can still give and take focus to textbox in closing event to manually fire validate.





Re: Windows Forms General Validating event when closing form

nobugz

Here's a workaround. It is not quite the same thing and may have side effects:

private void Form2_FormClosing(object sender, FormClosingEventArgs e) {
bool ok = this.ValidateChildren();
e.Cancel = !ok;
}






Re: Windows Forms General Validating event when closing form

Steve98796

Thanks for the replies.