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 に答える