winform使用代码删除tablelayoutpanel的列
时间: 2024-06-02 09:07:55 浏览: 151
您可以使用以下代码删除 WinForms TableLayoutPanel 中的一列:
int columnIndexToRemove = 1; //Replace with the index of column you want to remove
//Remove control in specified column
foreach (Control control in tableLayoutPanel1.Controls)
{
TableLayoutPanelCellPosition position = tableLayoutPanel1.GetPositionFromControl(control);
if (position.Column == columnIndexToRemove)
{
tableLayoutPanel1.Controls.Remove(control);
control.Dispose();
}
}
//Remove Column
for (int i = columnIndexToRemove + 1; i < tableLayoutPanel1.ColumnCount; i++)
{
for (int j = 0; j < tableLayoutPanel1.RowCount; j++)
{
Control control = tableLayoutPanel1.GetControlFromPosition(i, j);
if (control != null)
{
tableLayoutPanel1.SetCellPosition(control, new TableLayoutPanelCellPosition(i - 1, j));
}
}
}
tableLayoutPanel1.ColumnStyles.RemoveAt(tableLayoutPanel1.ColumnCount - 1);
阅读全文