Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maximum Hight of a panel in Split Container

How can I set the minimum & maximum height of a panel in a horizontal split container in my C# form?

like image 711
Bishwajyoti Roy Avatar asked Apr 12 '16 14:04

Bishwajyoti Roy


3 Answers

I realize this question is old, but I couldn't find a suitable answer anywhere I looked. Some people suggested setting Panel1 as a fixed panel, which was not what I wanted. I solved this issue by using by using the splitcontainers sizeChanged and SplitterMoved events:

    private const int Panel1MaxWidth = 1075;
    private void splitContainer1_SizeChanged(object sender, EventArgs e)
    {
        if(splitContainer1.Panel1.Width > Panel1MaxWidth)
        {
            splitContainer1.SplitterDistance = Panel1MaxWidth;
        }
    }

    private void splitContainer1_SplitterMoved(object sender, SplitterEventArgs e)
    {
        if (splitContainer1.Panel1.Width > Panel1MaxWidth)
        {
            splitContainer1.SplitterDistance = Panel1MaxWidth;
        }
    }

Just set the Pane1MaxWidth constant to whatever you want your maximum size of Panel1 to expand out to.

like image 171
PaladinMattt Avatar answered Oct 15 '22 06:10

PaladinMattt


SplitContainer has 2 fields: Panel1MinSize and Panel2MinSize. To set the maximum size for panel1 just set the appropriate min size for panel2.

like image 1
Artem Avatar answered Oct 15 '22 07:10

Artem


Irrespective of where the Panel is, you could normally specify the maximum height and width by doing:

panel1.MaximumSize = new Size(300, 300); //max 300 x 300

If you use SplitContainer and your Panel is inside the SplitContainer, and you want to change it while it is in the SplitContainer, however, you might need to identify if the Panel is in the Panel1 or Panel2 of the SplitContainer before you specify the max height and width as above. Something like this:

//assuming the name "panel1" in the Panel1 of the SplitContainer
Panel panel = splitContainer1.Panel1.Controls["panel1"];
panel.MaximumSize = new Size(300, 300); //max 300 x 300

However, if what you want is to change the splitContainer size itself, you could apply the MaximumSize for the splitContainer as well:

splitContainer1.MaximumSize = new Size(300, 300);

Or, if you want to change the splitContainer.Panel1 or splitContainer.Panel2, you could also try to play with SplitContainer.SplitterDistance property.

like image 1
Ian Avatar answered Oct 15 '22 06:10

Ian