Hi,
In windows application(c#) I am having a form which contains two textboxes.
For one textbox I have created validate event.like below
private
void txtProposedNextDueDate_Validating(object sender, CancelEventArgs e){
if (txtProposedNextDueDate.Text == ""){
MessageBox.Show("Please enter valid date"); txtProposedNextDueDate.Focus();}
}
But when I want to close the form without entering any value,then also it is showing the messagebox and then it is closing the form.
So when I close the form I dont want to validate the textbox.Simply it should close the form without showing message.
Niradhip ChakrabortyPosted May 5, 2009, 1:23 AM
jalajakshi pPosted May 5, 2009, 12:52 AM
Niradhip ChakrabortyPosted May 4, 2009, 10:30 AM
Also you can validate the textbox on "onkeypress" event or
in the submit button function before submit the data validate it.
Niradhip ChakrabortyPosted May 4, 2009, 10:06 AM
Validation is caused by the control receiving focus and not by the control losing it. The CausesValidation property determines if a control will cause the control losing focus to validate or not. Set this to false on your close button to avoid validation. However a word of caution. Validation will still occur if the user hits ENTER/ESC or clicks the close button in the caption. There is no easy way to prevent this validation. The new properties added in v2.0 like AutoValidate help somewhat but they add complications elsewhere. Do not prevent the focus change in any case to allow the user to interact with other controls. When the confirmation button (OK, Save, etc) is clicked validate all your child controls again (or alternatively just look for an error provider with an error set). Display a message and block the button. For cancel you don't do any validation so the form will go away.
To make it more maintainable you should probably create one validation routine for each control. This method accepts a boolean parameter specifying whether to display an error dialog or not. In all cases it sets or clears the error provider after validation. This method can be invoked in the validating handler for your control and in the confirmation button. v2.0 added the ValidateChildren method where you could put all the validation calls. This method is not called by the framework but you can easily call it in your form's validation or closing events.