2011-11-03 418 views
9

我已閱讀了一些關於此的文章,但似乎沒有幫助。如何排列在以下情況下,標籤和文本框:如何對齊TableLayoutPanel中的TextBox和Label?

Using frm As New frmWithTableLayout 
    frm.TableLayoutPanel1.ColumnCount = 2 
    frm.TableLayoutPanel1.RowCount = 3 

    'create report Type' 
    Dim lblReportType As New Label 
    lblReportType.Text = "Report Type" 
    lblReportType.Dock = DockStyle.Right 
    Dim reportType As New System.Windows.Forms.TextBox() 
    reportType.Text = "Income" 
    frm.TableLayoutPanel1.Controls.Add(lblReportType, 0, 0) 
    frm.TableLayoutPanel1.Controls.Add(reportType, 1, 0) 
End Using 
+1

@Bradley沒有理由刪除C#標籤。這不是一個特定於語言的問題,僅僅是因爲我的例子在VB.NET中並沒有使它成爲一個VB.NET問題。我對所有語言都開放,就在我使用VB.NET進行編程的時候,所以這個例子在VB.NET中更容易放在一起。用C#編程的人可能會想出解決問題的方法...... – Denis

+0

你忘了添加ColumnStyles。首先由設計人員在樣本表單上進行此操作。單擊解決方案資源管理器窗口中的顯示所有文件圖標。打開表單旁邊的節點,然後雙擊Designer.vb文件。查看設計者生成的代碼。 –

+0

@Denis我只想讓它成爲一個.net標記,因爲你遺漏了F#和其他基於.net的語言。 –

回答

1

更換上面:

Using frm As New frmWithTableLayout 
      frm.SetupTableLayout(2, 3) 

      'create report Type' 
      Dim lblReportType As New Label 
      lblReportType.Text = "Report Type" 
      frm.LayoutControl(lblReportType, 0, 0) 
      Dim tbReportType As New System.Windows.Forms.TextBox() 
      tbReportType.Text = "Income" 
      frm.LayoutControl(tbReportType, 1, 0) 

      frm.ShowDialog() 
    End Using 

這是一個總的黑客攻擊,但這似乎工作...也許有人會拿出更好的東西:

Public Sub LayoutControl(ByVal c As Control, ByVal column As Integer, ByVal row As Integer) 
     If TypeOf c Is Label Then 
      Dim clabel As Label = DirectCast(c, Label) 
      clabel.TextAlign = ContentAlignment.TopCenter 
      clabel.Dock = DockStyle.Right 
      clabel.Margin = New Padding(clabel.Margin.Left, clabel.Margin.Top + 5, clabel.Margin.Right, clabel.Margin.Bottom) 

     ElseIf TypeOf c Is System.Windows.Forms.TextBox Then 
      Dim ctbox As System.Windows.Forms.TextBox = DirectCast(c, System.Windows.Forms.TextBox) 
      ctbox.Margin = New Padding(0, 3, 0, 3) 
      ctbox.TextAlign = HorizontalAlignment.Center 
     End If 

     TableLayoutPanel1.Controls.Add(c, column, row) 
    End Sub 

    Public Sub SetupTableLayout(ByVal numOfColumns As Integer, ByVal numOfRows As Integer) 
     TableLayoutPanel1.ColumnCount = numOfColumns 
     TableLayoutPanel1.RowCount = numOfRows 
     While TableLayoutPanel1.RowStyles.Count < TableLayoutPanel1.RowCount 
      TableLayoutPanel1.RowStyles.Add(New RowStyle()) 
     End While 

     For Each row As RowStyle In TableLayoutPanel1.RowStyles 
      With row 
       .SizeType = SizeType.Percent 
       .Height = 100/TableLayoutPanel1.RowCount 
      End With 
     Next row 
    End Sub 
7

您可以用AnchorDock性能調整和拉伸控制在一個TableLayoutPanel。

lblReportType.TextAlign = ContentAlignment.MiddleCenter 
+0

我試過了,(見上文)。沒有運氣 – Denis

+1

@Denis嘗試TextAlign也 – Damith

+0

嘗試TextAlign,沒有運氣......有一個黑客到位,似乎做得很好,但很好,這是一個徹頭徹尾的破解... – Denis

11

標籤和文本框使用Anchor屬性在TableLayoutPanel內對齊。通常,Anchor確定控件在調整大小時將與哪個邊緣保持一致。但是對於TableLayoutPanel,Anchor屬性確定單元內的對齊。 TextAlign對TLP中的標籤對齊沒有影響。

從MSDN:

更改Button控件的Anchor屬性爲左值。按鈕控件移動以對齊單元格的左邊框。

注意:此行爲不同於其他容器控件的行爲。在其他容器控件中,當設置Anchor屬性時,子控件不會移動,並且在設置Anchor屬性時,錨定控件與父容器的邊界之間的距離是固定的。

https://msdn.microsoft.com/en-us/library/ms171691%28v=vs.90%29.aspx

0

有幾種方法可以解決,但做這種方式意味着你在設計時的變化(因此沒有必要運行的代碼,看看它如何看),它會追溯更正所有現有佈局,而無需在每個控件的基礎上修復每個Label上的TextAlignmentAnchor屬性。與new TableLayoutPanelEx(

1)

public class TableLayoutPanelEx : TableLayoutPanel 
{ 
    public TableLayoutPanelEx() 
    { 
     ControlAdded += OnControlAdded;    
    } 

    private void OnControlAdded(object sender, ControlEventArgs args) 
    { 
     var control = args.Control as Label; 
     if (control != null) { 
      control.TextAlign = ContentAlignment.MiddleLeft;     
      control.Anchor = (AnchorStyles.Bottom | AnchorStyles.Top | AnchorStyles.Left); 
     }    
    } 
} 

2)項目範圍內搜索/替換new TableLayoutPanel(

3)

4)利潤

前:enter image description here

後:enter image description here