有没有办法从 TableLayoutPanel 中的一行中删除所有控件?

发布于 2024-11-11 15:40:40 字数 558 浏览 6 评论 0原文

我动态生成 TableLayoutPanel 的控件。我的每一行都有一个删除按钮。当我点击它时,该行必须被删除。

    Dim removeBtn As New Button
    AddHandler removeBtn.Click, AddressOf DeleteRow
    tlp.Controls.Add(removeBtn, 5, rowCount)

我没有显示添加与上面类似的文本框的代码。我可以获得单击按钮的行号。使用它,如何从该行中删除所有控件。

Private Sub DeleteRow(ByVal sender As System.Object, ByVal e As System.EventArgs)
   Dim currentRow As Integer = CType(CType(sender, Button).Parent, TableLayoutPanel).GetRow(CType(sender, Button))
   'Using this currentRow, how to delete this Row
End Sub

I generate controls for a TableLayoutPanel dynamically. I have a delete button in each row. When I click that, that row has to be removed.

    Dim removeBtn As New Button
    AddHandler removeBtn.Click, AddressOf DeleteRow
    tlp.Controls.Add(removeBtn, 5, rowCount)

I have not shown the code to add text boxes which are similar to above. I can get the row number of the clicked button. Using this, how to remove all controls from this row.

Private Sub DeleteRow(ByVal sender As System.Object, ByVal e As System.EventArgs)
   Dim currentRow As Integer = CType(CType(sender, Button).Parent, TableLayoutPanel).GetRow(CType(sender, Button))
   'Using this currentRow, how to delete this Row
End Sub

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

陌上芳菲 2024-11-18 15:40:40

基本上,您必须:

  • 从该行获取控件列表并从 TLP 中删除它们
  • 从 TLP 中删除相应的行样式
  • 为删除后的每一行中的每个控件设置新的行索引
  • 递减 RowCount

这是 VB。 NET 代码执行相同的操作。

Public Sub RemoveRow(ByRef panel As TableLayoutPanel, ByRef rowIndex As Integer)

    panel.RowStyles.RemoveAt(rowIndex)
    Dim columnIndex As Integer
    For columnIndex = 0 To panel.ColumnCount - 1
        Dim Control As Control = panel.GetControlFromPosition(columnIndex, rowIndex)
        panel.Controls.Remove(Control)
    Next
    Dim i As Integer
    For i = rowIndex + 1 To panel.RowCount - 1
        columnIndex = 0
        For columnIndex = 0 To panel.ColumnCount - 1
            Dim control As Control = panel.GetControlFromPosition(columnIndex, i)
            panel.SetRow(control, i - 1)
        Next
    Next
    panel.RowCount -= 1
End Sub

这里有一个 C# 扩展方法可以为您完成此操作。

public static void RemoveRow(this TableLayoutPanel panel, int rowIndex)
{
    panel.RowStyles.RemoveAt(rowIndex);

    for (int columnIndex = 0; columnIndex < panel.ColumnCount; columnIndex++)
    {
        var control = panel.GetControlFromPosition(columnIndex, rowIndex);
        panel.Controls.Remove(control);
    }

    for (int i = rowIndex + 1; i < panel.RowCount; i++)
    {
        for (int columnIndex = 0; columnIndex < panel.ColumnCount; columnIndex++)
        {
            var control = panel.GetControlFromPosition(columnIndex, i);
            panel.SetRow(control, i - 1);
        }
    }

    panel.RowCount--;
}

Basically you have to:

  • Get the list of controls from that row and delete them from the TLP
  • Remove the corresponding row style from the TLP
  • Set the new row index for every control in every row after the deleted one
  • Decrement the RowCount

Here is the VB.NET code to do the same.

Public Sub RemoveRow(ByRef panel As TableLayoutPanel, ByRef rowIndex As Integer)

    panel.RowStyles.RemoveAt(rowIndex)
    Dim columnIndex As Integer
    For columnIndex = 0 To panel.ColumnCount - 1
        Dim Control As Control = panel.GetControlFromPosition(columnIndex, rowIndex)
        panel.Controls.Remove(Control)
    Next
    Dim i As Integer
    For i = rowIndex + 1 To panel.RowCount - 1
        columnIndex = 0
        For columnIndex = 0 To panel.ColumnCount - 1
            Dim control As Control = panel.GetControlFromPosition(columnIndex, i)
            panel.SetRow(control, i - 1)
        Next
    Next
    panel.RowCount -= 1
End Sub

Here is a C# extension method that will do this for you.

public static void RemoveRow(this TableLayoutPanel panel, int rowIndex)
{
    panel.RowStyles.RemoveAt(rowIndex);

    for (int columnIndex = 0; columnIndex < panel.ColumnCount; columnIndex++)
    {
        var control = panel.GetControlFromPosition(columnIndex, rowIndex);
        panel.Controls.Remove(control);
    }

    for (int i = rowIndex + 1; i < panel.RowCount; i++)
    {
        for (int columnIndex = 0; columnIndex < panel.ColumnCount; columnIndex++)
        {
            var control = panel.GetControlFromPosition(columnIndex, i);
            panel.SetRow(control, i - 1);
        }
    }

    panel.RowCount--;
}
燕归巢 2024-11-18 15:40:40

除了 Johann 和 emaillenin 的答案之外,您还应该将以下行更改

    panel.SetRow(control, i - 1);

为“To this

    if (control != null) panel.SetRow(control, i - 1);

Empty fields”,如果没有检查 null,跨度控件也会在此处抛出错误。

In addition to Johann and emaillenin's answers you should change the following line

    panel.SetRow(control, i - 1);

To this

    if (control != null) panel.SetRow(control, i - 1);

Empty fields and also spanned controls will throw an error here if theres no check for null.

葬花如无物 2024-11-18 15:40:40

何必这么辛苦...
使用
tableLayoutpanel1.Controls.Clear()

这将清除表格面板的内容

Why this much hardwork...
Use
tableLayoutpanel1.Controls.Clear()

This will clear the contents of table panel

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文