WinForms - Scrolling a panel

Unimatrix_001 used Ask the Experts™

I'm creating my own scrollbar which should be able to be bound to any control and scroll it as necessary (i.e. if the preferred size is greater than the shown size). My problem is that I cannot change the .top property of the bound control when scrollbar is used because the control is often docked and has anchors.

1) Is there some way around this without modifying the .top property?
2) How does an autoscroll Panel manage it when the child components have docked properties set?

Thank you,
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
you could set the dock property to none and resize controls to what they were before you change the docking ... then you can modify the top property.


Hrm, perhaps there isn't another way around it.... Although I don't suppose you can specifically answer the second question?

Thanks very much,
nope sorry - but try this code:

public static void ScrollControls(int Scrolltop, int Scrollleft,  Control parent, List<string> excludeControlsByName)
            foreach (Control control in parent.Controls)

                if (excludeControlsByName.Contains(control.Name)) continue;               
                if (control.Dock != DockStyle.None)
                    var top = control.Top;
                    var left = control.Left;
                    var width = control.Width;
                    var height = control.Height;
                    control.Dock = DockStyle.None;

                    //this may get reset after undocking
                    control.Top = top;
                    control.Left = left;
                    control.Width = width;
                    control.Height = height;

                control.Top += Scrolltop;
                control.Left += Scrollleft;

Open in new window

Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

use it like so:
var excludeControls = new List<string>();

            ScrollControls(10, 0, this, excludeControls);

Open in new window


Thank you. :)

I'm guessing that docking and anchoring just won't work - I can only guess that the Panel some something special internally... :(



I'm not sure about the panel, however I would guess that they do something similar as all object eventually inheritfrom cwnd anyway :)

If you are really keen to find out you could use redgates reflector and decomile the windows.forms.controls dll - then find the panel and check the code out.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial