Home > Net >  How to add scrolling to custom control in winforms
How to add scrolling to custom control in winforms

Time:05-23

I am creating a custom control which can display items inside of itself, so if there are items that are offscreen, i want to user to be able to scroll trough them.

But i can't seem to find any properties or flags in UseStyle() in standard Control class. So how do i create a scrollbar for control?

I know that i can create separate instance of VScrollBar class, and draw it as separate control, but it seems for me as inefficient or wrong way of doing so, i would appreciate suggestions for a way of how to solve this. Thanks.

CodePudding user response:

As comments suggested, i've derived my class from ScrollableControl and used AutoScroll property to make scrolling logic into my control.

Sadly Microsoft documentation about scrolling

Hope it help anyone who stumbles on this exact same problem as i did.

CodePudding user response:

If I understand your question, this is what has worked for me to get the scrolling behavior:

If a simple Panel control containing 3 child controls is set to Dock.Fill to the main form with the AutoScroll property set to true it behaves as shown below when the main window is resized to where the content doesn't fit anymore.

As stated in Hans' comment, If the user control inheritance is CustomUserControl : Panel then it will manifest this behavior.

enter image description here

  • Related