簡體   English   中英

清除沒有循環的TableLayoutPanel的列?

[英]clear a column of a TableLayoutPanel without loops?

我有一個包含兩列的TableLayoutPanel 我想從第二個動態刪除所有控件。 有一個簡單的方法嗎? 我真的不想要繁瑣的循環和類似的東西。

編輯:通過“循環”我的意思是實際上寫for循環。 在幕后循環的LINQ解決方案非常好。

這在很大程度上取決於你的意思:“清除一列”。 我選擇將此可見性設置為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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM