Anchoring - Make two components take up half of panel each

Viewed 11058

What I have:

I have a panel (the white space), and two DataGridViews represented by the green and blue squares. The panel is anchored to take up most of the center of my screen, and grows/shrinks with the window size.

What I need:

I would like the green square to always stay with it's right border in the middle of the screen and take up the left half of the screen. Equally, I'd like the blue square to stay with its left border in the middle and to take up the right half of the screen.

Basically, I just want it to always look like this image regardless.

Do I need to do this programmaticly? I can't seem to find a combination of anchoring or docking that makes this happen, and adding more panels as containers yields the same issue in the end.

DataGridViews In Panel Layout

3 Answers

I tried the solution with the TableLayoutPanel, which works ok.

But layouting inside the TableLayoutPanel is a bit unhandy and restricted if you want to use different positioning.

I found another solution which does it with a little of programming effort:

  • For the left item define anchoring to left
  • For the right item define no left/right anchoring

This leads into the left item remaining at its location when resizing the form and the right to move staying about the middle location.

The I added an OnSizeChanged handler to the form, which implements these lines:

int widthForItem = Item2.Left - Item1.Left; // you can subtract a distance here
Item1.Width = widthForEachItem;
Item2.Width = widthForEachItem
Related