![](/img/trans.png)
[英]Visual Inheritance - Add controls at designer to a Panel hosted in TableLayoutPanel
[英]Fast way to add Controls to a TableLayoutPanel
我必須將大量Control
對象動態地添加到TableLayoutPanel
,這要花費大量時間。 我還需要能夠通過TableLayoutPanel
行索引和列索引訪問控件,反之亦然。
TableLayoutPanel.Controls
(據我所知)具有3種添加Control
對象的方法:
.Add(Control)
-繼承,位置為.GetCellPosition(Control)
-1,-1 .Add(Control, column, row)
-位置和索引正確,但可能有點慢? .AddRange (Control[])
-繼承的,更快的,顯示的位置是正確的(填充了每個單元格,如果需要,則隨后設置了列跨度),但是使用.GetCellPosition(Control)
位置為-1,-1 有沒有一種方法可以結合.Add(Control, column, row
和.AddRange(Control[])
的優點,即快速將許多Control
對象添加到TableLayoutPanel
同時仍然能夠以編程方式獲取Control
的位置?
編輯以包括評論中的一些信息:
SuspendLayout()
和ResumeLayout()
TableLayoutPanel
大約需要2秒鍾才能加載。 根據分析器,大約50%的時間用於添加控件,而20%的時間用於ResumeLayout()
編輯:MCVE
我的原始代碼更加復雜,但這是TableLayoutPanel
的示例,其中添加控件花費了大部分時間(2/3)。 我正在尋找加快速度的方法。
public class FormTLPTest : Form
{
public FormTLPTest()
{
Height = 800;
Width = 800;
TableLayoutPanel tlp = new TableLayoutPanel();
tlp.Dock = DockStyle.Fill;
tlp.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;
tlp.AutoScroll = true;
Controls.Add(tlp);
tlp.ColumnCount = 7;
tlp.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 20));
tlp.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100.0F));
tlp.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 80));
tlp.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 100));
tlp.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 30));
tlp.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 70));
tlp.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 20));
tlp.SuspendLayout();
for (int i = 0; i<700; i++)
{
Button btn1 = new Button();
Label lb2 = new Label();
Label lb3 = new Label();
Label lb4 = new Label();
TextBox tb5 = new TextBox();
Button btn6 = new Button();
Button btn7 = new Button();
foreach (Control c in new Control[] { btn1, lb2, lb3, lb4, tb5, btn6, btn7})
{
c.Margin = new Padding();
c.Dock = DockStyle.Fill;
c.BackColor = Color.White;
}
btn1.FlatStyle = FlatStyle.Flat;
btn6.FlatStyle = FlatStyle.Flat;
btn7.FlatStyle = FlatStyle.Flat;
btn1.Text = "1";
lb2.Text = "Some longer Text - it contains information. Don't know what I should write to fill the space";
lb3.Text = "Short Text";
lb4.Text = "Short Text";
tb5.Text = "5";
btn6.Text = "Button";
btn7.Text = "+";
tlp.Controls.Add(btn1, 0, i);
tlp.Controls.Add(lb2, 1, i);
tlp.Controls.Add(lb3, 2, i);
tlp.Controls.Add(lb4, 3, i);
tlp.Controls.Add(tb5, 4, i);
tlp.Controls.Add(btn6, 5, i);
tlp.Controls.Add(btn7,6, i);
}
tlp.ResumeLayout();
}
}
查看參考源 ,您可以看到Control.ControlCollection.AddRange
方法只不過是SuspendLayout
/ ResumeLayout
包含的Add
循環。 由於您的代碼也包含在此類調用中,因此性能不應有差異。
TableLayoutPanel
還會執行另外兩個調用-SetRow和SetColumn ,因此我首先想到的是它們是較慢的部分。 但是,查看源代碼(並測量調用的時間或不進行這些調用的時間),當布局引擎掛起時,它們對性能的影響可以忽略不計。
我根本不使用TableLayoutPanel
對您的mcve
進行了一些其他測試,而只是向表單本身添加了控件。 結論是-您的控件太多了。 mcve
正在創建4900個控件。 對於WinForms(通常是Windows)來說,這太多了。 運行它之后,我的Windows幾乎消失了。
因此,添加控制性能無法提高。 但這不是您主要關心的問題。 考慮切換到DataGridView
或一些第三方數據轉發器控件,這些控件支持很多行,而不會創建大量控件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.