Overriding Control Validation on Windows Forms in the Closing Event

brainleak

Well-known member
Joined
Dec 20, 2016
Messages
54
Location
Barcelona area
Programming Experience
3-5
The thread's tile is precisely that of Microsoft's documentation site:

https://msdn.microsoft.com/en-us/library/4y44hzat(v=vs.71).aspx

I've tried out the stated example and when TextBox1.Text.Length is less than 8, I have included a warning message for display.
The inconvenience is that this message is shown when I click on the form's control box to close it. Is this unavoidable?
 
In such situations, you should be providing a Cancel button or the like that you can set the CausesValidation property of to False. The user can then close the form using that button without raising Validating events.

By the way, your link is for documentation for VS.NET 2003. You should endeavour to use documentation for the version you're using. For one thing, the Closing event is obsolete now and you should handle FormClosing instead.
 
In such situations, you should be providing a Cancel button or the like that you can set the CausesValidation property of to False. The user can then close the form using that button without raising Validating events.
The thing is the form has its own CausesValidation property which I tried to set to False but didn't work.
Now I've tried with a button and it's the same thing, it closes the application as clicking on the control box does only after the error message has been displayed.
 
Finally, here I've found this perfectly working code:

VB.NET:
[COLOR=#101094]Protected[/COLOR][COLOR=#101094]Overrides[/COLOR][COLOR=#101094]Sub[/COLOR][COLOR=#303336] WndProc[/COLOR][COLOR=#303336]([/COLOR][COLOR=#101094]ByRef[/COLOR][COLOR=#303336] m [/COLOR][COLOR=#101094]As[/COLOR][COLOR=#303336] System[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]Windows[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]Forms[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]Message[/COLOR][COLOR=#303336])[/COLOR][COLOR=#303336]
    [/COLOR][COLOR=#101094]If[/COLOR][COLOR=#303336] m[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]WParam[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]ToInt32 [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#7D2727]&HF060[/COLOR][COLOR=#101094]Then[/COLOR][COLOR=#101094]Me[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]AutoValidate [/COLOR][COLOR=#303336]=[/COLOR][COLOR=#303336] System[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]Windows[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]Forms[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]AutoValidate[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]Disable
    [/COLOR][COLOR=#101094]MyBase[/COLOR][COLOR=#303336].[/COLOR][COLOR=#303336]WndProc[/COLOR][COLOR=#303336]([/COLOR][COLOR=#303336]m[/COLOR][COLOR=#303336])[/COLOR][COLOR=#303336]
[/COLOR][COLOR=#101094]End[/COLOR][COLOR=#101094]Sub[/COLOR]
 
Back
Top