Results 1 to 5 of 5

Thread: [RESOLVED] Is the mouse click in a form's non-client area?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2013
    Location
    San Francisco, CA
    Posts
    470

    Resolved [RESOLVED] Is the mouse click in a form's non-client area?

    My form has a textbox and the textbox has a Validating event handler. The form also has a ControlBox that includes the "X" icon to close the form.

    The Validating event fires when I click the "X" icon to close the form. If inside the Validating event the Cancel property of the CancelEventArgs argument is set to true, the Validating event also cancels the form's close operation.

    As a workaround for this problem, in the Validating event I would like to set the Cancel property of the CancelEventArgs argument to true only if the mouse is in the form's client area. Since the form's close button is in the title bar that is outside the client area of the form, when I user click the form's close button, the Validating event's Cancel property is not set to true and the form's close operation should then occur.

    How do I detect if the mouse is in the form's non-client area when the "X" icon is clicked?

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    109,268

    Re: Is the mouse click in a form's non-client area?

    Before doing that, I'd check whether you can set the CausesValidation property of the form to False, in which case the Validating event would not be raised at all. If that doesn't work, I'd then check whether the FormClosing event is raised before the Validating event, which would enable you to set a flag to you could check in the Validating event handler.

    If you do have to go with what you suggest, I think that it should be something like the following:
    vb.net Code:
    1. If RectangleToScreen(ClientRectangle).Contains(Control.MousePosition) Then
    2.     'The mouse pointer is within the client area of the form.
    3. End If
    I can't test that right now.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2013
    Location
    San Francisco, CA
    Posts
    470

    Re: Is the mouse click in a form's non-client area?

    jmc -

    I tried your suggestion. The code snippet below is in a TextBox control's Validating event handler.

    Code:
    If RectangleToScreen(ClientRectangle).Contains(MousePosition) Then
        '...the mouse pointer is within the client area of the form
        '...perform validation
    Else
        '...the mouse pointer is within the non-client area of the form
        '   the form's ControlBox in the non-client area of the form and has the "X" icon to close the form
        '...ignore validation   
    End If
    The Else clause in above code executes when the user clicks one of the BindingNavigator's navigation buttons (MoveFirst, MoveLast, etc.), the BindingNavigator.PositionItem textbox, or the form's ControlBox ("X" icon).

    When the user clicks one of the BindingNavigator's navigation buttons, I would like to validate the user's data entry. So I'll keep hunting for a different approach.

    See also Post #3 of the following thread for more detail/context around this question.

    https://www.vbforums.com/showthread....EndEdit-method
    Last edited by Mark@SF; Dec 3rd, 2022 at 10:19 AM.

  4. #4
    Junior Member
    Join Date
    Oct 2017
    Posts
    17

    Re: Is the mouse click in a form's non-client area?

    Quote Originally Posted by Mark@SF View Post
    How do I detect if the mouse is in the form's non-client area when the "X" icon is clicked?
    I wouldn't mess with the mouse pointer position.
    What if the user tries to close the form with [ALT]+[F4] while the mouse pointer is inside the client area? - Just to name one scenario where mouse pointer position is not helpful.

    Quote Originally Posted by Mark@SF View Post
    As a workaround for this problem, in the Validating event I would like to set the Cancel property of the CancelEventArgs argument to true only if the mouse is in the form's client area.
    What about handling the FormClosing event and setting Cancel=False in there?

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2013
    Location
    San Francisco, CA
    Posts
    470

    Re: Is the mouse click in a form's non-client area?

    jmc and sonic -

    Thanks. I'll go with the FormClosing event.

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