更換上面:
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
@Bradley沒有理由刪除C#標籤。這不是一個特定於語言的問題,僅僅是因爲我的例子在VB.NET中並沒有使它成爲一個VB.NET問題。我對所有語言都開放,就在我使用VB.NET進行編程的時候,所以這個例子在VB.NET中更容易放在一起。用C#編程的人可能會想出解決問題的方法...... – Denis
你忘了添加ColumnStyles。首先由設計人員在樣本表單上進行此操作。單擊解決方案資源管理器窗口中的顯示所有文件圖標。打開表單旁邊的節點,然後雙擊Designer.vb文件。查看設計者生成的代碼。 –
@Denis我只想讓它成爲一個.net標記,因爲你遺漏了F#和其他基於.net的語言。 –