【问题标题】:Column Styles not working on TableLayoutPanel列样式在 TableLayoutPanel 上不起作用
【发布时间】:2014-03-25 14:00:18
【问题描述】:

我有一个 TableLayoutPanel,它具有由用户确定的动态列数和行数。我希望里面的按钮是方形的并且大小相同,但是每当我使用循环设置列/行样式时,它们永远不会变成我想要的大小。

如何获取列/行样式以设置容器元素的适当宽度和高度?

这是处理设置表格宽度大小的代码的循环方法(我对行使用类似的方法)

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

            style.Width = 60;
        }
    }

【问题讨论】:

    标签: c#


    【解决方案1】:

    你可以这样做......

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

    【讨论】:

    • 非常感谢,这完全符合我的需要!我猜问题是我需要清除样式并添加它们,将它们设置为新样式时搞砸了。
    【解决方案2】:

    很抱歉告诉您,但您没有使用正确的控件。 您肯定必须使用 FlowLayoutPanel 控件,并且您可以在其中添加任意数量的控件,您可以告诉哪个方向将填充控件,是否包含内容,等等。

    最重要的是 - 它不会像 TableLayoutPanel 那样闪烁 :)

    【讨论】:

      猜你喜欢
      • 2018-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-29
      • 1970-01-01
      • 2021-11-20
      • 1970-01-01
      • 2014-08-05
      相关资源
      最近更新 更多