Search code examples
vb.netflowlayoutpanel

vb.net FlowLayoutPanel Touchscreen Scrolling


I've managed to get some form of scrolling on my FlowLayoutPanel when using a touchscreen by implementing the following code...

Dim mouseDownPoint As Point
Private Sub FlowLayoutPanelUsers_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles FlowLayoutPanelUsers.MouseDown
    If (e.Button = MouseButtons.Left) Then
        Me.mouseDownPoint = e.Location
    End If

End Sub

Private Sub FlowLayoutPanelUsers_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles FlowLayoutPanelUsers.MouseMove
    If (e.Button <> MouseButtons.Left) Then
        Return
    End If

    If ((mouseDownPoint.X = e.Location.X) _
                AndAlso (mouseDownPoint.Y = e.Location.Y)) Then
        Return
    End If

    Dim currAutoS As Point = FlowLayoutPanelUsers.AutoScrollPosition
    If (mouseDownPoint.Y > e.Location.Y) Then
        'finger slide UP
        If (currAutoS.Y <> 0) Then
            currAutoS.Y = (Math.Abs(currAutoS.Y) - 1)
        End If

    ElseIf (mouseDownPoint.Y < e.Location.Y) Then
        'finger slide down
        currAutoS.Y = (Math.Abs(currAutoS.Y) + 1)
    Else
        currAutoS.Y = Math.Abs(currAutoS.Y)
    End If

    If (mouseDownPoint.X > e.Location.X) Then
        'finger slide left
        If (currAutoS.X <> 0) Then
            currAutoS.X = (Math.Abs(currAutoS.X) - 1)
        End If

    ElseIf (mouseDownPoint.X < e.Location.X) Then
        'finger slide right
        currAutoS.X = (Math.Abs(currAutoS.X) + 1)
    Else
        currAutoS.X = Math.Abs(currAutoS.X)
    End If

    FlowLayoutPanelUsers.AutoScrollPosition = currAutoS
    mouseDownPoint = e.Location
    'IMPORTANT
End Sub

This is some code I've already found on stackoverflow, so thanks for that initally!

What I want this code to do, if possible, is reverse the way it scrolls, so if I scroll left, the FlowLayoutPanel scrolls right, if I scroll up, the Panel scrolls down, a bit like a web browser does.

Does anyone have any insight on it? I've tried the simple bit of reversing the minus signs to plus and visa versa, but no effect.

Thanks in advance.


Solution

  • Revised code...

    Dim mouseDownPoint As Point
    Private Sub FlowLayoutPanelUsers_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles FlowLayoutPanelUsers.MouseDown
       If (e.Button = MouseButtons.Left) Then
           Me.mouseDownPoint = FlowLayoutPanelUsers.PointToClient(MousePosition)
       End If
    
    End Sub
    
    Private Sub FlowLayoutPanelUsers_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles FlowLayoutPanelUsers.MouseMove
       If (e.Button <> MouseButtons.Left) Then
           Return
       End If
    
       If ((mouseDownPoint.X = FlowLayoutPanelUsers.PointToClient(MousePosition).X) _
                   AndAlso (mouseDownPoint.Y = FlowLayoutPanelUsers.PointToClient(MousePosition).Y)) Then
           Return
       End If
    
       Dim currAutoS As Point = FlowLayoutPanelUsers.AutoScrollPosition
       If (mouseDownPoint.Y > FlowLayoutPanelUsers.PointToClient(MousePosition).Y) Then
           'finger slide UP
           If (currAutoS.Y <> 0) Then
               currAutoS.Y = (Math.Abs(currAutoS.Y) - 1)
           End If
    
       ElseIf (mouseDownPoint.Y < FlowLayoutPanelUsers.PointToClient(MousePosition).Y) Then
           'finger slide down
           currAutoS.Y = (Math.Abs(currAutoS.Y) + 1)
       Else
           currAutoS.Y = Math.Abs(currAutoS.Y)
       End If
    
       If (mouseDownPoint.X > FlowLayoutPanelUsers.PointToClient(MousePosition).X) Then
           'finger slide left
           If (currAutoS.X <> 0) Then
               currAutoS.X = (Math.Abs(currAutoS.X) - 1)
           End If
    
       ElseIf (mouseDownPoint.X < FlowLayoutPanelUsers.PointToClient(MousePosition).X) Then
           'finger slide right
           currAutoS.X = (Math.Abs(currAutoS.X) + 1)
       Else
           currAutoS.X = Math.Abs(currAutoS.X)
       End If
    
       FlowLayoutPanelUsers.AutoScrollPosition = currAutoS
       mouseDownPoint = FlowLayoutPanelUsers.PointToClient(MousePosition)
       'IMPORTANT
    End Sub