简体   繁体   中英

Constrain the movement of a SplitContainer in C#?

I have a SplitContainer in my form.
On the 1st panel I have a TreeView and a ListView on the 2nd. (Classic)
Now I want to limit the size of the 1st panel (with the TreeView ) to 250 pixels wide.
I wish to block the separator from moving too much (or too less).
How do I do that?

您可以使用SplitContainer.Panel1MinSize属性。

SplitContainer1.Panel1MinSize = 250;

First, if you want to constrain the TreeView to be EXACTLY 250px, set the FixedPanel to be Panel1, set the IsSplitterFixed property to True, and set the Panel1MinSize to 250. This basically makes the split graphical only; the splitter will default to a size large enough for the TreeView, and will not move.

If you want to constrain the TreeView to be AT LEAST 250px, simply set Panel1MinSize to be 250. This will prevent the user from making the panel SMALLER than that, though they can make it LARGER. There is no maximum constraint, but you can get the effect of one by setting a maximum size for the window and a minimum size for the other panel of the SplitContainer.

Just a little addition.

Here is the code to place in the frmMain_Load() (or whereever). In the code, the minimum is 250 pixels and the maximum is 400 pixels.

this.splitContainer1.Panel1MinSize = 250;
this.splitContainer1.Panel2MinSize = this.splitContainer1.Width - 400;

Do not forget to place the same code in the resize event frmMain_Resize()

I guess you should take look at the FixedPanelProperty of the splitContainer. I allows you to only let the other panel grow and shrink on rezise operations: The resizing is much smoother.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM