Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Winforms: Scrollbars without AutoScroll

I have a TabPage with stuff in it. A few of my users have tiny screens on which some of that stuff does not fit. When I set AutoScroll true on the TabPage, it adds the scrollbars, as expected. However...

There is a ListBox in this TabPage. The ListBox is positioned such that clicking on it to select a ListItem causes the TabPage to scroll the entire ListBox into view, which in turn causes the click to select the wrong ListItem.

if I disable AutoScroll, the ListBox works correctly but the user has no way to scroll the TabPage.

I have tried adding panels and TableLayoutPanels and messing with various combinations of which have AutoScroll and which do not.

I have tried a DLLImport hack that forced an unstyled scrollbar onto the panel, but that scrollbar didn't do anything and it didn't look like the rest of the scrollbars in the application.

Edit: Note that some users have larger screens. On those screens, there is enough space to display the entire TabPage without scrolling, and it works correctly.

How can I get scrollbars without the auto scroll behavior?

like image 453
tsilb Avatar asked Sep 11 '25 02:09

tsilb


1 Answers

Try creating a new panel control like this:

public class PanelEx : Panel {
  protected override Point ScrollToControl(Control activeControl) {
    return this.DisplayRectangle.Location;
  }
}

Put this panel inside your TabPage and set the Dock property to Fill. Put all of your controls inside that panel instead of the TabPage.

like image 68
LarsTech Avatar answered Sep 12 '25 14:09

LarsTech