将 PictureBoxes 添加到 TableLayoutPanel 非常慢 [英] Adding PictureBoxes to TableLayoutPanel is very slow
问题描述
大家好,我正在将 PictureBoxes
添加到 TableLayoutPanel
单元格,但是对于 10x10 网格需要 10 秒,这太长了.我的代码看起来像这样,你能告诉我如何改进它,这样 PictureBoxes
会立即添加吗?这是我的做法:
Hello guys I am adding PictureBoxes
to TableLayoutPanel
cells, but for 10x10 grid it takes like 10 seconds, which is too long. My code looks like this, could you tell me how can i improve it, so PictureBoxes
will be added instantly? Here is how i do it:
private void x10ToolStripMenuItem_Click(object sender, EventArgs e)
{
tableLayoutPanel1.Controls.Clear();
tableLayoutPanel1.ColumnStyles.Clear();
tableLayoutPanel1.RowStyles.Clear();
tableLayoutPanel1.ColumnCount = 10;
tableLayoutPanel1.RowCount = 10;
int sliderval = trackBar1.Value;
//tableLayoutPanel1.Controls.Add(new PictureBox(),2,1);
for (int i = 0; i < tableLayoutPanel1.RowCount; i++)
{
tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, (100 / tableLayoutPanel1.RowCount)));
for (int j = 0; j < tableLayoutPanel1.ColumnCount; j++)
{
tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, (100 / tableLayoutPanel1.ColumnCount)));
PictureBox picture = new PictureBox
{
Name = "pictureBox" + i,
Size = new Size(49, 35),
Visible = true,
Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right))),
BackColor = colors[random.Next(0, sliderval)],
TabIndex = 0,
TabStop = false
};
tableLayoutPanel1.Controls.Add(picture, j, i);
picture.Dock = DockStyle.Fill;
// picture.Margin = new Thickness(40, 16, 0, 0);
// picture.Padding = new Padding(0);
picture.SizeMode = PictureBoxSizeMode.Normal;
picture.Margin = new Padding(0);
}
}
AssignClickEvent();
}
推荐答案
看看这个 URL.可能是您的表单需要重新粉刷 alm 并将其拖慢.而是暂停绘画.完成所有更改,然后恢复它.少画.http://www.c-sharpcorner.com/Forums/Thread/52/一个>
Take a look at this URL. It might be that your form requires repainting alm the time and sloes it down. Rather pause the painting. Do all your changes and then resume it. Less painting. http://www.c-sharpcorner.com/Forums/Thread/52/
这篇关于将 PictureBoxes 添加到 TableLayoutPanel 非常慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!