我正在尝试制作一个自定义控件,以显示来自SQL源的一些数据。数据应该在由Timer控件触发的方法中更改。
比方说,如果我将所有需要的数据附加到字符串缓冲区中,然后在long label中显示这是有效的。每次重新绘制标签时,都不会闪烁。
但是,如果我想做更花哨的外观,我发现唯一的方法是将控件放在FLowLayoutPanel中并构建整个结构。好吧,它可以工作,但每次绘制控件时,我都需要处理它们。如果不是,则将控件添加到无穷大,并导致缓冲区重载。
但如果我对它们进行dispose()或clear()操作,那么每次重新绘制控件时都会闪烁。
有没有办法摆脱这种眨眼?或者用其他方式来完成我的工作?
我的代码:
void timer_Tick(object sender, EventArgs e)
{
try
{
GetMapData();
}
catch (Exception ex)
{
}
}
private void GetMapData()
{
DataSet ds = MapStatistics.GetMapData(top, bottom, left, right, idUnit, idCustomerTmp);
DataTable activDriversForZones = ds.Tables["ActivDriversForZone"];
DataTable zones zones = ds.Tables["Zones"];
if (zones != null || zones.Rows.Count > 0)
{
ClearflpPanel(flpTest2);
zonesDriverCount = 0;
foreach (DataRow rowZones in zones.Rows)
{
string idZone = Convert.ToString(rowZones["id_zone"]);
string title = Convert.ToString(rowZones["title"]);
StringBuilder zoneDriver = new StringBuilder();
Label labelRowZone = new Label();
labelRowZone.Width = 400;
labelRowZone.AutoSize = true;
labelRowZone.Font = new System.Drawing.Font("Arial", 10, FontStyle.Bold);
labelRowZone.Text = (title + ": ");
flpTest2.Controls.Add(labelRowZone);
Label labelRowDriver = new Label();
foreach (DataRow rowDriver in activDriversForZones.Rows)
{
string idUnit = Convert.ToString(rowDriver["id_unit"]);
string imsi = Convert.ToString(rowDriver["imsi"]);
string zoneIn = Convert.ToString(rowDriver["zone_in"]);
if (idZone == zoneIn)
{
zonesDriverCount++;
zoneDriver.Append(imsi + "/" + idUnit + ", ");
labelRowDriver.Text = (zoneDriver.ToString());
}
flpTest2.Controls.Add(labelRowDriver);
}
}
}
}
public void ClearflpPanel(FlowLayoutPanel flp)
{
zonesDriverCount = 0;
List<Control> listControls = flp.Controls.Cast<Control>().ToList();
foreach (Control control in listControls)
{
flpTest2.Controls.Clear();
//flpTest2.Dispose();
}
}
发布于 2014-11-19 16:37:09
创建自定义flowLayoutPanel类并将其添加到其构造函数中
public class CustomFLP: FlowLayoutPanel
{
public CustomFLP() : base()
{
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
this.UpdateStyles();
}
}
然后,除了使用CustomFLP而不是FlowLayoutPanel创建流布局面板之外,其他所有操作都与现在相同。
这给你的控件增加了一个双缓冲区,可能无法处理很多更新。
发布于 2017-03-23 04:24:45
我发现将Vajura的方法与SuspendLayout() / ResumeLayout()封装在一起可以完全消除闪烁。
// Declaration
CustomFLP m_doubleBufferFlowLayoutPanel;
void UpdatePanel()
{
m_doubleBufferFlowLayoutPanel.SuspendLayout();
// update the flow layout panel here
...
m_doubleBufferFlowLayoutPanel.ResumeLayout();
}
https://stackoverflow.com/questions/27012126
复制相似问题