代码之家  ›  专栏  ›  技术社区  ›  Kevin

推迟表格更新

  •  1
  • Kevin  · 技术社区  · 16 年前

    有没有一种方法可以在操作控件时推迟表单的更新/重新绘制?

    我正在使用 TableLayoutPanel 控件,我希望在运行时将不同的用户控件动态删除/添加到表的不同单元格中。

    tablelayout.SuspendLayout()
    

    SuspendLayout()

    4 回复  |  直到 12 年前
        1
  •  1
  •   Marc Gravell    16 年前

    这取决于控制;寻找 BeginUpdate / EndUpdate BeginEdit / EndEdit ,等等(并在 try / finally 块)。然而,请注意,只有 一些 控件有这些方法,并且它们没有绑定到一个方便的界面,所以它有点手动。例如:

        listView.BeginUpdate();
        try
        {
            for (int i = 0; i < 100; i++)
            {
                listView.Items.Add("Item " + i);
            }
        }
        finally
        {
            listView.EndUpdate();
        }
    

    请注意,数据绑定控件可能有其他机制用于禁用与数据相关的更改;例如 BindingList<T> RaiseListChangedEvents 这将在编辑数据时关闭更改通知。您还可以查看“虚拟模式”之类的内容。

        2
  •  1
  •   Dan Tao    16 年前

    尝试:

    try {
        SuspendLayout();
    
        // manipulate your controls
    
    } finally {
        ResumeLayout();
    }
    
        3
  •  1
  •   Kevin    16 年前

    我创建了一个继承表的类,然后启用了双缓冲,从而完全消除了闪烁。

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Windows.Forms;
    
    namespace myNameSpace.Forms.UserControls
    {
        public class TableLayoutPanelNoFlicker : TableLayoutPanel
        {
            public TableLayoutPanelNoFlicker()
            {
                this.DoubleBuffered = true;
            }
        }
    }
    
        4
  •  0
  •   matt    16 年前

    您可以尝试SuspendLayout/ResumeLayout—表单设计器生成的代码就是这样做的—查看表单代码背后的InitializeComponents方法,了解我的意思。

    推荐文章