首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在FlowLayoutpanel中动态添加控件时摆脱闪烁,由定时器触发

在FlowLayoutpanel中动态添加控件时摆脱闪烁,由定时器触发
EN

Stack Overflow用户
提问于 2014-11-19 16:30:23
回答 2查看 1.6K关注 0票数 1

我正在尝试制作一个自定义控件,以显示来自SQL源的一些数据。数据应该在由Timer控件触发的方法中更改。

比方说,如果我将所有需要的数据附加到字符串缓冲区中,然后在long label中显示这是有效的。每次重新绘制标签时,都不会闪烁。

但是,如果我想做更花哨的外观,我发现唯一的方法是将控件放在FLowLayoutPanel中并构建整个结构。好吧,它可以工作,但每次绘制控件时,我都需要处理它们。如果不是,则将控件添加到无穷大,并导致缓冲区重载。

但如果我对它们进行dispose()或clear()操作,那么每次重新绘制控件时都会闪烁。

有没有办法摆脱这种眨眼?或者用其他方式来完成我的工作?

我的代码:

代码语言:javascript
运行
复制
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();
        }
    }
EN

回答 2

Stack Overflow用户

发布于 2014-11-19 16:37:09

创建自定义flowLayoutPanel类并将其添加到其构造函数中

代码语言:javascript
运行
复制
public class CustomFLP: FlowLayoutPanel 
{
    public CustomFLP() : base()
    {
        this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
        this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
        this.UpdateStyles();
    }
}

然后,除了使用CustomFLP而不是FlowLayoutPanel创建流布局面板之外,其他所有操作都与现在相同。

这给你的控件增加了一个双缓冲区,可能无法处理很多更新。

票数 1
EN

Stack Overflow用户

发布于 2017-03-23 04:24:45

我发现将Vajura的方法与SuspendLayout() / ResumeLayout()封装在一起可以完全消除闪烁。

代码语言:javascript
运行
复制
// Declaration
CustomFLP m_doubleBufferFlowLayoutPanel;

void UpdatePanel()
{
    m_doubleBufferFlowLayoutPanel.SuspendLayout();

    // update the flow layout panel here
    ...

    m_doubleBufferFlowLayoutPanel.ResumeLayout();
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27012126

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档