none
Label_MouseMove() Don't work with mouse left button pressed. RRS feed

  • Question

  • ere's what I'd like to implement :
    I arranged a square text label in a row in my userform. when I click and hold the mouse, and drag along the row, the square text label's color changes to black.
    when I drag labels again the text label's color changes to white.
    (I'm making working hours record, and need this function.)

    So I used the MouseMove() function to implement it.

    but some problems occurs.


    MouseMove function seems not works well with left mouse button pressed.

    How can I solve this problem?

    Friday, July 6, 2018 3:16 PM

All replies

  • Please show the relevant code you are using and explain which line or lines do not work as expected.  Be sure to include a description of what you expect to happen.

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Friday, July 6, 2018 3:26 PM
    Moderator
  • Maybe Code is not important, because I just need MouseMove() Funtion work well with left mouse button pressed.

    belows are my code.

    Public Sub Day1_MouseMove(ByVal Index As Integer, ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
        If WhereAreLabelsMoving <> Index And CheckLeftMousePressed() = True Then
            If Day1.Item(Index).BackColor <> RGB(255, 255, 255) Then
                Day1.Item(Index).BackColor = RGB(255, 255, 255)
            Else
                Day1.Item(Index).BackColor = RGB(0, 0, 0)
            End If
        End If
        WhereAreLabelsMoving = Index
    End Sub

    I have several rows of text labels and each labels have codes like that.

    when I put mouse cursor on the text labels, usually MouseMove() function works well,

    but when I put mouse cursor on the text labels with left mouse button pressed, MouseMove() Function don't work. 



    • Edited by Truth- Saturday, July 7, 2018 2:32 AM
    Saturday, July 7, 2018 2:31 AM
  • as same as its name.

    CheckLeftMousePressed() checks whether if left mouse is pressed.

    belows are the code.

    they are declared in module.

    Declare Function GetKeyState Lib "user32.dll" (ByVal KeyCode As Long) As Integer
    Const VK_LBUTTON = &H1
    Function CheckLeftMousePressed()
        CheckLeftMousePressed = GetKeyState(vbKeyLButton)
    End Function

    and this function works well.... but can it be problem?

    when I made test program, without that code, MouseMove() Function don't work when the left mouse button is pressed.... 

    I think code is not important... I just wonder I can made MouseMove() Funtion works when left mouse pressed.
    • Edited by Truth- Saturday, July 7, 2018 12:18 PM
    Saturday, July 7, 2018 12:18 PM
  • what I expect to happen is 

    MouseMove function works well with left mouse button pressed.


    • Edited by Truth- Saturday, July 7, 2018 12:22 PM
    Saturday, July 7, 2018 12:22 PM


  • Saturday, July 7, 2018 12:57 PM
  • Declare Function GetKeyState Lib "user32.dll" (ByVal KeyCode As Long) As Integer
    Const VK_LBUTTON = &H1
    Function CheckLeftMousePressed()
        CheckLeftMousePressed = GetKeyState(vbKeyLButton)
    End Function

    The right way with APIs is :

    If (GetAsyncKeyState(VK_LBUTTON) And &H8000) Then
            ' Code
    End If

    with :

    <DllImport("User32.dll", SetLastError:=True, CharSet:=CharSet.Auto)>
    Private Shared Function GetAsyncKeyState(ByVal vKey As Integer) As Short
    End Function


    • Edited by Castorix31 Saturday, July 7, 2018 1:37 PM
    Saturday, July 7, 2018 1:25 PM
  • As shown, your unmanaged function is incorrect.

    But also, as shown, you don't need an unmanaged function.  The MouseMove event contains a MouseEventArgs parameter which contains the state of the mouse buttons... simply use it.

    Private Sub Button1_MouseMove(sender As Object, e As MouseEventArgs) Handles Button1.MouseMove
        If e.Button = MouseButtons.Left Then
    
        End If
    End Sub
    


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Sunday, July 8, 2018 12:36 PM
    Moderator
  • Thank you for your answer, but I didn't mean that....

    you can try making a label and, and label_MouseMove.

    press mouse button outside the label and put mouse cursor on the label.

    and Label_MouseMove function will not work on the label.

    Monday, July 9, 2018 2:33 PM
  • Thank you for your reply! but I dont have idea yet...
    • Edited by Truth- Monday, July 9, 2018 2:34 PM
    Monday, July 9, 2018 2:33 PM
  • thank you for your information! but I dont have idea yet...
    • Edited by Truth- Monday, July 9, 2018 2:34 PM
    Monday, July 9, 2018 2:34 PM
  • Thank you for your reply! but I dont have idea yet...

    You are calling your "Day1MouseMove()" method from the label or form's MouseMove event, right?  So add a parameter to your Day1MouseMove method to take the button state from the MouseMove event of the label/form.  Then your method will have the information about whether or not the mouse button is pressed.

    Also note that a statement like this "WhereAreLabelsMoving <> Index And CheckLeftMousePressed()" should use AndAlso, instead of just And.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Monday, July 9, 2018 3:10 PM
    Moderator
  • Created this class and added to the form.

    Public Class DragInfo
        Public InitialMouseCoords As Point
        Public InitialLocation As Point
        Private _NewLocation As Point
    
        Public Sub New(aControl As Control)
            InitialMouseCoords = Form.MousePosition
            InitialLocation = aControl.Location
            If aControl.ForeColor = Color.Black Then
                aControl.BackColor = Color.Black
                aControl.ForeColor = Color.White
            Else
                aControl.BackColor = Color.White
                aControl.ForeColor = Color.Black
            End If
        End Sub
    
        Public Function NewLocation() As Point
            Dim MouseCoords As Point = Form.MousePosition
            Me._NewLocation = New Point(InitialLocation.X + (MouseCoords.X - InitialMouseCoords.X), InitialLocation.Y + (MouseCoords.Y - InitialMouseCoords.Y))
            Return Me._NewLocation
        End Function
    End Class
    

    Then as a test I added three labels and did this

        Private Sub _MouseDown(sender As Object, e As MouseEventArgs) _
                Handles Label1.MouseDown, Label2.MouseDown, Label3.MouseDown
    
            Dim ctrl As Control = DirectCast(sender, Control)
            ctrl.Tag = New DragInfo(ctrl)
        End Sub
    
        Private Sub _MouseMove(sender As Object, e As MouseEventArgs) _
                Handles Label1.MouseMove, Label2.MouseMove, Label3.MouseMove
    
            Dim ctrl As Control = DirectCast(sender, Control)
            If ctrl.Tag IsNot Nothing Then
                Dim info As DragInfo = CType(ctrl.Tag, DragInfo)
                Dim newLoc As Point = info.NewLocation()
                If Me.ClientRectangle.Contains(New Rectangle(newLoc, ctrl.Size)) Then ctrl.Location = newLoc
            End If
        End Sub
    
        Private Sub _MouseUp(sender As Object, e As MouseEventArgs) _
                Handles Label1.MouseUp, Label2.MouseUp, Label3.MouseUp
    
            Dim ctrl As Control = DirectCast(sender, Control)
            ctrl.Tag = Nothing
        End Sub
    

    Hope it gives you some ideas.


    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it."

    - from former MSDN User JohnWein

    SerialPort Info

    Multics - An OS ahead of its time.

    Monday, July 9, 2018 8:51 PM
  • I'm not just wonder whether mouse button is pressed or not.

    When Label1_MouseMove Function is called and mouse is pressed,  Label2_MouseMove Function don't work,

    and if you enter Label 1 with mouse button pressed, the Label 1 MouseMove function don't works, as my question.

    If you press mouse on the Label 1, MouseMove works and "Button" Variables will be changed as it is clicked.

    but, If you press mouse outside the Label 1, and move to Label1,  Mousemove don't work, as my question.

    I think my question was hard to understand... (my fault) but my problem is not solved yet.


    • Edited by Truth- Tuesday, July 17, 2018 1:16 PM
    Tuesday, July 17, 2018 1:08 PM
  • I'm not just wonder whether mouse button is pressed or not.

    When Label1_MouseMove Function is called and mouse is pressed,  Label2_MouseMove Function don't work,

    and if you enter Label 1 with mouse button pressed, the Label 1 MouseMove function don't works, as my question.

    If you press mouse on the Label 1, MouseMove works and "Button" Variables will be changed as it is clicked.

    but, If you press mouse outside the Label 1, and move to Label1,  Mousemove don't work, as my question.

    I think my question was hard to understand... (my fault) but my problem is not solved yet.


    That is because the Form "captures" the mouse when you press the mouse button while the pointer is on the form.  Once the mouse has been captured, move events are not passed to other controls.

    You can bypass this behavior by setting the Capture property to false when the mouse button is pressed on the form:

    Private Sub Form1_MouseDown(sender As Object, e As MouseEventArgs) Handles Me.MouseDown
        Me.Capture = False
    End Sub
    


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Tuesday, July 17, 2018 1:39 PM
    Moderator