清除没有循环的 TableLayoutPanel 的列?

clear a column of a TableLayoutPanel without loops?

我有一个包含两列的 TableLayoutPanel。我想从第二个中动态删除所有控件。有没有简单的方法?我真的不想要乏味的循环之类的东西。

编辑: "looping" 我的意思是实际编写 for 循环。在幕后循环的 LINQ 解决方案非常好。

这在很大程度上取决于您的意思:"clear a column"。对于此示例,我选择将可见性设置为 false

这看起来真的很糟糕:

// grab all controls from Colum 2 (index == 1)
List<Control> Col_2_Stuff = tableLayoutPanel1.Controls.OfType<Control>()
             .Where(x => tableLayoutPanel1.GetPositionFromControl(x).Column == 1).ToList();

// make them invisible
Col_2_Stuff.Select(c => { c.Visible = false; c = null; return c; }).ToList();

但它完成了工作

编辑:

这是实际删除它们的行:

Col_2_Stuff.Select(c => { tableLayoutPanel1.Controls.Remove(c); return c; }).ToList();

受到@LarsTech 的启发:你也可以在之后调用 dispose 并清除列表

Col_2_Stuff.Select(c => { tableLayoutPanel1.Controls.Remove(c); c.Dispose(); return c; }).ToList();

Col_2_Stuff.Clear();