user3334134
user3334134

Reputation: 23

Column Styles not working on TableLayoutPanel

I have a TableLayoutPanel that has a dynamic amount of columns and rows determined by the user. I want the buttons inside to be square and the same size, but whenever I use a loop to set the column/rows styles, they never turn out to be the size I want them to be.

How can I get the column/row styles to set the appropriate widths and height os the container elements?

Here is the loop method of the code that handles setting the width size of the table (I use a similar method for rows)

 void FormatTableWidth(ref TableLayoutPanel container)
    {
        TableLayoutColumnStyleCollection columnStyles = container.ColumnStyles;
        foreach (ColumnStyle style in columnStyles)
        {
            style.SizeType = SizeType.Absolute;

            style.Width = 60;
        }
    }

Upvotes: 2

Views: 3153

Answers (2)

Vali Maties
Vali Maties

Reputation: 399

Sorry to tell you, but you don't use the right control. You definitely must use FlowLayoutPanel control, and you can add as many controls you want in it, you can tell which direction will fill the control, if it wrap content or not, and many others.

And the most important - It Will Not Flickering like TableLayoutPanel does :)

Upvotes: 0

Milan Raval
Milan Raval

Reputation: 1880

You can do it like....

public void AddButtontControls()
        {
            tblPanel.SuspendLayout();
            tblPanel.Controls.Clear();           
            tblPanel.GrowStyle = TableLayoutPanelGrowStyle.FixedSize;//.AddColumns;
            tblPanel.ColumnStyles.Clear();
            for (int i = 0; i < tblPanel.ColumnCount; i++)
            {
                ColumnStyle cs = new ColumnStyle(SizeType.Percent, 100 / tblPanel.ColumnCount);
                tblPanel.ColumnStyles.Add(cs);

                //Add Button
                Button a = new Button();
                a.Text = "Button " + i + 1;                
                tblPanel.Controls.Add(a);
            }
            tblPanel.ResumeLayout();
        }

Upvotes: 4

Related Questions