2
votes

I'm having trouble getting the mouse cursor to change while in a Mouse Move Event when the Left Mouse button = the Left Mouse Button.

enter image description here

In the gif image in the 'Text Control Coordinates' Text Box you can see that I am updating this text box with an entry of what the Cursor should be updating to. It's the last entry in the box at the bottom. The Cursor is set to 'SizeAll' however when I am moving the control using the Left Mouse Button I have code to set the controls cursor to a 'Hand'. The text box shows that the logic is correctly being reached to update the Cursor to a 'Hand'. The issue is that the cursor only updates after I release the Left Mouse Button. It does this until I begin moving the mouse again and the MouseMove event takes over again.

Private Sub ClsTextObj_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
    ' Get object under cursor when user moved mouse - MouseMove
    ' We are only going to perform the WORK if the object is TypeOf clsTextObj
    If TypeOf sender Is clsTextObj Then
        Dim txt_clsText_ctrl_tmp = DirectCast(sender, clsTextObj)

        'Declare Bool to determine if Left Mouse Button is being used
        Dim IsMouseLeftButton = e.Button = Windows.Forms.MouseButtons.Left

        Dim mCursor As Cursor
        Dim MouseCursor = "Logic Never Reached"

        'Set mCursor var to the hand cursor when the LEFT Mouse Button is being used
        If IsMouseLeftButton Then
            MouseCursor = "Hand"
            mCursor = Cursors.Hand
        Else
            MouseCursor = "SizeAll"
            mCursor = Cursors.SizeAll
        End If

        'START Determine Mouse Cursor - Find where the mouse cursor is within the control
        Dim MouseIsInLeftEdge As Boolean
        Dim MouseIsInRightEdge As Boolean
        Dim MouseIsInTopEdge As Boolean
        Dim MouseIsInBottomEdge As Boolean

        MouseIsInLeftEdge = Math.Abs(e.X) <= 9
        MouseIsInRightEdge = Math.Abs(e.X - txt_clsText_ctrl_tmp.Width) <= 9
        MouseIsInTopEdge = Math.Abs(e.Y) <= 9
        MouseIsInBottomEdge = Math.Abs(e.Y - txt_clsText_ctrl_tmp.Height) <= 9

        If MouseIsInLeftEdge Then
            If MouseIsInTopEdge Then
                txt_clsText_ctrl_tmp.Appearance.Cursor = Cursors.SizeNWSE
            ElseIf MouseIsInBottomEdge Then
                txt_clsText_ctrl_tmp.Appearance.Cursor = Cursors.SizeNESW
            Else
                txt_clsText_ctrl_tmp.Appearance.Cursor = Cursors.SizeWE
            End If
        ElseIf MouseIsInRightEdge Then
            If MouseIsInTopEdge Then
                txt_clsText_ctrl_tmp.Appearance.Cursor = Cursors.SizeNESW
            ElseIf MouseIsInBottomEdge Then
                txt_clsText_ctrl_tmp.Appearance.Cursor = Cursors.SizeNWSE
            Else
                txt_clsText_ctrl_tmp.Appearance.Cursor = Cursors.SizeWE
            End If
        ElseIf (MouseIsInTopEdge Or MouseIsInBottomEdge) Then
            txt_clsText_ctrl_tmp.Appearance.Cursor = Cursors.SizeNS
        Else
            txt_clsText_ctrl_tmp.Appearance.Cursor = mCursor
            'txt_clsText_ctrl_tmp.Appearance.Cursor = Cursor.SizeAll
        End If
        'END Determining Mouse Cursor

        'Capture Mouse Down Clicks while moving mouse cursor.  Mouse Move overrides Mouse Down basically and so we capture mouse down buttons while moving here
        'If e.Button = Windows.Forms.MouseButtons.Left Then
        If IsMouseLeftButton Then
            txt_clsText_ctrl.Appearance.Cursor = Cursors.Hand
            txt_clsText_ctrl.Location = New Point(txt_clsText_ctrl.Location.X + (e.X - initialClickLocation.X), txt_clsText_ctrl.Location.Y + (e.Y - initialClickLocation.Y))
        End If

        'Output mouse details
        ClsTextObj1.Value = "X: " & txt_clsText_ctrl_tmp.Location.X + e.X _
                          & ",Y: " & txt_clsText_ctrl_tmp.Location.Y + e.Y

        'Build User output info
        builder.Clear()
        builder.Append("Control Name: " & txt_clsText_ctrl_tmp.Name).AppendLine()
        builder.Append("e.X " & e.X & ", e.Y: " & e.Y).AppendLine()
        builder.Append("e.Point Location: " & e.Location.ToString()).AppendLine()
        builder.Append("txt_clsText_ctrl Point Location: " & txt_clsText_ctrl_tmp.Location.ToString()).AppendLine()
        builder.Append("Form Location: {X=" & txt_clsText_ctrl_tmp.Location.X + e.X)
        builder.Append(",Y=" & txt_clsText_ctrl_tmp.Location.Y + e.Y & "}").AppendLine()
        builder.Append("initial Ctrl Location: " & initialCtrlLocation.ToString()).AppendLine()
        builder.Append("initial Click Location: " & initialClickLocation.ToString()).AppendLine()
        builder.Append("Calc Point: " & "X: " & (e.X - initialClickLocation.X))
        builder.Append(",Y: " & (e.Y - initialClickLocation.Y)).AppendLine()
        builder.Append("New Label Pos: " & "X: " & txt_clsText_ctrl_tmp.Location.X + (e.X - initialClickLocation.X))
        builder.Append(",Y: " & txt_clsText_ctrl_tmp.Location.Y + (e.Y - initialClickLocation.Y)).AppendLine()
        builder.Append("Mouse Button: " & e.Button.ToString()).AppendLine()
        builder.Append("Left Edge: " & MouseIsInLeftEdge).AppendLine()
        builder.Append("Right Edge: " & MouseIsInRightEdge).AppendLine()
        builder.Append("Top Edge: " & MouseIsInTopEdge).AppendLine()
        builder.Append("Bottom Edge: " & MouseIsInBottomEdge).AppendLine()
        builder.Append("Cursor: " & appendMouseCursor)

        ClsTextObj2.Value = builder.ToString()

    End If
End Sub

What am I missing here? I can successfully update other parts of my application while the LEFT Mouse Button is being used with the exception of the Mouse Cursor. I even tried using Refresh() on the control and some other silly ideas that never seemed to get the cursor to turn into a hand while the control is moving.

You can even see that I am successfully updating the Mouse Cursor based on the edges of the Control from within the MouseMove Sub. However when the Left Mouse Button is pressed it is as though the updating of the mouse cursor is put on hold until I let go of the button.

Update 10.19.18: I used the Form Cursor instead:

If IsMouseLeftButton And IsMouseDown Then
        Me.Cursor = Cursors.Hand
        'txt_clsText_ctrl.Appearance.Cursor = Cursors.Hand
        txt_clsText_ctrl.Location = New Point(txt_clsText_ctrl.Location.X + (e.X - initialClickLocation.X), txt_clsText_ctrl.Location.Y + (e.Y - initialClickLocation.Y))
End If

Since I was using the Form Cursor I had to be sure to update it back to the default. I found MouseUp to work just fine for this. At least I haven't encountered any bugs yet.

Private Sub ClsTextObj_MouseUp(sender As Object, e As MouseEventArgs)
    If TypeOf sender Is STORE.PDFBuilder.clsTextObj Then
        Dim txt_clsText_ctrl_tmp = DirectCast(sender, STORE.PDFBuilder.clsTextObj)

        IsMouseDown = False
        Me.Cursor = Cursors.Default

    End If
End Sub

Lastly, for any Normal .NET Control you can use this same code but instead of my having to use Control.Appearance.Cursor ---> you would instead use Control.Cursor

1
What is clsTextObj? A TextBox derived class? What does its Appearance property perform? Set/define the Cursor type/style? You should inspect that code. - Jimi
Is this part "Dim IsMouseLeftButton = e.Button = Windows.Forms.MouseButtons.Left" working properly? - Koken
The application I support uses 3rd Part Controls created by a company called Infragisitcs. I wrote something very similar to what I'm doing now in C# but it uses standard .NET controls and it works perfectly. @Jimi got me thinking that since the code that is inaccessible to me in the Appearance property that I should just instead use the Form Cursor. I updated my code to instead update the Form Cursor to the hand while the Left Mouse Button is being used. This worked perfectly. - Code Novice

1 Answers

0
votes

With a bit of a nudge from Jimi I instead used the Form Cursor as the Infragistics Control I was working with wasn't able to update the mouse cursor while the Mouse Button was being used for whatever reason. I instead used the Form Cursor. Code below:

Private Sub ClsTextObj_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)

    ' Get object under cursor when user moved mouse - MouseMove
    ' We are only going to perform the WORK if the object is TypeOf clsTextObj
    If TypeOf sender Is clsTextObj Then
        Dim txt_clsText_ctrl_tmp = DirectCast(sender, clsTextObj)

        Dim IsMouseLeftButton = e.Button = Windows.Forms.MouseButtons.Left

        Dim mCursor As Cursor
        Dim MouseCursor = "Logic Never Reached"

        If IsMouseLeftButton And IsMouseDown Then
            MouseCursor = "Hand"    'Used to output info to screen
            mCursor = Cursors.Hand
        Else
            MouseCursor = "SizeAll" 'Used to output info to screen
            mCursor = Cursors.SizeAll
        End If

        'START Determine Mouse Cursor - Find where the mouse cursor is within the control
        Dim MouseIsInLeftEdge As Boolean
        Dim MouseIsInRightEdge As Boolean
        Dim MouseIsInTopEdge As Boolean
        Dim MouseIsInBottomEdge As Boolean

        MouseIsInLeftEdge = Math.Abs(e.X) <= 9
        MouseIsInRightEdge = Math.Abs(e.X - txt_clsText_ctrl_tmp.Width) <= 9
        MouseIsInTopEdge = Math.Abs(e.Y) <= 9
        MouseIsInBottomEdge = Math.Abs(e.Y - txt_clsText_ctrl_tmp.Height) <= 9

        If MouseIsInLeftEdge Then
            If MouseIsInTopEdge Then
                txt_clsText_ctrl_tmp.Appearance.Cursor = Cursors.SizeNWSE
            ElseIf MouseIsInBottomEdge Then
                txt_clsText_ctrl_tmp.Appearance.Cursor = Cursors.SizeNESW
            Else
                txt_clsText_ctrl_tmp.Appearance.Cursor = Cursors.SizeWE
            End If
        ElseIf MouseIsInRightEdge Then
            If MouseIsInTopEdge Then
                txt_clsText_ctrl_tmp.Appearance.Cursor = Cursors.SizeNESW
            ElseIf MouseIsInBottomEdge Then
                txt_clsText_ctrl_tmp.Appearance.Cursor = Cursors.SizeNWSE
            Else
                txt_clsText_ctrl_tmp.Appearance.Cursor = Cursors.SizeWE
            End If
        ElseIf (MouseIsInTopEdge Or MouseIsInBottomEdge) Then
            txt_clsText_ctrl_tmp.Appearance.Cursor = Cursors.SizeNS
        Else
            txt_clsText_ctrl_tmp.Appearance.Cursor = mCursor
        End If
        'END Determining Mouse Cursor

        'Capture Mouse Down Clicks while moving mouse cursour.  Mouse Move overrides Mouse Down basically and so we capture mouse down buttons while moving here
        'If e.Button = Windows.Forms.MouseButtons.Left Then
        If IsMouseLeftButton Then 'And IsMouseDown Then
            Me.Cursor = mCursor
            txt_clsText_ctrl.Location = New Point(txt_clsText_ctrl.Location.X + (e.X - initialClickLocation.X), txt_clsText_ctrl.Location.Y + (e.Y - initialClickLocation.Y))
        End If


        ClsTextObj1.Value = "X: " & txt_clsText_ctrl_tmp.Location.X + e.X _
                          & ",Y: " & txt_clsText_ctrl_tmp.Location.Y + e.Y

        'Build User output info
        builder.Clear()
        builder.Append("Control Name: " & txt_clsText_ctrl_tmp.Name).AppendLine()
        builder.Append("e.X " & e.X & ", e.Y: " & e.Y).AppendLine()
        builder.Append("e.Point Location: " & e.Location.ToString()).AppendLine()
        builder.Append("txt_clsText_ctrl Point Location: " & txt_clsText_ctrl_tmp.Location.ToString()).AppendLine()
        builder.Append("Form Location: {X=" & txt_clsText_ctrl_tmp.Location.X + e.X)
        builder.Append(",Y=" & txt_clsText_ctrl_tmp.Location.Y + e.Y & "}").AppendLine()
        builder.Append("initial Ctrl Location: " & initialCtrlLocation.ToString()).AppendLine()
        builder.Append("initial Click Location: " & initialClickLocation.ToString()).AppendLine()
        builder.Append("Calc Point: " & "X: " & (e.X - initialClickLocation.X))
        builder.Append(",Y: " & (e.Y - initialClickLocation.Y)).AppendLine()
        builder.Append("New Label Pos: " & "X: " & txt_clsText_ctrl_tmp.Location.X + (e.X - initialClickLocation.X))
        builder.Append(",Y: " & txt_clsText_ctrl_tmp.Location.Y + (e.Y - initialClickLocation.Y)).AppendLine()
        builder.Append("Mouse Button: " & e.Button.ToString()).AppendLine()
        builder.Append("Left Edge: " & MouseIsInLeftEdge).AppendLine()
        builder.Append("Right Edge: " & MouseIsInRightEdge).AppendLine()
        builder.Append("Top Edge: " & MouseIsInTopEdge).AppendLine()
        builder.Append("Bottom Edge: " & MouseIsInBottomEdge).AppendLine()
        builder.Append("Cursor: " & MouseCursor).AppendLine()
        builder.Append("Is Mouse Down: " & IsMouseDown).AppendLine()
        builder.Append("Is Mouse Left: " & IsMouseLeftButton)

        ClsTextObj2.Value = builder.ToString()

    End If

End Sub

Private Sub ClsTextObj_MouseDown(sender As Object, e As MouseEventArgs)
    ' Get object under cursor when user clicked MouseDown
    If TypeOf sender Is clsTextObj Then

        initialClickLocation = New Point(e.X, e.Y)

        txt_clsText_ctrl = DirectCast(sender, clsTextObj)

        IsMouseDown = True

        initialCtrlLocation = txt_clsText_ctrl.Location

        'ListView Stuff
        updateListView()

    End If
End Sub

Private Sub ClsTextObj_MouseUp(sender As Object, e As MouseEventArgs)

    IsMouseDown = False
    Me.Cursor = Cursors.Default

End Sub

With some additional Code I finally got what I needed: enter image description here