1

I am using tablelayoutpanelin my WinForm project, it responds slowly to events. I referred to this link to make tablelayoutpanel render faster.

In my code, all the cells in tablelayoutpanel are of equal size(100%), I have added a picturebox in each cell and it's SizeMode property to stretch.The number of cells are just 9 and when i resize the panel it is slow.
The code is as shown below. If i create 300 cells it takes 4 seconds to resize.

Void change_tableLayout(int num) //num =9
 {
     CoTableLayoutPanel ctlp = gcnew CoTableLayoutPanel(); //inherited tablelayoutpanel as described in above link
     global_ctlp.Cursor = System.Windows.Forms.Cursors.WaitCursor;     
     num = Math.Sqrt(num);
     global_ctlp.Controls.Clear();
     global_ctlp.RowStyles.Clear();
     global_ctlp.ColumnCount = num;
     global_ctlp.RowCount = num;
         for(int i = 0; i < num; i++) //row cell
             {
                 RowStyle rs = gcnew RowStyle(SizeType::Percent, 50);
                 global_ctlp.RowStyles.Add(rs);
                    for (int j = 0; j < num; j++) //column cell
                       {
                         global_ctlp.ColumnStyles.Add(gcnew ColumnStyle(SizeType.Percent, 50));
                         PictureBox pictureBox1 = gcnew PictureBox();
                         pictureBox1.Dock = System.Windows.Forms.DockStyle.Fill;
                         pictureBox1.Image = Image.FromFile("image_path");
                         global_ctlp.Controls.Add(pictureBox1, i, j);
                         pictureBox1.Margin = System.Windows.Forms.Padding(0);
                         pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
                     }
             }
     global_ctlp.Cursor = System.Windows.Forms.Cursors.Default;
 }

Can this be improved further. How do i make this faster? or Is there any better component other than tablelayoutpanel.

Community
  • 1
  • 1
Suraksha Ajith
  • 872
  • 2
  • 12
  • 23
  • If you're trying to just add images you could use Listview – Adarsh Ravi Apr 04 '17 at 05:23
  • Thanks for your suggestion but I want bigger display of pictures(and they should be of equal size hence i have given the cell size as equal) LIstview only list the filenames. – Suraksha Ajith Apr 04 '17 at 05:28
  • Not true list view can be used to add images as well. I am unsure about the size it will let you display but I am pretty sure about listview letting you add images as I have implemented the same for one of the applications I have worked on. – Adarsh Ravi Apr 04 '17 at 05:30
  • You will have to add an ImageList to your form and then the ListView has a property LargeImageList where in you can refer to the ImageList added. It will allow you to have images of size upto 256 X 256 – Adarsh Ravi Apr 04 '17 at 05:37
  • Try using a DataGridView and the CellPaint event. – LarsTech Apr 04 '17 at 18:57
  • 1
    Possible duplicate of [TableLayoutPanel responds very slowly to events](https://stackoverflow.com/questions/8900099/tablelayoutpanel-responds-very-slowly-to-events) – Jim Fell Mar 07 '19 at 22:58

0 Answers0