![](/img/trans.png)
[英]Centering Windows Forms Controls inside TableLayoutPanel with Visual Studio Designer
[英]Swap controls in TableLayoutPanel Visual Studio
我需要在TableLayoutPanel中交换控件。 它们分开排列。 我尝试了建议的代码但无济于事。 除了删除所有控件并重新添加之外,还有其他解决方案吗? 答案可以是C#或VB。
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
Dim c1 As Control = Me.tlp.GetControlFromPosition(0, 0)
Dim c2 As Control = Me.tlp.GetControlFromPosition(0, 1)
If c1 IsNot Nothing And c2 IsNot Nothing Then
Me.tlp.SetRow(c2, 0)
Me.tlp.SetRow(c1, 1)
End If
End Sub
下面是在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))
解决方案基于MSL上的TableLayoutPanel.SetRow帮助文章以及对其反编译表示的一些研究。 两者都经过测试并被认为有效。
如果你有固定长度的TableLayoutPanel
,你可以使用FlowLayoutPanel
(那么它的长度控件必须具有相等的FlowLayoutPanel
长度):
Dim c1 As Control = Button1
Dim c2 As Control = CheckBox1
Dim i1 = FlowLayoutPanel1.Controls.IndexOf(c1)
Dim i2 = FlowLayoutPanel1.Controls.IndexOf(c2)
FlowLayoutPanel1.Controls.SetChildIndex(c1, i2)
FlowLayoutPanel1.Controls.SetChildIndex(c2, i1)
在C#代码中相同:在任何位置1和2交换/交换控件:
private void SwapControls(TableLayoutPanel tlp, TableLayoutPanelCellPosition cpos1, TableLayoutPanelCellPosition cpos2)
{
var ctl1 = tlp.GetControlFromPosition(cpos1.Column, cpos1.Row);
var ctl2 = tlp.GetControlFromPosition(cpos2.Column, cpos2.Row);
if (ctl1 != null) // position1 can be empty
tlp.SetCellPosition(ctl1, cpos2);
if (ctl2 != null) // position2 can be empty
tlp.SetCellPosition(ctl2, cpos1);
}
使用样本:
SwapControls(TableLayoutPanel1, new TableLayoutPanelCellPosition(0, 0), new TableLayoutPanelCellPosition(1, 0))
在寻找了一天并且没有得到任何结果之后,我终于在黑暗中拍摄并找到了答案。 您需要在表中使用控件的SetChildIndex。 见下文...
注意:仅当您向TableLayoutPanel添加控件而没有行或列索引时,此方法才有效。
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
tlp.RowStyles.Clear()
For i As Integer = 0 To 4
Dim txt As New TextBox
txt.Text = i
txt.Name = "txt" & i
tlp.Controls.Add(txt) 'this works
'tlp.Controls.Add(txt, 0, i) 'this will not work when button is clicked
Next
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
Dim c1 As Control = Me.tlp.GetControlFromPosition(0, 0)
Dim c2 As Control = Me.tlp.GetControlFromPosition(0, 1)
If c1 IsNot Nothing And c2 IsNot Nothing Then
tlp.Controls.SetChildIndex(c1, 1)
tlp.Controls.SetChildIndex(c2, 0)
End If
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.