2012-10-05 5 views
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을 제공합니까?TableLayoutPanel 높이 속성이 작동하지 않습니다.

+0

자동 크기 조정이 마음에 들지 않으면 AutoSize를 true로 설정하지 마십시오. –

답변

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 
    

또한 표를 양식에 추가 한 후에 높이 확인을 이동해야합니다. 그렇지 않으면 레이아웃 작업이 수행되지 않습니다.

관련 문제