Data Validation

G

Guest

Hi there

I am trying to implement very simple field data validation:

I have a form with one TextBox (textBox1) on it. To validate user input I implemented Validating event handler (usual stuff)

private void textBox1_Validating(object sender, System.ComponentModel.CancelEventArgs e)
{
string error = null;
if (textBox1.Text == String.Empty)
{
e.Cancel = true;
error = "need text here";
}
errorProvider.SetError((Control)sender, error);
}

My problem is that when I click on form’s control box close button I don’t want any validation to happen as I’m going to close the form.

If I had my own “Close†button on this form I could set it’s CausesValidaton property to false and the problem would be solved.

Are there any properties of the form that I can set to avoid validation? Any suggestions?

Thanx
 
C

ClayB [Syncfusion]

This FAQ shows one way you can distinguish a click on the close box from
other types of closing. It sets a flag that you could test in your
validation routine to know whether or not to do the validation.

George Shepherd's Windows Forms FAQ contains an entry entitled:

How can I tell if a form is closed from the controlbox (system menu) or
from a call to Form.Close?

Check it out at:
http://www.syncfusion.com/faq/winforms/search/862.asp

=============================================
Clay Burch, .NET MVP

Syncfusion, Inc.
visit http://www.syncfusion.com for .NET Essentials


BorisDotNet said:
Hi there

I am trying to implement very simple field data validation:

I have a form with one TextBox (textBox1) on it. To validate user input I
implemented Validating event handler (usual stuff)
private void textBox1_Validating(object sender,
System.ComponentModel.CancelEventArgs e)
{
string error = null;
if (textBox1.Text == String.Empty)
{
e.Cancel = true;
error = "need text here";
}
errorProvider.SetError((Control)sender, error);
}

My problem is that when I click on form's control box close button I don't
want any validation to happen as I'm going to close the form.
If I had my own "Close" button on this form I could set it's
CausesValidaton property to false and the problem would be solved.
 
G

Guest

Thanks a lot - this is it

ClayB said:
This FAQ shows one way you can distinguish a click on the close box from
other types of closing. It sets a flag that you could test in your
validation routine to know whether or not to do the validation.

George Shepherd's Windows Forms FAQ contains an entry entitled:

How can I tell if a form is closed from the controlbox (system menu) or
from a call to Form.Close?

Check it out at:
http://www.syncfusion.com/faq/winforms/search/862.asp

=============================================
Clay Burch, .NET MVP

Syncfusion, Inc.
visit http://www.syncfusion.com for .NET Essentials



implemented Validating event handler (usual stuff)
System.ComponentModel.CancelEventArgs e)
want any validation to happen as I'm going to close the form.
CausesValidaton property to false and the problem would be solved.
 

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