Results 1 to 8 of 8

Thread: cancel validation of txtbox if form close btn(x) is pressed

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,158

    cancel validation of txtbox if form close btn(x) is pressed

    Is there an way to supress the validating event of textbox when the forms close button(X) is pressed.

    In the textbox validating event , if the condition fails then i am displaying an messagebox. Now suppose if the textbox has the focus and user directly presses the forms X button , then the messagebox is displayed. I want to supress this.

    Plz Help!

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: cancel validation of txtbox if form close btn(x) is pressed

    What you describe is not the default behaviour. The only times that the Validating event is raised is when the user attempts to shift focus from the control or when the code calls Validate or ValidateChildren. Clicking the Close button on the form's title bar does not shift focus from the active control so, unless there's something broken on your system, you must be calling Validate or ValidateChildren on the FormClosing or FormClosed event. You don't have to suppress anything; you just have to not initiate it.

    If you don't it's your code then try it with a brand new project where that's the only functionality and see how that behaves.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,158

    Re: cancel validation of txtbox if form close btn(x) is pressed

    Well infact it behaves.

    i have created a new project and all it contains is an textbox and the following code

    vb Code:
    1. Private Sub TextBox1_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
    2.         If Not IsNumeric(Me.TextBox1.Text) Then
    3.             MessageBox.Show("Invalid")
    4.             e.Cancel = True
    5.         Else
    6.             e.Cancel = False
    7.  
    8.         End If

    when the project runs the textbox has the control, i entered some non numeric string in it an pressed the X button, i got the msgbox

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: cancel validation of txtbox if form close btn(x) is pressed

    In my original test I displayed the form by calling ShowDialog. I just tested again calling ShowDialog and Show. When I called Show it did behave as you describe. When you close a form displayed with ShowDialog, you are actually hiding it. When you close a form displayed with Show, you are actually closing it. In that case, the form's Validate method gets called and the Validating event will be raised.

    I just had a quick look and couldn't see an obvious check to make in the Validating event. A more thorough examination may reveal more but the only thing I can see for the moment is trapping the WM_CLOSE message in the WndProc method and setting a flag. I guess the first thing to ask is whether or not this form is your startup form and, if it's not, is it practical to display it by calling ShowDialog?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,158

    Re: cancel validation of txtbox if form close btn(x) is pressed

    Thanks and Yes its not my main form and i can manage with showdialog.

    but for the brain size sake
    Quote Originally Posted by jmcilhinney View Post
    When you close a form displayed with Show, you are actually closing it. In that case, the form's Validate method gets called and the Validating event will be raised.
    what happens in the forms validating event that makes the textbox to raise its validating event.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: cancel validation of txtbox if form close btn(x) is pressed

    It's not the form's Validating event. The form's Validate method is called from, if I remember correctly, the WndProc method, which would occur when the WM_CLOSE message is received. I'm not at a computer with VS installed right now so I can't check but you can set a breakpoint in the TextBox's Validating event handler and check the Call Stack window yourself.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7
    New Member
    Join Date
    Apr 2010
    Posts
    2

    Re: cancel validation of txtbox if form close btn(x) is pressed

    That's an interesting thread, I had a very similar problem. While it could be solved for the X-button, I still haven't found any solution for an additional Cancel-Button:

    Clicking the Close button on the form's title bar does not shift focus from the active control
    Any other button, however, does. Meaning, if I enter a textbox which has validation implemented, clicking the Cancel-Button (the one I added, not the X-button) won't do anything, because the focus is stuck to the textbox. Is there any solution to that? How can you make your custom button behave like the x-button?

    Thank you very much for any hints!

    - spitfire

  8. #8
    New Member
    Join Date
    Apr 2010
    Posts
    2

    Re: cancel validation of txtbox if form close btn(x) is pressed

    I just stumbled over a solution by myself: Simply set the CausesValidation-Property of the button to false, and it can do its thing

Posting Permissions

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



Click Here to Expand Forum to Full Width