面板vb.net上的自动滚动

时间:2014-03-19 20:30:19

标签: vb.net timer scrollbar

我正在尝试自动滚动(在面板上一直向下滚动,然后一直向上滚动)。我得到它直到面板的末尾,但不能让它回来。它被设置为在计时器上工作。这是我的代码:

Private Sub tmrSCROLL_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs)      Handles tmrSCROLL.Tick

       pnlScrollFeed.AutoScrollPosition = New Point(pnlScrollFeed.AutoScrollPosition.X, Math.Abs(pnlScrollFeed.AutoScrollPosition.Y) + 1)

        Dim totalHeight As Integer = pnlScrollFeed.VerticalScroll.Maximum
        Dim tempHeight As Integer = pnlScrollFeed.VerticalScroll.Value
        Dim tempDiff As Integer = totalHeight - tempHeight

        If tempDiff > 800 Then
            pnlScrollFeed.AutoScrollPosition = New Point(pnlScrollFeed.AutoScrollPosition.X, Math.Abs(pnlScrollFeed.AutoScrollPosition.Y) + 1)
        ElseIf tempDiff <= 800 Then
            pnlScrollFeed.AutoScrollPosition = New Point(pnlScrollFeed.AutoScrollPosition.X, Math.Abs(pnlScrollFeed.AutoScrollPosition.Y) - 1)
            tempHeight += 1
        End If


    End Sub

1 个答案:

答案 0 :(得分:2)

如果你有一个变量来告诉你要去哪个方向,那么可能效果会更好,然后只计算滚动拇指何时触及底部或顶部:

Private scrollUp As Boolean = False

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
  If scrollUp Then
    Dim scrollY As Integer = -Panel1.AutoScrollPosition.Y - 1
    If scrollY < 0 Then
      scrollUp = False
    Else
      Panel1.AutoScrollPosition = New Point(Panel1.AutoScrollPosition.X, scrollY)
    End If
  Else
    Dim scrollY As Integer = -Panel1.AutoScrollPosition.Y + 1
    If scrollY > Panel1.AutoScrollMinSize.Height - Panel1.ClientSize.Height Then
      scrollUp = True
    Else
      Panel1.AutoScrollPosition = New Point(Panel1.AutoScrollPosition.X, scrollY)
    End If
  End If
End Sub