C# 在TableLayoutPanel Visual Studio中交换控件

C# 在TableLayoutPanel Visual Studio中交换控件,c#,vb.net,visual-studio-2010,C#,Vb.net,Visual Studio 2010,我需要交换TableLayoutPanel中的控件。它们是分开的一排。我尝试了建议的代码,但没有用。除了删除所有控件并重新添加外,还有其他解决方案吗?答案可以是C#或VB 在四处搜寻了一天,没有发现任何东西后,我终于在黑暗中找到了答案。您需要使用表中控件的SetChildIndex。见下文 注意:只有在将控件添加到TableLayoutPanel而没有行或列索引时,此选项才有效 Private Sub Form1_Load(sender As System.Object, e As System

我需要交换TableLayoutPanel中的控件。它们是分开的一排。我尝试了建议的代码,但没有用。除了删除所有控件并重新添加外,还有其他解决方案吗?答案可以是C#或VB


在四处搜寻了一天,没有发现任何东西后,我终于在黑暗中找到了答案。您需要使用表中控件的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

以下是在
表格布局面板中交换控件的代码-您有两个选项

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
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
它不取决于控件在
TableLayoutPanel
中的位置-可以是不同的行、列或两者

示例用法:

SwapControls(TableLayoutPanel1, Button1, Button2)
SwapControls(TableLayoutPanel1, New TableLayoutPanelCellPosition(0, 0), New TableLayoutPanelCellPosition(1, 0))
2) 按列/行索引交换

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
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, Button1, Button2)
SwapControls(TableLayoutPanel1, New TableLayoutPanelCellPosition(0, 0), New TableLayoutPanelCellPosition(1, 0))

解决方案基于MSDN上的帮助文章以及对其反编译表示的一些研究。这两个控件都经过测试并被视为正常工作。

如果您有固定长度的
TableLayoutPanel
,您可以使用
FlowLayoutPanel
(那么it控件的长度必须等于
FlowLayoutPanel
长度):

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

@Neolisk如果您添加了具有行和列索引的控件,那么您是对的,在所使用的示例中,添加这些控件时没有添加索引。所以当我使用Controls.Add(UCName)语法添加UserControls时,它确实在我的工作应用程序中起作用。你的方法是正确的,尽管在所有情况下,今天我需要的正是这个:)感谢上帝,我找到了。我还得用c#翻译,但很简单。非常感谢。记住也要交换索引。