Search code examples
vb.netscrollbartabpage

Scroll vertical dynamically in tabPage control


In my relatively simple project I have a two page tabControl. tabPage2 consists of dynamically created pictureBoxes (thumbnails, pic_XXX). tabPage2 is of fixed size, with AutoScroll enabled.

On tabPage1, among other things, I can search for a given name (pic_XXX). When I switch to tabPage2 I'd like it to be scrolled, so the row in which pic_XXX is, is visible. Manually scrolling in tabPage2 is working.

I'm struggling to dynamically scroll tabPage2 to accomplish this. The following solution throws an exception:

Dim pos As Point = tabPage2.Controls.Item("pic_" & imgNum).Location
tabPage2.VerticalScroll.Value = pos.Y
tabPage2.refresh()

I run out of ideas!?


Solution

  • So how to scroll the specified child control into view on an auto-scroll enabled control?

    You should use method .ScrollControlIntoView( [Control] )

    tabPage2.ScrollControlIntoView(tabPage2.Controls.Item("pic_" & imgNum) ) 
    

    Answer for your question:

    Dim pos As Point = tabPage2.Controls.Item("pic_" & imgNum).Location 
    tabPage2.VerticalScroll.Maximum = tabPage2.Height
    tabPage2.VerticalScroll.Value = pos.Y
    tabPage2.PerformLayout()
    

    You have to call .PerformLayout() to make the scrolling control update.