'How could I refresh the panel in once?
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;
}
Solution 1:[1]
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();
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Joel Coehoorn |
