0

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();
        }
Saber Amani
  • 6,409
  • 12
  • 53
  • 88
e0n
  • 41
  • 2
  • 3
  • 3
    This is one of the few cases where calling the control's SuspendLayout() method is important. – Hans Passant Mar 25 '13 at 20:30
  • Not sure why Hans didn't post that an answer. WinForms is not "too slow", whatever that means. And even if it were, a few picture boxes on a panel doesn't count as a "graphics-intensive application". People have been making Windows applications for years using the technology that underlies WinForms and computers have gotten nothing but faster since then. If it used to be workable, it's certainly workable now. You just have to know how to use it. Begin the method by calling `tableLayoutPanel1.SuspendLayout()` and end it by calling `tableLayoutPanel.ResumeLayout()`. Look up the docs to see why. – Cody Gray - on strike Mar 25 '13 at 21:23
  • thank you very much Hans Passant it helped :) – e0n Mar 25 '13 at 21:33
  • @CodyGray Didn't see your hackforms comment here... Im still waiting to reward you with a bounty on [this one](http://stackoverflow.com/questions/15532639/complex-ui-inside-listboxitem) – Federico Berasategui Mar 28 '13 at 03:19
  • @HighCore Sorry, I'm not playing that game. It's rigged. As soon as I waste a bunch of time posting a solution, the target will change. Besides, I don't think you're really interested in the answer, and that's the only reason I answer questions here—to help people. I couldn't care less about reputation. The [two bounties I've earned](http://stackoverflow.com/users/366904/cody-gray?tab=bounties&sort=earned), I earned accidentally. If you're really looking for a good answer, might I suggest asking only one question per question? You have **8** there. – Cody Gray - on strike Mar 28 '13 at 17:04
  • @CodyGray you're right. The good thing is that [every day more developers are coming into the conclusion that winforms is useless and deprecated](http://stackoverflow.com/q/15681352/643085) – Federico Berasategui Mar 28 '13 at 17:12
  • @HighCore Yes, heaven and earth rejoice! Good thing I don't have any stock invested in WinForms. When everyone stops using it, I won't have to answer any more questions. Pray tell, what crusade will you embark on next?! Getting everyone to switch to OS X? I hear Core Graphics and Quartz is *even more advanced*. – Cody Gray - on strike Mar 28 '13 at 17:34
  • @CodyGray it will suffice for me, and I will rest peacefully when I have conviced **you** to switch to WPF. =) – Federico Berasategui Mar 28 '13 at 17:42

1 Answers1

0

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/

Ryan Gunn
  • 1,161
  • 7
  • 18