繁体   English   中英

如何在TableLayoutPanel中对齐TextBox和Label?

[英]How do I align a TextBox and Label in TableLayoutPanel?

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

 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

标签和文本框使用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

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

lblReportType.TextAlign = ContentAlignment.MiddleCenter 

有几种方法可以实现它,但这样做意味着您可以在设计时获得更改(因此无需运行代码来查看它的外观),并且它将追溯性地纠正所有现有布局,而无需修复每个Label上的TextAlignmentAnchor属性,逐个控制。

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(使用new TableLayoutPanelEx(

3)

4)利润

之前: 在此输入图像描述

后: 在此输入图像描述

用以下内容代替上述内容:

  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

对我有用的是:

    Label lblAmountInWords = new Label();

    lblAmountInWords.Text = "FOUR THOUSAND THREE HUNDRED AND TWENTY ONLY";
    lblAmountInWords.Font = new Font("Arial", 9, FontStyle.Bold);
    lblAmountInWords.AutoSize = false;
    lblAmountInWords.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top | AnchorStyles.Bottom;
    lblAmountInWords.TextAlign = ContentAlignment.MiddleCenter;

    tableLayoutPanelAmountInWords.Controls.Add(lblAmountInWords, 0, 0);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM