Share via

Problem with MouseDown

Anonymous
2015-06-10T15:07:33+00:00

Hi everybody,

I have a column of text boxes on a form that are organized to pass the focus to another one in the same row as them when clicked on (no matter how strange that seems, even to me).

Private Sub ctrlTX_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)

  ' Code that sets the focus to another text box and make its name the current ActiveTx

  Target.Controls(CallGet(ActiveTx_Stub) & nmSufx).SetFocus

  Call CallLet(ActiveTx, CallGet(ActiveTx_Stub) & nmSufx)

End Sub

Note: "Target" is the frame containing the textboxes, "ActiveTx_Stub" represents the current active column on the form, "nmSufx" represents the row of the text box being clicked on and "CallLet" is a function within the class module that uses CallByName.

Anyways, everything seems to be functionning well (the other text box receives the focus and change its color appropriately), except one little detail: the textbox that was clicked on seems to keep scanning for mouse events, and no matter where I put my next click, it sets focus back on the original textbox that was clicked on the first time. Then things resume the way they should.

So getting back to the original state seems to be a 2-click affair.

Anybody has any thoughts on that?

Thanks everybody,

Feelow

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

10 answers

Sort by: Most helpful
  1. Anonymous
    2015-06-11T11:36:46+00:00

    Hi,

    I'm of the opinion that this is a bug. If we use mousedown and put a STOP command in the code we can establish that the code isn't called by any subsequent clicks so it seems that it's the execution of the code one-time that causes the problem.

    Intrigued by this I too did some Googling and can find many references that allude to this but none that actually state it's a bug of any kind. Oddly if you put the STOP command in the code and click in textbox 1 then the code executes, textbox2 gets the focus and the code halts at the stop. If you then tap F5 in VB editor to continue then clicking anywhere on the userform does not make it select textbox1 again and it behaves as you would expect.

    Private Sub TextBox1_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)

      TextBox2.SetFocus

    stop

    End Sub

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2015-06-10T22:26:32+00:00

    More meat on the subject from https://msdn.microsoft.com/en-us/library/office/gg278486.aspx

    If a mouse button is pressed while the pointer is over a form or control, that object "captures" the mouse and receives all mouse events up to and including the last MouseUp event. This implies that the XY mouse-pointer coordinates returned by a mouse event may not always be within the boundaries of the object that receives them.

    No idea how to break that chain, though...

    —— EDIT ——

    Losing the focus prevents the MouseUp event from happening, as can be observed if both MouseDown & MouseUp subroutines are present and a MsgBox or Debug.Print statement added to the MouseUp event.

    If the TextBox2.SetFocus statement is still present in the MouseDown sub, the MsgBox or Debug.Print won't show up until the second click.

    I haven't found anything online related to this, but still searching.

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2015-06-10T21:30:53+00:00

    Thanks for your input Mike. MouseUp is indeed a workaround, but I don't like it as much as MouseDown, since it's not as instantaneous.

    Could be a temporary solution for sure, though.

    For some reason, I suspect the problem could occur when the mouse button(s) is/are being released, but I couldn't think of any mean to test the validity of the hypothesis.

    Anyways, good to have feedback from a trustworthy source like you. Thanks again.

    If anyone can come up with a code in MouseDown that would solve the problem, I'm still a taker.

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2015-06-10T20:52:56+00:00

    Hi,

    I don't have the slightest idea about what is causing that but you can cure it by using MouseUp instead of MouseDown

    Private Sub TextBox1_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)

    Me.TextBox2.SetFocus

    End Sub

    Was this answer helpful?

    0 comments No comments
  5. Anonymous
    2015-06-10T19:39:59+00:00

    Okay, I tried to simplify my problem a bit and found something that people could build very quickly to figure out my problem.

    1 Userform, containing

    3 TextBox objects named respectively "TextBox1", "TextBox2" & "TextBox3"

    The following code in it:

    ' Code begins here

    Private Sub UserForm_Initialize()

      Dim i As Integer

      For i = 1 To 3: Me.Controls("TextBox" & i).Text = i: Next i

      Me.TextBox3.SetFocus

    End Sub

    Private Sub TextBox1_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)

      TextBox2.SetFocus

    End Sub

    ' Code ends here

    Showing the userform, it fills the text boxes with their numbers (so we know which one is which), then sets the focus on TextBox3.

    Clicking on TextBox1 should give focus to TextBox2. Go ahead and do it. Works!

    BUT then, the next click, on any of the three boxes, or on the form, or even anywhere on the screen, even if not on the Excel application window, will send the focus back to TextBox1...

    Then I'm lost at trying to go around this problem.

    Has anyone experienced this?

    Thanks everybody,

    Feelow

    Was this answer helpful?

    0 comments No comments