-->

How to add new element in a specific position (in

2019-06-11 03:49发布

问题:

I have set a TableLayoutPanel as a layout to organise my inputs. That was a good idea up to the moment when I realised that I had to add new elements that are not supposed to be in a specific cell in the table (They must span across table cells, but cells have to remain untouched)

回答1:

You need to set the starting cell for the control

Panel pan = new Panel() {BackColor = Color.Red, Parent = tableLayoutPanel1 };

either like this:

tableLayoutPanel1.SetCellPosition(pan, new TableLayoutPanelCellPosition(1, 2));

or like this:

tableLayoutPanel1.SetColumn(pan, 1);
tableLayoutPanel1.SetRow(pan, 2);

Then you can set the spans from that position like this:

tableLayoutPanel1.SetColumnSpan(pan, 3);
tableLayoutPanel1.SetRowSpan(pan, 2);

And finally you can fine-tune the position at pixel-level by setting the control's Margin:

  pan.Margin = new Padding(55, 5, 0, 0);

Note however that a spanned range acts like a cell: It can only contain one control, so you cannot place any other Control in the spanned range! If you need more controls in the same range you need to nest them all in a Panel or other Container!