views:

203

answers:

2

This is really simple.

I have a TableLayoutPanel that is populated with controls (just Labels, Buttons, and some Panels with buttons) based on a database query. When the data needs to be refreshed, I use TableLayoutPanel.Controls.Clear(). Unfortunately, this is a very slow operation. I would expect it to be faster than the code populating the table, but it is at least 3 or 4 times slower.

I definitively proved that the slowness is when executing Controls.Clear() by executing this as the single thing done to the TableLayoutPanel after a message box is displayed (then the procedure returns). The controls visibly disappear from the bottom up. When the recordset is used to repopulate the TableLayoutPanel, the speed of the controls appearing from top to bottom is almost faster than I can see.

I'm already doing TableLayoutPanel.SuspendLayout() and ResumeLayout().

Using this.DoubleBuffered = true on the form doesn't appear to do anything.

I could just Dispose the entire control and recreate it through code, but this is a big pain and makes having a nice form designer GUI pointless. I would have to dig into every property I've set on the control and create a line of code for it (though I guess I could get this out of the designer code itself, it still feels wrong).

Any ideas on how to do the job faster? I'm even open to using other methods besides a TableLayoutPanel... I just need the freedom to put multiple buttons per cell or barring that to be able to span columns in the table header.

Can C# at least freeze the whole form while it redraws and then paint all at once?

A: 

If i'm going to built up some dynamic gui i'm always going to do so in code. But at a starting point i just start with the designer on a dummy form and style each control the way i (or better the customer) like(s). Afterwards i take a look into the Designer.cs file and copy the necessary property settings out of it into some factory function like

private TextBox CreateTextBox(string name, /* maybe other parameters */)
{
    var textBox = new TextBox();
    textBox.Name = name;
    //Other settings from given parameters...

    //Further settings which are all the same for these kind of control
    textBox.KeyDown += (sender, e) => {};

    return textBox;
}

So i make sure that every control feels and looks the same on my GUI. This will be done on each level within my surface (starting with the small controls like TextBox and goes up to the containers like GroupBox or TableLayoutPanel.

In some cases this leads to a point where a factory function calls several other factory functions. If this is becoming true it's time to think about encapsulating these controls into a single UserControl, but as always it depends if this is needed or not.

From my side i can only encourage you to move your code out of the designer into a self-written function. At the beginning it is (as always) more work, but afterwards it is easier to make even bigger changes to the layout.

Oliver
A: 

I've run into issues with slowness using TableLayoutPanels as well. Rather than setting the DoubleBuffered property on the form, the best solution I have found is to create a new class that inherits from TableLayoutPanel, and in that class' constructor, enable double-buffering:

public class DoubleBufferedTableLayoutPanel : TableLayoutPanel
{
    public DoubleBufferedTableLayoutPanel()
    {
        DoubleBuffered = true;
    }
}

Then, use the DoubleBufferedTableLayoutPanel wherever you would normally use a TableLayoutPanel.

Chris Ryan
Could you speculate on why you think that is? The TableLayoutPanel has a DoubleBuffered property, itself... why not just set it at design time (I'm assuming this doesn't work though)? So what's the deal?
Emtucifor
In the version of .NET I am using (2008, SP1), neither the Properties toolbar nor Intellisense lists the DoubleBuffered property for TableLayoutPanels. If you have an instance of a TableLayoutPanel, say myTableLayoutPanel and attempt to set myTableLayoutPanel.DoubleBuffered = true, the compiler will display a "Cannot access protected member 'System.Windows.Forms.Control.DoubleBuffered'" error. If your version of .NET does indeed offer the DoubleBuffered property on the ordinary TableLayoutPanel, then I'd imagine setting it there would work fine.
Chris Ryan