0

I have several panels in a windows form application, they are sorted in two columns and maximally 4 rows, so maximally 8 panels. The number of elements included in every single panel changes during the runtime, so not to waste place on monitor I set all of them to autosize. The problem is, I dont know how I can keep them placed correctly, like how to make that when the first one shrinks that the other three come up a bit so there is not too much space between them.

1
  • What control do you use now for placing panels in four rows and two columns? If first one panel shrinks - which panels should change their size - panels from current row, or from column? Dec 8, 2012 at 20:35

1 Answer 1

2

Try to use the TableLayoutPanel or the FlowLayoutPanel (or possibly even a SplitContainer). They all can be very useful for this kind of task. You find them in the section Containers in the Toolbox. You can keep the right distance by setting the margins of the panels appropriately. The TableLayoutPanel gives you different options for sizing the rows and columns (absolute or percent size or auto). Also by working with the Dock or the Anchor properties of the panels and controls you can attain a dynamic behavior when resizing or adding and removing controls.

You might also have to set the MinimumSize and MaximumSize properties of the controls.

You can add controls like this the TableLayoutPanel

int count = tableLayoutPanel1.Controls.Count;
int newColumn = count % 2;
int newRow = count / 2;
if (newRow >= tableLayoutPanel1.RowCount) {
    tableLayoutPanel1.RowCount++;

    // Set appropriate row style
    tableLayoutPanel1.RowStyles.Add(new RowStyle { SizeType = SizeType.AutoSize });
}
var newControl = new Button { Dock = DockStyle.Fill };
tableLayoutPanel1.Controls.Add(newControl, newColumn, newRow);
3
  • @ lazyberezovsky: This is a visual thing, not so much a code thing. If the panels have the right width, they will wrap to the next row automatically after two columns in the FlowLayoutPanel. Dec 8, 2012 at 20:38
  • Yes, it's not code thing, but saying that you can use TableLayoutPanel for controls positioning is like saying that you can use Linq to load customer data from database :) Dec 8, 2012 at 20:56
  • @lazyberezovsky: I addd an example for the TableLayoutPanel. Dec 8, 2012 at 21:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.