如何在TableLayoutPanel中对齐TextBox和Label?

Den*_*nis 10 .net c# vb.net winforms

我已经阅读了一些关于此的文章,但似乎没有任何帮助.在以下情况下,如何对齐标签和文本框:

 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
Run Code Online (Sandbox Code Playgroud)

小智 15

标签和文本框使用Anchor属性在TableLayoutPanel中对齐.通常,Anchor确定控件在调整大小时将坚持使用的父级边缘.但是使用TableLayoutPanel,Anchor属性确定单元格内的对齐方式.TextAlign对TLP中的标签对齐没有影响.

来自MSDN:

将Button控件的Anchor属性的值更改为Left.Button控件移动以与单元格的左边框对齐.

注意:此行为与其他容器控件的行为不同.在其他容器控件中,当设置Anchor属性时,子控件不会移动,并且锚定控件和父容器边界之间的距离在设置Anchor属性时是固定的.

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


Dam*_*ith 7

您可以使用AnchorDock属性在TableLayoutPanel中对齐和拉伸控件.

lblReportType.TextAlign = ContentAlignment.MiddleCenter 
Run Code Online (Sandbox Code Playgroud)


Den*_*nis 2

将上面的内容替换为:

  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
Run Code Online (Sandbox Code Playgroud)

这完全是一个黑客,但这似乎有效......也许有人会想出更好的东西:

 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
Run Code Online (Sandbox Code Playgroud)