Problem:
"...Validating Event and Sticky Forms the Validating event also fires when you close
a form. If you set the Cancel property of the CancelEventArgs object to true inside
this event, it will cancel the close operation as well. "..
(Quoted from:
http://www.examcram2.com/articles/ar...30936&seqNum=6)
The closing operation can be conducted in one of three ways:
Either 1) Clicking a cancel button, 2) Window close button or 3) Escape key.
Solution:
We now show how to stop the validation from occuring in case of canceling the operation. We
refer to each of the possible operations specified above.
1. Cancel Button:
Simply set the CausesValidation of the cancel button to false . Thus, every control that
is assigned to a validating event will not be called.
2. Window close button:
Need to override the wndProc method, check the window msg and if the last equals WN_CLOSE, and define it somewhere (it is not automatically defined) ,
set the CausesValidation of the active control to false.
e.g.
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_CLOSE)
{
ActiveControl.CausesValidation = false;
}
base.wndProc (m);
}
3. Escape key:
Derive a class from System.Windows.Forms.Button where you override the IButtonControl.PerformClick
method.
e.g.
public class CancelButton : Button, IButtonControl
{
void IButtonControl.PerformClick()
{
this.Parent.ActiveControl.CausesValidation = false;
base.PerformClick();
}
}
and use it as your form's cancel button
This about it. Do hope was of help.