使用datagridview滑动滚动

时间:2015-12-30 18:40:26

标签: vb.net winforms scroll datagridview windows-7

是否可以通过datagridview调整滚动位置?我希望用户可以用手指向上或向下或向左或向右滚动,而不是使用滚动条进行触摸屏。我知道某些控件你内置了一个AutoScrollPosition但是我可以使用什么来改变滚动位置以使用winforms上的datagridview用手指移动?操作系统是Windows 7,框架是4.我想在他们开始进行平移动作以打开平移并让它们滚动时进行操作。

1 个答案:

答案 0 :(得分:1)

这就是我完成你所要求的方式。

Private Sub DataGridView_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles MyBase.MouseMove
    Select Case e.Button
        Case MouseButtons.Left
            Dim mouseMovement As Point = GetMouseMovement(e.Location)
            If Panning Then
                If mouseMovement.X = 0 Then
                    Me.AutoScrollOffset = Point.Round(New PointF(-AutoScrollOffset.X, mouseMovement.Y - AutoScrollOffset.Y))
                ElseIf mouseMovement.Y = 0 Then
                    Me.AutoScrollOffset = Point.Round(New PointF((mouseMovement.X / Convert.ToSingle(Math.Abs(mouseMovement.X))) * 10 - AutoScrollOffset.X, -AutoScrollOffset.Y))
                Else
                    Me.AutoScrollOffset = Point.Round(New PointF((mouseMovement.X / Convert.ToSingle(Math.Abs(mouseMovement.X))) * 10 - AutoScrollOffset.X, mouseMovement.Y - AutoScrollOffset.Y))
                End If
                Me.ClearSelection()
            End If
    End Select
End Sub

''' <summary>
''' Checks if there is any mouse movement based on passed in point
''' </summary>
''' <remarks></remarks>
Private Function GetMouseMovement(ByVal point As Point) As Point
    If Math.Abs(mouseDownPosition.X - point.X) >= SystemInformation.DoubleClickSize.Width Or
           Math.Abs(mouseDownPosition.Y - point.Y) >= SystemInformation.DoubleClickSize.Height Then
        _panning = True
        Return New Point(point.X - mouseDownPosition.X, point.Y - mouseDownPosition.Y)
    End If
    _panning = False
    Return New Point(0, 0)
End Function