0
        TableLayoutPanel t = new TableLayoutPanel();  
        t.RowStyles.Add(new RowStyle(SizeType.AutoSize));
        t.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100));
        t.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;
        Label lbl = new Label();
        lbl.Margin = new System.Windows.Forms.Padding(20, 150, 20, 20);
        lbl.Text = "Hello";
        t.Controls.Add(lbl, 0, 0);
        this.Text = t.Size.Height.ToString();
        this.Controls.Add(t);

为什么 t.Size.Height 属性总是给我 100 ?

4

1 回答 1

4

这总是返回 100 的原因是您需要:

  • AutoSize = true
  • AutoSizeMode =AutoSizeMode.GrowAndShrink
  • t.RowCount >= 1
  • t.ColumnCount >= 1

        TableLayoutPanel t = new TableLayoutPanel();
        t.AutoSize = true;    //added
        t.AutoSizeMode =AutoSizeMode.GrowAndShrink;    //added
        t.RowCount = 1;  //added
        t.ColumnCount = 1;   //added
        t.RowStyles.Add(new RowStyle(SizeType.AutoSize));
        t.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100));
        t.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;
        Label lbl = new Label();
        lbl.Margin = new System.Windows.Forms.Padding(20, 150, 20, 20);
        lbl.Text = "Hello";
        t.Controls.Add(lbl, 0, 0);
        this.Controls.Add(t);
        this.Text = t.Size.Height.ToString();  //moved
    

在将表格添加到表单后,您还需要将高度检查移动到,否则不会发生任何布局操作。

于 2012-10-19T16:00:27.697 回答