Increasing a panel's AutoScrollPosition

DiverDan

Contributor
Joined
Jan 16, 2003
Messages
645
Location
Sacramento, CA
Hi,
I'm having a problem programmably increasing a panel's AutoScrollPosition. With the lower code, the auto scroll vibrates up and down instead of progressing constantly downward.

Visual Basic:
    Private Sub bkrTemp_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles bkrTemp.MouseMove

        Dim pt As Point
        pt.X = Me.PointToClient(Cursor.Current.Position).X - x
        pt.Y = Me.PointToClient(Cursor.Current.Position).Y - y
        With bkrTemp
            .Location = pt
            .Capture = True
        End With

        If pnlPanel.AutoScroll = True Then
            Dim bkrLocY As Integer = bkrTemp.Location.Y - (pnlPanel.Location.Y + pnlPanel.AutoScrollPosition.Y) + bkrTemp.Height
            Dim pnlHeight As Integer = pnlPanel.Height - (pnlPanel.AutoScrollMargin.Height + 17)

            If bkrLocY + 2 >= pnlHeight Then
                pnlPanel.AutoScrollPosition = New Point(pnlPanel.AutoScrollPosition.X, pnlPanel.AutoScrollPosition.Y + 5)
            End If
        End If
    End Sub

How can I have pnlPanel scroll constantly downward with bkrTemp?

Thanks,
Dan
 
I ran into a very similar problem recently. The AutoScrollPosition property is pretty quirky. When you get the AutoScrollPosition is returns how much the view has moved, i.e. {10, 20} would be scrolled right ten and down twenty. When you set it, you need to specify how far the controls should be moved. If you are scrolled ten right and twenty down, the controls have moved ten left and twenty up on the screen, which means that you would want to set AutoScrollPosition to {-10, -20} to set it to that same position.

I'm guessing that this is a bug. I believe that the solution is to negate the value immediately before assigning it (if you want to think in terms of where the view port is... if you want to think in terms of how far the controls have moved, negate the value as soon as you obtain it). For example, to move the AutoScrollPosition down and right fifty pixels, you would use the following code:
Visual Basic:
Dim View As Point = MyPanel.AutoScrollPosition
View.X = -(View.X + 50)
View.Y = -(View.Y + 50)
MyPanel.AutoScrollPosition = View
I didn't test that code, but something like that should do the trick.
 
Back
Top