Validating textbox w/ Cancel button

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Darin

    #1

    Validating textbox w/ Cancel button

    I have a form w/ a textbox and Cancel button on it. I have a routine to
    handle textbox.validat ing, and I have the form setup so the Cancel
    button is the Cancel button.

    WHen the user clicks on the cancel button, the textbox.validat ing is
    being called. I don't want it to be since they are exiting the screen
    the validation doesn't have to be done.

    How can I do that.

    Thanks.

    Darin

    *** Sent via Developersdex http://www.developersdex.com ***
  • iwdu15

    #2
    RE: Validating textbox w/ Cancel button

    if you want to exit the form when the user presses cancel then just say, in
    the btnCancel click event

    Me.Close()

    and the form will close
    --
    -iwdu15

    Comment

    • Darin

      #3
      RE: Validating textbox w/ Cancel button

      I know that, but the problem is when the user clicks EXIT, the
      textbox.validat ing event is called BEFORE the cancel.click is done, so
      the textbox is being validated. It doesn't need to be validated because
      the user is exiting.

      Darin

      *** Sent via Developersdex http://www.developersdex.com ***

      Comment

      • Terry

        #4
        RE: Validating textbox w/ Cancel button

        Yes, closing the form causes 'validating' to take place. One, remove the
        "controlBox " from the form, so the user cant close it that way. Then in the
        btnCancel_click event, either set a module level variable like mCanceling to
        True and then in the validating event ... If not mCanceling ...
        Another approach. in the btnCancel_Click event:
        Removehandeler TextBox1.valida ting, Addressof TextBox1_Valida ting
        --
        Terry


        "Darin" wrote:
        I know that, but the problem is when the user clicks EXIT, the
        textbox.validat ing event is called BEFORE the cancel.click is done, so
        the textbox is being validated. It doesn't need to be validated because
        the user is exiting.
        >
        Darin
        >
        *** Sent via Developersdex http://www.developersdex.com ***
        >

        Comment

        • Al Reid

          #5
          Re: Validating textbox w/ Cancel button

          "Terry" <Terry@nospam.n ospamwrote in message news:9059256D-E5C4-41D7-B702-667305E23EA4@mi crosoft.com...
          Yes, closing the form causes 'validating' to take place. One, remove the
          "controlBox " from the form, so the user cant close it that way. Then in the
          btnCancel_click event, either set a module level variable like mCanceling to
          True and then in the validating event ... If not mCanceling ...
          Another approach. in the btnCancel_Click event:
          Removehandeler TextBox1.valida ting, Addressof TextBox1_Valida ting
          --
          Terry
          >
          >
          That doesn't work either, at least not for VB2005. I've been fighting the same problem for some time now. When you click on the
          button the validating event fires. If you set e.Cancel=True the button events never fire.

          I think that the only way to do this is to grab the mouse coordinates in the validating event, do a WindowFromPoint API call, then
          compare the returned HWND to the Cancel Button's Handle. If the handle matched then immediately exit the Validating event without
          setting e.Cancel = True.

          --
          Al Reid


          Comment

          • Terry

            #6
            Re: Validating textbox w/ Cancel button

            Well it works for me - but one thing I left out - you need to set the cancel
            buttons 'causes validation' to false. The following code works as you might
            hope:
            Public Class Form1
            Private mCanceling As Boolean = False

            Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
            System.EventArg s) Handles Button1.Click
            mCanceling = True
            Me.Close()
            End Sub

            Private Sub TextBox1_Valida ting(ByVal sender As Object, ByVal e As
            System.Componen tModel.CancelEv entArgs) Handles TextBox1.Valida ting
            If Not mCanceling Then
            If TextBox1.Text.L ength < 5 Then
            e.Cancel = True
            End If
            End If
            End Sub
            End Class
            The removehandeler method also works.

            Also, as I just discovered, if you set the text box's causesvalidatio n to
            false, you can close the form from the control box without validation taking
            place. This really sounds more like a bug to me.

            --


            Terry


            "Al Reid" wrote:
            "Terry" <Terry@nospam.n ospamwrote in message news:9059256D-E5C4-41D7-B702-667305E23EA4@mi crosoft.com...
            Yes, closing the form causes 'validating' to take place. One, remove the
            "controlBox " from the form, so the user cant close it that way. Then in the
            btnCancel_click event, either set a module level variable like mCanceling to
            True and then in the validating event ... If not mCanceling ...
            Another approach. in the btnCancel_Click event:
            Removehandeler TextBox1.valida ting, Addressof TextBox1_Valida ting
            --
            Terry
            >
            That doesn't work either, at least not for VB2005. I've been fighting the same problem for some time now. When you click on the
            button the validating event fires. If you set e.Cancel=True the button events never fire.
            >
            I think that the only way to do this is to grab the mouse coordinates in the validating event, do a WindowFromPoint API call, then
            compare the returned HWND to the Cancel Button's Handle. If the handle matched then immediately exit the Validating event without
            setting e.Cancel = True.
            >
            --
            Al Reid
            >
            >
            >

            Comment

            • Al Reid

              #7
              Re: Validating textbox w/ Cancel button

              I had tried all of that, *BUT*, I had neglected to set the button's CausesValidatio n to False. DUH!!!
              It does indeed work. I also got the WindowFromPoint approach working as well. Now I can go back and strip that out.

              Thanks.

              --
              Al Reid (who didn't mean to hijack the thread)


              "Terry" <Terry@nospam.n ospamwrote in message news:70E9A54F-85F3-49D4-AB1E-6EAFC54FCCE9@mi crosoft.com...
              Well it works for me - but one thing I left out - you need to set the cancel
              buttons 'causes validation' to false. The following code works as you might
              hope:
              Public Class Form1
              Private mCanceling As Boolean = False
              >
              Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
              System.EventArg s) Handles Button1.Click
              mCanceling = True
              Me.Close()
              End Sub
              >
              Private Sub TextBox1_Valida ting(ByVal sender As Object, ByVal e As
              System.Componen tModel.CancelEv entArgs) Handles TextBox1.Valida ting
              If Not mCanceling Then
              If TextBox1.Text.L ength < 5 Then
              e.Cancel = True
              End If
              End If
              End Sub
              End Class
              The removehandeler method also works.
              >
              Also, as I just discovered, if you set the text box's causesvalidatio n to
              false, you can close the form from the control box without validation taking
              place. This really sounds more like a bug to me.
              >
              --
              >
              >
              Terry
              >
              >

              Comment

              • Darin

                #8
                Re: Validating textbox w/ Cancel button

                That doesn't work for me. I am on VB.Net, VS 2003. When I click on the
                cancel button it doesn't even get to the cancel.click event until AFTER
                it has done the validating.

                Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
                System.EventArg s) Handles Button1.Click
                MessageBox.Show ("Button1_click ")
                mCanceling = True
                Me.Close()
                End Sub

                Private Sub TextBox1_Valida ting(ByVal sender As Object, ByVal e As
                System.Componen tModel.CancelEv entArgs) Handles TextBox1.Valida ting
                If Not mCanceling Then
                If TextBox1.Text.L ength < 5 Then
                e.Cancel = True
                End If
                End If
                End Sub

                Run the program (make sure the tab stop has the textbox first then the
                button), leave the text in textbox to be textbox, click on button, you
                will see the message. Run it again, clear out the text in textbox, click
                on cancel and you will not see the message - the system is not honoring
                the cancel.click because (I guess) the e.cancel=true in _validating.


                Darin

                *** Sent via Developersdex http://www.developersdex.com ***

                Comment

                • Al Reid

                  #9
                  Re: Validating textbox w/ Cancel button

                  Just a follow-up. The thing that this approach does not allow (unless I've one again neglected the obvious) is using the escape key
                  to trigger the form cancel. If I hit the escape, the validation event fires and since the mCanceling is not True, the validation
                  error gets displayed before the form closes.

                  My final solution, unless someone has a better suggestion, is to change the Validating event to the Leave event and detecting
                  whether the cancel button had been clicked in the Leave event and exiting if it was. This way both the escape key and the cancel
                  button closes the form without causing the validation to occur.

                  The code to tell if the cancel button has been clicked is:

                  ///////

                  Public Declare Function WindowFromPoint Lib "user32" (ByVal p As POINTAPI) As IntPtr

                  Public Structure POINTAPI

                  Dim X As Integer

                  Dim Y As Integer

                  End Structure



                  Private Function CancelButtonCli cked() As Boolean

                  Dim p As New POINTAPI

                  p.X = Form.MousePosit ion.X

                  p.Y = Form.MousePosit ion.Y

                  Dim HWnd As IntPtr = WindowFromPoint (p)

                  If HWnd = CType(Me.Cancel Button, Button).Handle Then

                  Return True

                  Else

                  Return False

                  End If

                  End Function

                  \\\\\\\\\\\\

                  --
                  Al Reid

                  "Al Reid" <areidjr@reidDA SHhome.comwrote in message news:uAhLRUppGH A.2328@TK2MSFTN GP05.phx.gbl...
                  I had tried all of that, *BUT*, I had neglected to set the button's CausesValidatio n to False. DUH!!!
                  It does indeed work. I also got the WindowFromPoint approach working as well. Now I can go back and strip that out.
                  >
                  Thanks.
                  >
                  --
                  Al Reid (who didn't mean to hijack the thread)
                  >
                  >
                  "Terry" <Terry@nospam.n ospamwrote in message news:70E9A54F-85F3-49D4-AB1E-6EAFC54FCCE9@mi crosoft.com...
                  Well it works for me - but one thing I left out - you need to set the cancel
                  buttons 'causes validation' to false. The following code works as you might
                  hope:
                  Public Class Form1
                  Private mCanceling As Boolean = False

                  Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
                  System.EventArg s) Handles Button1.Click
                  mCanceling = True
                  Me.Close()
                  End Sub

                  Private Sub TextBox1_Valida ting(ByVal sender As Object, ByVal e As
                  System.Componen tModel.CancelEv entArgs) Handles TextBox1.Valida ting
                  If Not mCanceling Then
                  If TextBox1.Text.L ength < 5 Then
                  e.Cancel = True
                  End If
                  End If
                  End Sub
                  End Class
                  The removehandeler method also works.

                  Also, as I just discovered, if you set the text box's causesvalidatio n to
                  false, you can close the form from the control box without validation taking
                  place. This really sounds more like a bug to me.

                  --


                  Terry
                  >
                  >

                  Comment

                  • Terry

                    #10
                    Re: Validating textbox w/ Cancel button

                    Hi Al,
                    Hmmm, I just tried the 'escape' key and it works fine! The form cancels
                    and the 'validating' event is skipped. Sounds like you have something else
                    going on.
                    --
                    Terry


                    "Al Reid" wrote:
                    Just a follow-up. The thing that this approach does not allow (unless I've one again neglected the obvious) is using the escape key
                    to trigger the form cancel. If I hit the escape, the validation event fires and since the mCanceling is not True, the validation
                    error gets displayed before the form closes.
                    >
                    My final solution, unless someone has a better suggestion, is to change the Validating event to the Leave event and detecting
                    whether the cancel button had been clicked in the Leave event and exiting if it was. This way both the escape key and the cancel
                    button closes the form without causing the validation to occur.
                    >
                    The code to tell if the cancel button has been clicked is:
                    >
                    ///////
                    >
                    Public Declare Function WindowFromPoint Lib "user32" (ByVal p As POINTAPI) As IntPtr
                    >
                    Public Structure POINTAPI
                    >
                    Dim X As Integer
                    >
                    Dim Y As Integer
                    >
                    End Structure
                    >
                    >
                    >
                    Private Function CancelButtonCli cked() As Boolean
                    >
                    Dim p As New POINTAPI
                    >
                    p.X = Form.MousePosit ion.X
                    >
                    p.Y = Form.MousePosit ion.Y
                    >
                    Dim HWnd As IntPtr = WindowFromPoint (p)
                    >
                    If HWnd = CType(Me.Cancel Button, Button).Handle Then
                    >
                    Return True
                    >
                    Else
                    >
                    Return False
                    >
                    End If
                    >
                    End Function
                    >
                    \\\\\\\\\\\\
                    >
                    --
                    Al Reid
                    >
                    "Al Reid" <areidjr@reidDA SHhome.comwrote in message news:uAhLRUppGH A.2328@TK2MSFTN GP05.phx.gbl...
                    I had tried all of that, *BUT*, I had neglected to set the button's CausesValidatio n to False. DUH!!!
                    It does indeed work. I also got the WindowFromPoint approach working as well. Now I can go back and strip that out.

                    Thanks.

                    --
                    Al Reid (who didn't mean to hijack the thread)


                    "Terry" <Terry@nospam.n ospamwrote in message news:70E9A54F-85F3-49D4-AB1E-6EAFC54FCCE9@mi crosoft.com...
                    Well it works for me - but one thing I left out - you need to set the cancel
                    buttons 'causes validation' to false. The following code works as you might
                    hope:
                    Public Class Form1
                    Private mCanceling As Boolean = False
                    >
                    Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
                    System.EventArg s) Handles Button1.Click
                    mCanceling = True
                    Me.Close()
                    End Sub
                    >
                    Private Sub TextBox1_Valida ting(ByVal sender As Object, ByVal e As
                    System.Componen tModel.CancelEv entArgs) Handles TextBox1.Valida ting
                    If Not mCanceling Then
                    If TextBox1.Text.L ength < 5 Then
                    e.Cancel = True
                    End If
                    End If
                    End Sub
                    End Class
                    The removehandeler method also works.
                    >
                    Also, as I just discovered, if you set the text box's causesvalidatio n to
                    false, you can close the form from the control box without validation taking
                    place. This really sounds more like a bug to me.
                    >
                    --
                    >
                    >
                    Terry
                    >
                    >
                    >
                    >
                    >

                    Comment

                    • Terry

                      #11
                      Re: Validating textbox w/ Cancel button

                      Are you sure you have set the Cancel button's 'CausesValidati on' to False?
                      --
                      Terry


                      "Darin" wrote:
                      That doesn't work for me. I am on VB.Net, VS 2003. When I click on the
                      cancel button it doesn't even get to the cancel.click event until AFTER
                      it has done the validating.
                      >
                      Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
                      System.EventArg s) Handles Button1.Click
                      MessageBox.Show ("Button1_click ")
                      mCanceling = True
                      Me.Close()
                      End Sub
                      >
                      Private Sub TextBox1_Valida ting(ByVal sender As Object, ByVal e As
                      System.Componen tModel.CancelEv entArgs) Handles TextBox1.Valida ting
                      If Not mCanceling Then
                      If TextBox1.Text.L ength < 5 Then
                      e.Cancel = True
                      End If
                      End If
                      End Sub
                      >
                      Run the program (make sure the tab stop has the textbox first then the
                      button), leave the text in textbox to be textbox, click on button, you
                      will see the message. Run it again, clear out the text in textbox, click
                      on cancel and you will not see the message - the system is not honoring
                      the cancel.click because (I guess) the e.cancel=true in _validating.
                      >
                      >
                      Darin
                      >
                      *** Sent via Developersdex http://www.developersdex.com ***
                      >

                      Comment

                      • Al Reid

                        #12
                        Re: Validating textbox w/ Cancel button

                        I guess I'm going to have to start with a new project and try it. The
                        users have "required" several features such as navigation with the enter key
                        instead of tab and also that all of the text gets selected upon entering a
                        field, etc. The form's KeyPreview is set to True so that I can handle key
                        events on the form... Somewhere in all of that, something must be getting
                        crossed up.

                        I always get the weird ones. For example, I have another application that
                        uses a wedge type barcode reader. If I type the info and press enter it
                        works. If I use a wand type reader, it works. If I connect a gun type, the
                        program fails and the cursor jumps to a text box that has it's TabStop
                        property set to False. If I do a debug.Print in the Form's Keypress event
                        all three methods display the exact same key codes.

                        Oh well, that's what keeps it interesting.

                        --
                        Al Reid

                        "Terry" <Terry@nospam.n ospamwrote in message
                        news:EFEE22CF-A2CD-47BC-B07C-17D8D3B10904@mi crosoft.com...
                        Hi Al,
                        Hmmm, I just tried the 'escape' key and it works fine! The form cancels
                        and the 'validating' event is skipped. Sounds like you have something
                        else
                        going on.
                        --
                        Terry
                        >
                        >

                        Comment

                        • Terry

                          #13
                          Re: Validating textbox w/ Cancel button

                          Hi again Al,
                          On the barcode reader problem. Maybe you should be using the KeyDown event
                          instead of the KeyPress event. They don't receive the same things! here is
                          a line copied from the documentation: "The KeyPress event is not raised by
                          noncharacter keys; however, the noncharacter keys do raise the KeyDown and
                          KeyUp events." My guess is that the gun is sending some noncharacter data to
                          you. I am new to VB .net comming from VB6, but that was also the case in
                          VB6. Also in the docs on KeyDown there is a mention of "IsInputKey " which I
                          will have to look into. If you figure it out first, let me know.

                          --
                          Terry


                          "Al Reid" wrote:
                          I guess I'm going to have to start with a new project and try it. The
                          users have "required" several features such as navigation with the enter key
                          instead of tab and also that all of the text gets selected upon entering a
                          field, etc. The form's KeyPreview is set to True so that I can handle key
                          events on the form... Somewhere in all of that, something must be getting
                          crossed up.
                          >
                          I always get the weird ones. For example, I have another application that
                          uses a wedge type barcode reader. If I type the info and press enter it
                          works. If I use a wand type reader, it works. If I connect a gun type, the
                          program fails and the cursor jumps to a text box that has it's TabStop
                          property set to False. If I do a debug.Print in the Form's Keypress event
                          all three methods display the exact same key codes.
                          >
                          Oh well, that's what keeps it interesting.
                          >
                          --
                          Al Reid
                          >
                          "Terry" <Terry@nospam.n ospamwrote in message
                          news:EFEE22CF-A2CD-47BC-B07C-17D8D3B10904@mi crosoft.com...
                          Hi Al,
                          Hmmm, I just tried the 'escape' key and it works fine! The form cancels
                          and the 'validating' event is skipped. Sounds like you have something
                          else
                          going on.
                          --
                          Terry
                          >
                          >
                          >

                          Comment

                          • Al Reid

                            #14
                            Re: Validating textbox w/ Cancel button

                            Terry,

                            I guess we have a similar background. 26 years in computing and used VB
                            since Version 2.

                            I don't do anything with the barcode data in the KeyPress event. I use it
                            to catch and handle the "enter" key. I guess I could inspect the data in
                            the KeyDown event to see if there is a difference. I just don't know what
                            key code combination could direct focus to a text box control that should
                            never receive focus since the TabStop property is false and there is no
                            other code that sets focus to the textbox. The textbox is also ReadOnly.
                            It's frustrating!!!

                            --
                            Al Reid

                            "Terry" <Terry@nospam.n ospamwrote in message
                            news:785ACE72-1ED6-4D48-B6A6-EEEECB00E729@mi crosoft.com...
                            Hi again Al,
                            On the barcode reader problem. Maybe you should be using the KeyDown
                            event
                            instead of the KeyPress event. They don't receive the same things! here
                            is
                            a line copied from the documentation: "The KeyPress event is not raised by
                            noncharacter keys; however, the noncharacter keys do raise the KeyDown and
                            KeyUp events." My guess is that the gun is sending some noncharacter data
                            to
                            you. I am new to VB .net comming from VB6, but that was also the case in
                            VB6. Also in the docs on KeyDown there is a mention of "IsInputKey " which
                            I
                            will have to look into. If you figure it out first, let me know.
                            >
                            --
                            Terry
                            >
                            >
                            >>

                            Comment

                            • Terry

                              #15
                              Re: Validating textbox w/ Cancel button

                              Hi Al,
                              Nearly 40 years for me since my first college FORTRAN IV class! My, how
                              the years fly!
                              Well, one of the keys you would not see in either the keypress or keydown
                              events is the tab key, which of course will move the focus. Why it would
                              stop on a textbox with tabstop property set to False - I have no idea, unless
                              it is a behavior of the container its in. As far as the 'gun', it may be
                              better to write a console app. to read its output.
                              --
                              Terry


                              "Al Reid" wrote:
                              Terry,
                              >
                              I guess we have a similar background. 26 years in computing and used VB
                              since Version 2.
                              >
                              I don't do anything with the barcode data in the KeyPress event. I use it
                              to catch and handle the "enter" key. I guess I could inspect the data in
                              the KeyDown event to see if there is a difference. I just don't know what
                              key code combination could direct focus to a text box control that should
                              never receive focus since the TabStop property is false and there is no
                              other code that sets focus to the textbox. The textbox is also ReadOnly.
                              It's frustrating!!!
                              >
                              --
                              Al Reid
                              >
                              "Terry" <Terry@nospam.n ospamwrote in message
                              news:785ACE72-1ED6-4D48-B6A6-EEEECB00E729@mi crosoft.com...
                              Hi again Al,
                              On the barcode reader problem. Maybe you should be using the KeyDown
                              event
                              instead of the KeyPress event. They don't receive the same things! here
                              is
                              a line copied from the documentation: "The KeyPress event is not raised by
                              noncharacter keys; however, the noncharacter keys do raise the KeyDown and
                              KeyUp events." My guess is that the gun is sending some noncharacter data
                              to
                              you. I am new to VB .net comming from VB6, but that was also the case in
                              VB6. Also in the docs on KeyDown there is a mention of "IsInputKey " which
                              I
                              will have to look into. If you figure it out first, let me know.

                              --
                              Terry

                              >
                              >
                              >
                              >

                              Comment

                              Working...