Click on panel and Move mouse Right, then panel follows X axis only?

StormyChan

New member
Joined
May 31, 2014
Messages
2
Programming Experience
1-3
Hello everyone.

i am working on a application. i need some help figuring out a problem i have with one of the features:

i use Visual Basic express 2010

what the feature should do:
i have 1 panel in the form that want the user to be able to move the panel along the location.X
by now i have this code:
Dim MoveBox, Curlocation As New Point(0, 0)
    Private Sub sync()
        MoveBox = MoveArea.Location
        Curlocation = Cursor.Position

    End Sub
    Private Sub MoveArea_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MoveArea.Click

    End Sub
    Private Sub MoveArea_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MoveArea.MouseDown
        Timer1.Enabled = True
        Timer1.Start()
        sync()
    End Sub
    Private Sub MoveArea_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MoveArea.MouseUp
        Timer1.Stop()
        sync()

    End Sub
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        MoveArea.Location = MoveBox - Curlocation + Cursor.Position
    End Sub

Controls in form:
1 Panel called: MoveArea
1 Timer called: Timer1

what this code allready do is:
you can click the panel and move it around freely in the form. but i want it to only move the X location of the panel.

sins there are no "MouseRight" Event it have to be an other way around.

What do i need to change to make this work??

Please help me out.

-Stormy
 
Last edited by a moderator:

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,481
Location
Sydney, Australia
Programming Experience
10+
You wouldn't use a Timer for a start. This example is by no means rigorous but shows the sort of thing that you should do:
Private isMoving As Boolean = False
Private previousX As Integer

Private ignoreNextMove As Boolean = False

Private Sub Panel1_MouseDown(sender As Object, e As MouseEventArgs) Handles Panel1.MouseDown
    isMoving = True
    previousX = e.X
End Sub

Private Sub Panel1_MouseMove(sender As Object, e As MouseEventArgs) Handles Panel1.MouseMove
    If ignoreNextMove Then
        'This event was triggered by the Panel moving rather than the mouse.
        ignoreNextMove = False
    ElseIf isMoving Then
        Dim delta = e.X - previousX
        Dim newLeft = Panel1.Left + delta

        If newLeft < 0 Then
            newLeft = 0
        Else
            Dim newRight = Panel1.Right + delta

            If newRight > Me.ClientSize.Width Then
                newLeft -= Me.ClientSize.Width - Panel1.Width
            End If
        End If

        'Moving the Panel will trigger another MouseMove event so we should ignore that one.
        ignoreNextMove = True

        previousX = e.X
        Panel1.Left = newLeft
    End If
End Sub

Private Sub Panel1_MouseUp(sender As Object, e As MouseEventArgs) Handles Panel1.MouseUp
    isMoving = False
End Sub
 

StormyChan

New member
Joined
May 31, 2014
Messages
2
Programming Experience
1-3
Amazing ^,^ it works great but a little bit laggy movement, but am happy about it :3
Thank you very much for your help!
this was my first post on this forum :3
 
Top Bottom