1

I have a 50x50 tableLayoutPanel with all the cells empty when the program starts. Them, after clicking new or Load, I add panels to all the cells, with specific background images. The problem is that it is way too slow to perform that, and even after the all thing is set, the program lags while scrolling...

Here's the code that adds the panels to the cells:

private void startLayout(object sender, EventArgs e)
    {
        BeginControlUpdate(tableLayoutPanel1);
        LoadCellsBar.Visible = true;
        Panel p;

        for (int Row = 0; Row <= tableLayoutPanel1.RowCount - 1; Row++)
        {
            for (int Column = 0; Column <= tableLayoutPanel1.ColumnCount - 1; Column++)
            {
                p = new Panel();
                p.BackgroundImageLayout = ImageLayout.Stretch;
                p.Dock = DockStyle.Fill;
                p.Margin = new Padding(1);
                p.BackgroundImage = Symbols.Clearbm;
                p.MouseClick += new MouseEventHandler(Panel_click);
                p.MouseHover += new System.EventHandler(this.ShowCoord);
                p.ContextMenuStrip = MenuPanels;
                tableLayoutPanel1.Controls.Add(p);
            }
            LoadCellsBar.PerformStep();
        }
        LoadCellsBar.Visible = false;
        LoadCellsBar.Value = 0;
        tableLayoutPanel1.Visible = true;
        EndControlUpdate(tableLayoutPanel1);
    }

I'm not doing suspend layout beacouse it wasn't working and instead using what was recomended in this thread Parallel Generation of UI. I've also tried to set doubleBuffer to true like proposed in here .Net TableLayoutPanel – Clearing Controls is Very Slow and it isn't changing the behavior os the program...

Is there any other way to do this, like running the panel in a different thread or am I doing something wrong in the code itself?

Community
  • 1
  • 1
  • You're using a framework (Winforms controls) that is not suitable for the intensive graphics processing required to do what you need. I suggest looking at DirectX or OpenGL as frameworks that will be able to draw this 50x50 tile map. There are even some higher level frameworks like Unity that can make doing this a lot simpler. Good luck. – Jon Feb 12 '16 at 19:37
  • _I'm not doing suspend layout because it wasn't working_ Hm, it certainly should work and you should use it. How did it 'not work' ?? Otoh, 50x50 = 2500 and this is close to or even beyond a reasonable number of controls in a Winforms application. I would seriously consider switching to a more virtual design, like drawing the images onto one bitmap which you can then scroll; the mouseevents simply must take the relative positions into account.. How large are those images and those panels? And: No, parallelism won't help a seriously bogged down UI thread. But __maybe__ the drawing routine.. – TaW Feb 12 '16 at 19:37
  • Comment out the BackgroundImageLayout and BackgroundImage lines and test that. It's not clear to us what you are doing in BeginControlUpdate and EndControlUpdate. You have, quite simply, too many panels. I user can't really digest that many visuals. – LarsTech Feb 12 '16 at 19:39
  • _user can't really digest that many visuals_ that was my thought as well, but he __may__ be simply be drawing a large map from a lot of small tiles. One more reason to create one bitmap and scroll around it.. – TaW Feb 12 '16 at 19:43
  • @TaW ...or a google images down scroll... – LarsTech Feb 12 '16 at 19:44

0 Answers0