6

我需要在 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
4

4 回答 4

6

这是在 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帮助文章以及对其反编译表示的一些研究。两者都经过测试并被认为有效。

于 2012-11-10T22:10:59.087 回答
0

如果你有固定长度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)
于 2012-11-11T02:14:54.663 回答
0

在 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))
于 2015-09-03T13:00:28.080 回答
-1

找了一天,一无所获后,我终于在黑暗中拍了一张照片,找到了答案。您需要使用表中控件的 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
于 2012-11-10T18:35:44.407 回答