在WinForms中运行时创建和更改TableLayoutPanel

时间:2014-06-15 12:52:18

标签: vb.net winforms tablelayoutpanel

我想知道如何在VB.NET,WinForms中在运行时创建和更改TableLayoutPanel。

我已经看过MSDN documentation,但我似乎无法理解如何改变列/行的数量(即创建新的列),也不知道如何更改列值/行的值任何细胞。

我的目标是拥有一个包含16个标签的4x4网格,其文本来自多维(4x4)整数数组。

我目前的代码是:

Dim table As New TableLayoutPanel
table.ColumnCount = 4
table.RowCount = 4
table.RowStyles.Add(New RowStyle(SizeType.Absolute, 8.0F))

这是基于MSDN示例,但我不确定如何使用RowStyles.Add(多个参数)方法。谁能解释一下呢?

2 个答案:

答案 0 :(得分:2)

以下内容将在运行时创建TableLayoutPanel和所有标签。它是完全可调的,因为你有一个任何大小的二维数组,它将显示该数组中的所有值。使用此代码示例应该向您展示如何在运行时动态地向TableLayoutPanel添加行和列。

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

答案 1 :(得分:1)

我建议您使用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;

要向TableLayout添加控件,请使用它控制属性。例如:

  private void button2_Click(object sender, EventArgs e)
    {

        Label label = new Label();
        label.Text = "Hello!";
        tableLayoutPanel1.Controls.Add(label, 0, 0);

    }

enter code here
相关问题