Restore Scroll Bar Position on Tab

P

Paul Engel

I have an application on which I have a tab control with two tabs. When a
user double-clicks on an item in tab1 of my tabs collection, tab2 becomes
the visible tab. (tabImageTabs.SelectedIndex = 1) When they double-click on
the content in tab2, they are taken back to tab1.
(tabImageTabs.SelectedIndex = 0)

When tab1 is restored, I have lost the position of the scrollbar and the
user must re-scroll to get back to where they were. I've tried setting the
scroll value (grab the verticlescroll value when leaving tab1 and assigning
it that value when I go back, but it doesn't seem to work.

Any ideas?
Here's the code I use to capture the position of the scrollbar when changing
to tab2:
ScrollBarLocation = tabThumbNails.VerticalScroll.Value
tabImageTabs.SelectedIndex = 1

Here's the code I am using to move back to the tab1 with the verticle scroll
value set:
tabImageTabs.SelectedIndex = 0
tabThumbNails.VerticalScroll.Value = ScrollBarLocation

(At this point, I get my tab1 back in front, but the scroll bar is back at
the top.)
 
O

OmegaSquared

Hello, Paul,
I tried a few experiments with this and it does indeed seem flaky. I
wasn't able to find any "nice" solution. But I guess that there is some
timing problem involved where the TabPage is trying to scroll the
top-leftmost control into view and so it only partly pays attention to the
value that you are setting into tabThumbNails.VerticalScroll.Value.

The only thing I could do to get it to perform reasonably was to add a timer
and use it to set the scroll position. Even then, the value I tried to set
wouldn't "take" on the first attempt. Hence I kept the timer going until the
TabPage was finally persuaded to take the scroll value.

This is definitely not a pretty solution, but here it is anyway:

Public Class Form1
Private ScrollBarLocation As Integer

Private Sub tabThumbNails_DoubleClick(ByVal sender As Object, ByVal e As
System.EventArgs) Handles tabThumbNails.DoubleClick
ScrollBarLocation = tabThumbNails.VerticalScroll.Value
tabImageTabs.SelectedIndex = 1
End Sub

Private Sub TabPage2_DoubleClick(ByVal sender As Object, ByVal e As
System.EventArgs) Handles tabPage2.DoubleClick
tabImageTabs.SelectedIndex = 0
Timer1.Start()
End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Timer1.Tick
tabThumbNails.VerticalScroll.Value = ScrollBarLocation
If (tabThumbNails.VerticalScroll.Value = ScrollBarLocation) Then
Timer1.Stop()
End If
End Sub
End Class

Cheers,
Randy
 
P

Paul Engel

Thanks, Randy. Yup, it's a bit messy, but it solves the problem. I really
appreciate your taking the time to play w/ it. I'll implement your solution
ASAP!!!

Paul
 
P

Paul Engel

Randy,

I have tried this and what I find is that the scroll bar verticle value
changes immediately, but when the tab is returned to the focus, the scroll
bar does not move. It stays at the 0 position. Any ideas?

Paul
 
P

Paul Engel

I've solved this problem. Instead of setting the value of the scroll bar,
which does not reposition it, I have used the ScrollControlIntoView method
of the container object. I run through my collection of thumbnails and grab
their Y coordiante. Since the ScrollControlIntoView method simply makes sure
the control is showing at the bottom of the viewable container, I have to
subtract the height of the container from my calculation to position the
control at 0,0. Here's the button I wrote to test this:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim ICPosition As Point

Dim ICY As Integer

For Each ImageControl As Windows.Forms.Control In tabThumbNails.Controls

ICPosition = ImageControl.Location

ICY = ICPosition.Y

If ImageControl.Height - tabThumbNails.Height + ICY > 0 Then

tabThumbNails.ScrollControlIntoView(ImageViewers.Item(CInt(ImageControl.Name)))

End If

Next



End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top