我只是想在窗口表单上交换两个按钮,没关系,但是当我在Panel
,GroupBox
或TableLayoutPanel
上放置按钮时,没有任何反应。
我尝试使用以下代码片段进行交换:
System.Drawing.Point locationTemp = button2.Location;
button2.Location = button1.Location;
button1.Location = locationTemp;
我该如何解决这个问题?
答案 0 :(得分:1)
您的代码有效,除非这些控件位于TableLayoutPanel中,由于子控件是单元格的成员(每个单元格只允许一个父控件),因此行为不同:
TableLayoutPanelCellPosition cell1 = tlp.GetCellPosition(button1);
tlp.SetCellPosition(button1, tlp.GetCellPosition(button2));
tlp.SetCellPosition(button2, cell1);