我已经阅读了一些关于此的文章,但似乎没有任何帮助。在以下情况中,如何对齐标签和文本框:
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
答案 0 :(得分:13)
使用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
答案 1 :(得分:7)
您可以使用Anchor和Dock属性在TableLayoutPanel中对齐和拉伸控件。
lblReportType.TextAlign = ContentAlignment.MiddleCenter
答案 2 :(得分: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
答案 3 :(得分:0)
有几种方法可以实现它,但这样做意味着你可以在设计时获得更改(因此无需运行代码来查看它的外观),并且它将追溯性地纠正所有现有布局而无需需要在逐个控制的基础上修复每个TextAlignment
上的Anchor
和Label
属性。
<强> 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)利润
答案 4 :(得分:0)
对我有用的是:
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);