这是在 a 中交换控件的代码TableLayoutPanel
- 您有两个选择。
1)通过参考控制交换:
Private Sub SwapControls(tlp As TableLayoutPanel, ctl1 As Control, ctl2 As Control)
Dim ctl1pos As TableLayoutPanelCellPosition = tlp.GetPositionFromControl(ctl1)
tlp.SetCellPosition(ctl1, tlp.GetPositionFromControl(ctl2))
tlp.SetCellPosition(ctl2, ctl1pos)
End Sub
它不依赖于控件所在的位置TableLayoutPanel
- 可能是不同的行、列或两者。
示例用法:
SwapControls(TableLayoutPanel1, Button1, Button2)
2)按列/行索引交换:
Private Sub SwapControls(tlp As TableLayoutPanel, pos1 As TableLayoutPanelCellPosition, pos2 As TableLayoutPanelCellPosition)
Dim ctl1 As Control = tlp.GetControlFromPosition(pos1.Column, pos1.Row)
Dim ctl2 As Control = tlp.GetControlFromPosition(pos2.Column, pos2.Row)
SwapControls(tlp, ctl1, ctl2)
End Sub
示例用法:
SwapControls(TableLayoutPanel1, New TableLayoutPanelCellPosition(0, 0), New TableLayoutPanelCellPosition(1, 0))
解决方案基于MSDN 上的TableLayoutPanel.SetRow帮助文章以及对其反编译表示的一些研究。两者都经过测试并被认为有效。