Results 1 to 5 of 5

Thread: Overriding Control Validation on Windows Forms in the Closing Event

  1. #1
    brainleak is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.5
    Join Date
    Dec 2016
    Posts
    27
    Reputation
    7

    Overriding Control Validation on Windows Forms in the Closing Event

    The thread's tile is precisely that of Microsoft's documentation site:

    https://msdn.microsoft.com/en-us/lib...(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?

  2. #2
    jmcilhinney's Avatar
    jmcilhinney is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    13,813
    Reputation
    1707
    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.

  3. #3
    brainleak is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.5
    Join Date
    Dec 2016
    Posts
    27
    Reputation
    7
    Quote Originally Posted by jmcilhinney View Post
    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.

  4. #4
    brainleak is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.5
    Join Date
    Dec 2016
    Posts
    27
    Reputation
    7
    Just solved the issue by setting the form's AutoValidate property to EnableAllowFocusChange.

    EDIT: delete delete delete...! It definitely doesn't work either.

  5. #5
    brainleak is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.5
    Join Date
    Dec 2016
    Posts
    27
    Reputation
    7
    Finally, here I've found this perfectly working code:

    Code:
    ProtectedOverridesSub WndProc(ByRef m As System.Windows.Forms.Message)
        If m.WParam.ToInt32 =&HF060ThenMe.AutoValidate = System.Windows.Forms.AutoValidate.Disable
        MyBase.WndProc(m)
    EndSub

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •