简体   繁体   English

如何在使用tabControl.TabPages [0] .Controls.Add()时停止表单闪烁?

[英]how can i stop form flickering when using tabControl.TabPages[0].Controls.Add()?

am using below code to add a Form to a tabControls tabPage 我使用下面的代码将一个表单添加到tabControls tabPage

private void btnStudents_Click(object sender, EventArgs e)
    {
       foreach (Form c in tabStudents.TabPages[0].Controls)
        {
            tabStudents.TabPages[0].Controls.Remove(c);
            c.Dispose();
        }

        //load form
        StudentsMasterForm f = new StudentsMasterForm
        {
            TopLevel = false,
            FormBorderStyle = FormBorderStyle.None,
            Dock = DockStyle.Fill
        };

        tabStudents.TabPages[0].Controls.Add(f);
        f.Show();
    }

the problem however is, there is too much form flickering when the button is clicked (ie when the form is loaded). 然而问题是,单击按钮时(即加载表单时)表单闪烁太多。 I have tried using tabCustomersAndOrders.TabPages[0].SuspendLayout(); 我尝试过使用tabCustomersAndOrders.TabPages[0].SuspendLayout(); and tabCustomersAndOrders.TabPages[0].ResumeLayout(); tabCustomersAndOrders.TabPages[0].ResumeLayout(); ` but the flickering isn't going away. `但闪烁不会消失。

I want to transition from one form to another to be as smooth as possible. 我希望从一种形式过渡到另一种形式,以尽可能顺利。

Enabling double-buffering on the TabControl might help. 在TabControl上启用双缓冲可能会有所帮助。 With double-buffering, the control graphics all get rendered into memory, and then displayed only when all control rendering is complete. 通过双缓冲,控制图形全部渲染到内存中,然后仅在所有控件渲染完成时显示。

This will mean a visible delay until completion, but should remove the flickering effect of multiple controls rendering. 这将意味着直到完成时的可见延迟,但应该消除多个控件渲染的闪烁效果。

The following will enable double-buffering: 以下将启用双缓冲:

myTabControl.SetStyle(ControlStyles.DoubleBuffer | 
      ControlStyles.UserPaint | 
      ControlStyles.AllPaintingInWmPaint,
      true);

The other alternative (the one I'd recommend) is to look at the problem from a different angle. 另一种选择(我推荐的那种)是从不同的角度来看问题。 Is there any way you can alter the UI design so that this kind of form population is pre-cached, or occurs before rendering to screen? 有没有什么方法可以改变UI设计,以便预先缓存这种表单填充,或者在渲染到屏幕之前发生?

For the purpuse of marking this question as answered, here i post a link to another stackoverflow question that has a solution to my question. 对于将此问题标记为已回答的问题,我在这里发布了另一个stackoverflow问题的链接,该问题解决了我的问题。

Here is the link how to fix the flickering in user controls 以下是如何修复用户控件中的闪烁的链接

Just paste this in your main GUI: 只需将其粘贴到主GUI中:

protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ExStyle |= 0x02000000;  // Turn on WS_EX_COMPOSITED
                return cp;
            }
        }

this should solve your problem. 这应该可以解决你的问题。 At least from many things that I searched over the web, this one helped. 至少从我在网上搜索过的很多东西来看,这个有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM