将PictureBoxes添加到TableLayoutPanel非常慢

时间:2013-03-25 19:49:23

标签: c# winforms tablelayoutpanel

大家好我将PictureBoxes添加到TableLayoutPanel个单元格,但是对于10x10网格,它需要10秒,这太长了。我的代码看起来像这样,你能告诉我如何改进它,所以PictureBoxes会立即添加吗?我是这样做的:

 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();
        }

1 个答案:

答案 0 :(得分:0)

看看这个网址。可能是你的表格需要重新涂抹时间并且不屑一顾。而是暂停画画。完成所有更改,然后重新开始。减少绘画。 http://www.c-sharpcorner.com/Forums/Thread/52/