Monday, June 09, 2003 | |
Validation and Form Close Ran into a weird one today. We have a windows forms application that uses the validating event on it's controls. When the user clicks the close box (red x) in the corner we want to prompt whether they would like to save and then close the form. The validators cause problems with this. Clicking on the close box causes validation to run. First blush you think no problem, I will set CausesValidation on the form to false. Whoa! The Validating event still fires. It appears that the close box always causes validation. Next you think I will override OnClosing() and ask whether they want to save before calling the base implementation thus only allowing the Validating events to fire if they are saving. Wrong again. Validating fires before OnClosing! So here is the hack I came up with. Override WndProc in the form and look for the WM_CLOSE message. If you receive it then set a protected member on the form to indicate it is currently closing. I have this hidden in a base class that exposes the member as a read only property. Here is some sample code: using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data;
Wow that is ugly! If anyone knows of a good C# -> HTML converter that works with Radio send me a pointer at mailto:ckinsman@vergentsoftware.com. This is from http://www.manoli.net/csharpformat/ 2:58:19 PM |