如何一次刷新面板



我在刷新面板时遇到了这个问题。我发现创建的控件越多,面板刷新速度就越慢。那么,如果有什么方法可以立即停止刷新面板,并最终进行一次刷新?

以下是我刷新面板的代码。我是一个对C#知识不太了解的新手,我真的希望得到帮助。

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;
}        

在更改开始时使用SuspendLayout()一次,在更改结束时使用ResumeLayout()

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();
}  

我也可以试试这个:

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();
} 

最新更新