简体   繁体   English

列样式在TableLayoutPanel上不起作用

[英]Column Styles not working on TableLayoutPanel

I have a TableLayoutPanel that has a dynamic amount of columns and rows determined by the user. 我有一个TableLayoutPanel,它具有由用户确定的动态数量的列和行。 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. 您绝对必须使用FlowLayoutPanel控件,并且可以在其中添加任意数量的控件,可以告诉哪个方向将填充该控件(是否包装内容)以及许多其他控件。

And the most important - It Will Not Flickering like TableLayoutPanel does :) 最重要的是-它不会像TableLayoutPanel那样闪烁:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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