StealthRT
StealthRT

Reputation: 10542

panel scrolling programmable

Hey all I am trying to control the horizontal scroll of a panel box in vb.net. The problem is that I can only seem to move it just a little bit using the following code:

Private Sub HScrollBar1_Scroll(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles HScrollBar1.Scroll
    panSS.HorizontalScroll.Value = HScrollBar1.Value
End Sub

The forms width is 800 while the panel itself stretches 1000+ (but only 800px are shown). I just don't know how to go about using the HScrollBar to move it like it does if I had it on the auto-scroll feature.

Any help would be great.

Thanks!

David

update

Also tried doing this and the panel would not move at all:

Dim range = HScrollBar1.Maximum - HScrollBar1.LargeChange + HScrollBar1.SmallChange
Dim panelPos = (panSS.AutoScrollMinSize.Width - panSS.Width) * e.NewValue / range

    panSS.AutoScrollPosition = New Point(panelPos, 0)

Upvotes: 1

Views: 12972

Answers (2)

ileff
ileff

Reputation: 358

I would set the panel's AutoScroll property to True. Leave the panel width to 800, but as you add content even wider a built-in scroll bar will appear and manage the scrolling.

Upvotes: 1

Hans Passant
Hans Passant

Reputation: 941317

Yes, doesn't work because the panel's scroll range is much larger than your scroll bar's. You could fix it by setting the HScrollBar.Maximum value equal to the panel's scroll width. Or use this code, it works regardless of the scroll ranges:

Private Sub HScrollBar1_Scroll(ByVal sender As System.Object, ByVal e As ScrollEventArgs) Handles HScrollBar1.Scroll
    Dim range = HScrollBar1.Maximum - HScrollBar1.LargeChange + HScrollBar1.SmallChange
    Dim panelPos = (Panel1.AutoScrollMinSize.Width - Panel1.Width) * e.NewValue / range
    Panel1.AutoScrollPosition = New Point(panelPos, 0)
End Sub

Upvotes: 3

Related Questions