How to add new element in a specific position (in

2019-06-11 03:32发布

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条回答
走好不送
2楼-- · 2019-06-11 04:27

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);

enter image description here

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!

查看更多
登录 后发表回答