How to add new element in a specific position (in pixels) without complying with TableLayoutPanel rules?

三世轮回 提交于 2019-12-08 13:29:36

问题


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!



来源:https://stackoverflow.com/questions/39139047/how-to-add-new-element-in-a-specific-position-in-pixels-without-complying-with

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!