views:

187

answers:

2

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 actually 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?

+1  A: 

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
Paul Sasik
Double buffering has always been on in the properties.
AngryHacker
Everything (including downlevel controls are set to double buffer. The entire resizing operation starts after the .Visible is set to true. Which makes it look ugly.
AngryHacker
Are you using 3rd party controls?
Paul Sasik
@Paul Sasik. Yes, mostly DevExpress. Are you saying that I should loop through all the 3rd party controls and force them to double buffer?
AngryHacker
Might be worth a shot. It's only a few lines of code.
Paul Sasik
A: 

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;
AngryHacker