0

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.

Richard C
  • 401
  • 1
  • 5
  • 19
  • Ok, so I found that the e.Location.X is representative to the Control, not the Parent control that I am trying to scroll. After changing e.Location to... FlowLayoutPanelUsers.PointToClient(MousePosition) this worked a treat. Just in case anyone is looking to do the similar – Richard C Feb 29 '16 at 23:11

1 Answers1

1

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
Richard C
  • 401
  • 1
  • 5
  • 19
  • I'm trying to use this approach to add touch-scrolling to a flowlayoutpanel that is quite "crowded" with controls. Unfortunately this only works as long as you don't touch one of the controls, but one of the teeny-tiny open sections between the controls. FlowLayoutPanel does not appear to have a keypreview capability so the important events for scrolling are lost. Any suggestions? – Destek Aug 31 '17 at 16:33