【问题标题】:How do I align a TextBox and Label in TableLayoutPanel?如何在 TableLayoutPanel 中对齐 TextBox 和 Label?
【发布时间】:2011-12-21 11:48:56
【问题描述】:

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

 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

【问题讨论】:

  • @Bradley 没有理由删除 C# 标签。这不是一个特定于语言的问题,仅仅因为我的示例是在 VB.NET 中并不能使它成为一个 VB.NET 问题。我对所有语言都持开放态度,当时我正在使用 VB.NET 进行编程,所以这个示例更容易在 VB.NET 中组合在一起。用 C# 编程的人可能会想出解决问题的办法……
  • 您忘记添加 ColumnStyles。首先与设计师一起在示例表单上执行此操作。单击解决方案资源管理器窗口中的显示所有文件图标。打开表单旁边的节点并双击 Designer.vb 文件。查看设计器生成的代码。
  • Label 应该将 AutoSize 设置为 true

标签: c# .net vb.net winforms


【解决方案1】:

对我有用的是这个:

    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);

【讨论】:

    【解决方案2】:

    有几种方法可以解决它,但这样做意味着您可以在设计时获得更改(因此无需运行代码来查看它的外观),并且它会追溯更正您现有的所有布局,而无需需要逐个控制地修复每个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)利润

    之前:

    之后:

    【讨论】:

      【解决方案3】:

      标签和文本框使用 Anchor 属性在 TableLayoutPanel 内对齐。通常,Anchor 决定了控件在调整大小时将粘在父项的哪个边缘。但是对于 TableLayoutPanel,Anchor 属性决定了单元格内的 alignment。 TextAlign 对 TLP 中的标签对齐没有影响。

      来自 MSDN:

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

      注意:此行为不同于其他容器控件的行为。在其他容器控件中,设置Anchor属性时子控件不移动,设置Anchor属性时锚定控件与父容器边界的距离是固定的。

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

      【讨论】:

        【解决方案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
        

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

         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
        

        【讨论】:

          【解决方案5】:

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

          lblReportType.TextAlign = ContentAlignment.MiddleCenter 
          

          【讨论】:

          • 尝试过 TextAlign,不走运...有一个 hack 似乎做得很好但是很好,这完全是 hack...
          猜你喜欢
          • 1970-01-01
          • 2011-04-10
          • 1970-01-01
          • 1970-01-01
          • 2018-03-13
          • 2017-04-17
          • 2012-06-14
          • 2015-12-09
          • 2017-12-31
          相关资源
          最近更新 更多