Sally
Sally

Reputation: 1789

Panels side by side taking up 50% width each

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

Answers (4)

MarkusT
MarkusT

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

Dyppl
Dyppl

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

Sum Settavut
Sum Settavut

Reputation: 1

Check SplitterDistance Property. Override form resize and set this value = form.width / 2;

See more. https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.splitcontainer.splitterdistance?view=netframework-4.8

Upvotes: 0

Bala R
Bala R

Reputation: 109017

Use TableLayoutPanel with one row(100%) and two columns (50% each).

Upvotes: 71

Related Questions