简体   繁体   中英

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

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();
        }

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 :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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