vb.net 在 WinForms 中在运行时制作和更改 TableLayoutPanel
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24229774/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Make and change TableLayoutPanel at runtime in WinForms
提问by user3740891
I would like to know how to make and change a TableLayoutPanel at runtime in VB.NET, WinForms.
我想知道如何在运行时在 VB.NET、WinForms 中制作和更改 TableLayoutPanel。
I've had a look at the MSDN documentationbut I can't seem to understand how to vary the number of columns/rows (ie create new ones) nor how to change the values of any of the cells.
我看过MSDN 文档,但我似乎无法理解如何改变列/行的数量(即创建新的),也不明白如何更改任何单元格的值。
My aim is to have a 4x4 grid that contains 16 labels, whose text comes from a multi-dimensional (4x4) integer array.
我的目标是拥有一个包含 16 个标签的 4x4 网格,其文本来自一个多维 (4x4) 整数数组。
My current code is:
我目前的代码是:
Dim table As New TableLayoutPanel
table.ColumnCount = 4
table.RowCount = 4
table.RowStyles.Add(New RowStyle(SizeType.Absolute, 8.0F))
This is based off the MSDN examples, but I'm not sure how to use the RowStyles.Add(several arguments)method. Can anyone explain it?
这是基于 MSDN 示例,但我不确定如何使用RowStyles.Add(多个参数)方法。谁能解释一下?
回答by в?a???? в?н???
The following will create a TableLayoutPaneland all labels at run time. It is fully adjustable, in that you have have a 2 dimensional array of any size and it will display all values within that array. Using this code example should show you how to add rows and columns to a TableLayoutPaneldynamically at runtime.
以下将TableLayoutPanel在运行时创建一个和所有标签。它是完全可调的,因为您有一个任意大小的二维数组,它将显示该数组中的所有值。使用此代码示例应该会向您展示如何TableLayoutPanel在运行时动态地向 a 中添加行和列。
Public Class Form1
Friend WithEvents TableLayout As TableLayoutPanel
Private DataArray(,) As Integer = New Integer(3, 3) {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}}
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
Me.AutoSizeMode = Windows.Forms.AutoSizeMode.GrowAndShrink
Me.AutoSize = True
TableLayout = New TableLayoutPanel
With TableLayout
.Name = "tableLayout"
.Margin = New System.Windows.Forms.Padding(0, 0, 0, 0)
.ColumnCount = 0
.RowCount = 0
.Dock = DockStyle.Fill
.AutoSizeMode = Windows.Forms.AutoSizeMode.GrowAndShrink
.AutoSize = True
End With
Me.Controls.Add(TableLayout)
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
For x = LBound(DataArray, 1) To UBound(DataArray, 1)
Me.TableLayout.ColumnCount += 1
Me.TableLayout.ColumnStyles.Add(New ColumnStyle(SizeType.AutoSize))
For y = LBound(DataArray, 2) To UBound(DataArray, 2)
If y = LBound(DataArray, 2) Then
Me.TableLayout.RowCount += 1
Me.TableLayout.RowStyles.Add(New ColumnStyle(SizeType.AutoSize))
End If
Dim lbl = New Label
With lbl
.Name = "lbl" & x & y
.TextAlign = ContentAlignment.MiddleCenter
.Text = "Value: " & DataArray.GetValue(x, y)
.Dock = DockStyle.Fill
.AutoSize = True
End With
Me.TableLayout.Controls.Add(lbl, y, x)
Next
Next
End Sub
End Class
回答by Lev Z
I suggest to you to create a TableLayoutPanel using Designer and after that to check the auto generated code in Designer.cs (Designer.vb in your case) class. Here small example in C#:
我建议您使用 Designer 创建一个 TableLayoutPanel,然后在 Designer.cs(在您的情况下为 Designer.vb)类中检查自动生成的代码。这是 C# 中的小例子:
// tableLayoutPanel1
//
this.tableLayoutPanel1.ColumnCount = 4;
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 20F));
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 20F));
this.tableLayoutPanel1.Location = new System.Drawing.Point(252, 75);
this.tableLayoutPanel1.Name = "tableLayoutPanel1";
this.tableLayoutPanel1.RowCount = 4;
this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 20F));
this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 20F));
this.tableLayoutPanel1.Size = new System.Drawing.Size(200, 100);
this.tableLayoutPanel1.TabIndex = 4;
To add Controls to your TableLayout use it Controls property. For example:
要将控件添加到您的 TableLayout,请使用它的 Controls 属性。例如:
private void button2_Click(object sender, EventArgs e)
{
Label label = new Label();
label.Text = "Hello!";
tableLayoutPanel1.Controls.Add(label, 0, 0);
}
enter code here

