-1

I've the question while doing panel refresh. I found that the more controls created, the slower the panel refresh. So, if there any way to stop refresh the panel immediately, and do refresh in once at last?

Here's the codes how I refresh my panel. I'm a newbie with less knowledge in C#, and really hope for help.

private void DoPanelReresh()
{
            int height = pl_Main.VerticalScroll.Value;

            pl_Main.Controls.Clear();
            if (PCandPLC.Equals(EPcPlc.PLC_to_PC))
            {
                if (stations_PLC.Count - 1 >= 0)
                {
                    for (int x = stations_PLC.Count - 1; x >= 0; x--)
                    {
                        pl_Main.Controls.Add(stations_PLC[x]);
                    }
                }
            }
            else
            {
                if (stations_PC.Count - 1 >= 0)
                {
                    for (int x = stations_PC.Count - 1; x >= 0; x--)
                    {
                        pl_Main.Controls.Add(stations_PC[x]);
                    }
                }
            }

            pl_Main.VerticalScroll.Value = height;
}        
  • 2
    Are you looking for [`SuspendLayout` and `ResumeLayout`](https://stackoverflow.com/questions/126876/how-do-i-disable-updating-a-form-in-windows-forms)? – John Wu Apr 14 '22 at 04:22
  • I've try it, but it still be stuck if I create and refresh too many controls in the panel. – Chang Jin-Hau Apr 14 '22 at 05:00
  • Welcome to Stack Overflow! Are you using Windows Forms? If so, please add the [winforms] tag to the question. – sbridewell Apr 14 '22 at 16:29

1 Answers1

0

Use SuspendLayout() once at the beginning of the changes and ResumeLayout() once at the end.

private void DoPanelReresh()
{
    int height = pl_Main.VerticalScroll.Value;

    pl_Main.SuspendLayout();
    pl_Main.Controls.Clear();
    if (PCandPLC.Equals(EPcPlc.PLC_to_PC))
    {
        if (stations_PLC.Count - 1 >= 0)
        {
            for (int x = stations_PLC.Count - 1; x >= 0; x--)
            {
                    pl_Main.Controls.Add(stations_PLC[x]);
            }
        }
    }
    else
    {
        if (stations_PC.Count - 1 >= 0)
        {
            for (int x = stations_PC.Count - 1; x >= 0; x--)
            {
                pl_Main.Controls.Add(stations_PC[x]);
            }
        }
    }
    pl_Main.VerticalScroll.Value = height;
    pl_Main.ResumeLayout();
}  

I might also try this:

private void DoPanelReresh()
{
    int height = pl_Main.VerticalScroll.Value;

    pl_Main.SuspendLayout();
    pl_Main.Controls.Clear();

    Control[] controlsToAdd = PCandPLC.Equals(EPcPlc.PLC_to_PC)?stations_PLC:stations_PC;
    if (controlsToAdd.Length > 0)
        pl_Main.Controls.AddRange(controlsToAdd);

    pl_Main.VerticalScroll.Value = height;
    pl_Main.ResumeLayout();
} 
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • Thanks for your answer. I've try the other way to refresh the panel. Instead of canceling controls and adding them, I've tried to change index by the SetChildIndex(Control, int) method, and it works. – Chang Jin-Hau Apr 15 '22 at 03:46