Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to scroll to the bottom of WinForm

I am using visual studio 2012 for this. Basically I have a WinForm that I want to expand.

Inside the form designer, I am able to see that my form has a scroll bar, but when I compile the program, the scroll bar does not appear. The controls that are beyond my screen size are clipped off, as opposed to having a scrollbar.

Are there any settings that I have missed out? Currently I set my AutoScroll = true.

like image 454
John Tan Avatar asked Sep 06 '25 18:09

John Tan


1 Answers

Scrollbars show up when a parent control has the AutoScroll set to true and a child control has a MinimumSize such that the client area of the child control is larger than the client area of the parent control.

E.g.

[STAThread]
static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);

    var sampleForm = new Form() { AutoScroll = true };

    Panel panel = new Panel() { BackColor = Color.Red, AutoSizeMode = AutoSizeMode.GrowAndShrink, AutoSize = true };
    Button btn = new Button { Text = "Toggle MinSize", AutoSize = true };
    panel.Controls.Add(btn);

    btn.Click += delegate {
        if (panel.MinimumSize == Size.Empty)
            panel.MinimumSize = new Size(600,600);
        else
            panel.MinimumSize = Size.Empty;
    };

    sampleForm.Controls.Add(panel);
    Application.Run(sampleForm);
}

If your child panel correctly calculates its preferred size, then you can override the MinimumSize property and return the PreferredSize.

like image 125
Loathing Avatar answered Sep 09 '25 09:09

Loathing