CausesValidation CausesProblems After searching through many lists for. With the introduction of. When the OK button is clicked, call Validate to fire all the validating event handlers for the form; call ValidateChildren if the form has container controls that have controls with validating events. Issue 1 According to the. NET documentation, if a button has the CausesValidation property set to false then validation events should not fire. This is not correct if a form is opened using Show instead of ShowDialog.
In this case, the button's event handler is called first, then when the handler calls the Close method, the validation events fire. Solution In this case it is possible to fix the error because after the validation events fire, the Closing event fires. The closing event receives the same EventArgs as the validating events, so the solution is to set e. Issue 2 According to the. NET documentation, if a button has its DialogResult set to Cancel and the form has the CancelButton set to this button, then when the user presses the ESC key, the button's event handler should fire.
If there is a control that has a validating event and the control has invalid data, then the validating event handler will be called before the cancel button's event handler is called and will prevent the form from closing. Apparently, the validation event fires twice if the form itself has CausesValidation set to true. Solution In this case, it is necessary to override the ProcessDialogKey and intercept the ESC key and call the event handler for the button directly.
Issue 2 deals with the ESC key, but the same thing happens if the Close button the X on the control bar is clicked. However, in this case, the Closing event will be fired, so the first solution will also fix that problem. However, if more control is needed, the following code can be used to intercept the key press of the Close on the control bar.
There is a choice to be made concerning the call to the base class WndProc. Do not call it if you plan to handle the event entirely yourself. In this case, turn off all CausesValidation properties and define a new delegate. Define a Delegate Define a new delegate that is global because it defines a type.
Declare it in the namespace, but outside of any class definitions. A delegate defines a way for classes to cummunicate. One class declares an instance of a delegate, while another registers methods with the declared delegate. The definition of the delegate specifies the signature of all the methods that can be registered with an instance of the delegate.
You can also use Visual Studio to auto-generate the ValidateControl methods. Instantiate the Delegate with Handlers Instantiate the delegate with new delagates that are constructed with the name of the validating handlers. The handler must have the signature that was defined by the delegate. Think of this as a linked list and that each new object is added to the end of the previous delegate in the list.
There are two ways to do this. If you want to be able to stop executing the handlers at the first error, then you will need to loop through each event and test args. This is the technique that is used by the default CausesValidation process. Cancel to see if there was success or failure If you want to test them all, then you can fire the delegate directly and it will call each event in turn.
Each event will receive the CancelEventArgs that you send to the delegate. The same arguments will be passed in turn to each handler. Cancel to see if there was success or failure Zip file of a project that exhibits these errors and solutions, except for overriding WndProc.