Question How to capture the cursor and keep it in a control

CoachBarker

Well-known member
Joined
Jul 26, 2007
Messages
133
Programming Experience
1-3
In my app I have a pause and continue button. I capture the last active control when pause is clicked, change the Backcolor to let the user know where they were, from blue to red and leave the cursor there.

What I would like to do without disabling and enabling all the controls on the form is to keep the cursor in the LastActiveControl no matter what until the continue button is clicked. In other words, tab, enter key, mouse click or nothing will make the cursor move until continue is clicked.

Sound possible?
 
Sounds pretty possible. You can probably capture the mouse using the Enter/Leave events of the button control and then reset it to the center of the button. You could handle the key down or press event and check for the Tab and Enter keys.
 
This works for everything but the enter key. Is it essential the enter not allow the the button to be clicked?
In the key up event you can see that the 'enter' key has been pressed, unfortunately this event is raised after the button click event which, in my code has already changed the canLeaveButton value.

Incidently the code for resetting the cursor position i found with a google search. :)


VB.NET:
    Dim canLeaveButton As Boolean = True

    Private Sub btn2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn2.Click
        canLeaveButton = Not canLeaveButton
    End Sub

    Private Sub btn2_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles btn2.KeyUp
        If Not canLeaveButton Then
            If e.KeyCode = Keys.Tab Then
                btn2.Focus()
            End If

            If e.KeyCode = Keys.Return Or e.KeyCode = Keys.Enter Then

            End If

        End If

    End Sub

    Private Sub btn2_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn2.MouseLeave
        If Not canLeaveButton Then
            'reposition the mouse
            Windows.Forms.Cursor.Position = New System.Drawing.Point(btn2.Location.X + Me.Location.X + 50, btn2.Location.Y + Me.Location.Y + 30)
        End If
    End Sub
 
Back
Top