Reputation: 1131
How can I set the minimum & maximum height of a panel in a horizontal split container in my C# form?
Upvotes: 6
Views: 5498
Reputation: 161
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.
Upvotes: 5
Reputation: 30813
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.
Upvotes: 1
Reputation: 1575
SplitContainer has 2 fields: Panel1MinSize and Panel2MinSize. To set the maximum size for panel1 just set the appropriate min size for panel2.
Upvotes: 1