Reputation: 1789
I have two panels: panelA, panelB in a panel: panelContainer.
How do I make panelA and panelB go side by side taking 50% width each of the panelContainer?
Upvotes: 31
Views: 38908
Reputation: 279
You can use the widths of the panels to "split the difference":
// Calculate how much Panel1 is wider (or narrower) than Panel2
var diff = mainSplitter.Panel1.Width - mainSplitter.Panel2.Width;
// Split the difference
mainSplitter.SplitterDistance -= diff / 2;
This has the advantage of being independent of any other setting, e.g. splitter width, borders etc. Basically you're saying: of Panel1 is 40 pixels wider than Panel2, move the splitter 20 pixels to the left.
Upvotes: 0
Reputation: 12381
You can use SplitContainer
instead of panel. Set IsSplitterFixed
to true, in design mode set SplitterDistance
to be half of SplitContainer
's width and set the SplitterWidth
to 1. Make sure that FixedPanel
is set to none. Then at runtime it will maintain the ratio of panels widths.
The only problem is that you can't set SplitterWidth
to zero so there will always be a slight distance between panels. If that's not a problem and if you don't need the panelContainer
to actually be a panel for some reason, that's the way I would do it.
Upvotes: 23
Reputation: 1
Check SplitterDistance Property. Override form resize and set this value = form.width / 2;
Upvotes: 0
Reputation: 109017
Use TableLayoutPanel with one row(100%) and two columns (50% each).
Upvotes: 71