开发者

Redraw and flicker issues

I have an Outlook style app. So basically I have a sidebar on the left and on the right I have a panel control (pnlMainBody) that hosts content.

The content is typically a user control that I add to the panel when user clicks appropriate button in the side bar. The way I add the user control to the panel is as follows:

// _pnlEmails is the User Control that I am adding to the panel
_pnlEmails = new pnlEmails();
_pnlEmails.Dock = DockStyle.Fill;
this.pnlMainBody.Controls.Add(_pnlEmails);

Some of the user controls that I add to the main panel are quite complex UI-wise. So when this.pnlMainBody.Controls.Add(_pnlEmails); fires, I see the control appear on the screen, then it resizes itself to fill the body of the panel control.

It's quite ugly actually, so I was wondering whether there is a way to not show the resizing until it's act开发者_JS百科ually done resizing?

I've tried setting the user control's .Visible to false. I've tried doing .SuspendLayout, all to no avail.

Is there a way to do this so the screen transitions are smooth?


First try to turn on double buffer painting in the parent form by setting:

this.DoubleBuffered = true;

Do that in your load handler or some such place to see if the flicker goes away.

If that doesn't work you can also try to set the DoubleBuffered property to true for the child controls if they are .NET Control-derived entities. Here's some code that I used recently to get controls that did not expose the double buffer property to paint nicely: (vb version. Do you need C#?)

Private Sub ForceDoubleBuffering(ByVal o As Object)
    Dim ctrl As Control
    Dim method As Reflection.MethodInfo
    Dim flags As Reflection.BindingFlags
    ctrl = TryCast(o, Control)
    flags = Reflection.BindingFlags.Instance Or Reflection.BindingFlags.NonPublic
    method = ctrl.GetType().GetMethod("SetStyle", flags)
    method.Invoke(ctrl, New Object() {ControlStyles.OptimizedDoubleBuffer, True})
End Sub


I figured out the trick to solving the issue. As I long as I set up the Dock.Fill property after adding the control to the main panel, there is no flicker.

this.pnlMainBody.Controls.Add(_pnlEmails);
_pnlEmails.Dock = DockStyle.Fill;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜