如何在运行时动态创建多个控件

How to dynamicallty create multiple controls at runtime

我正在尝试在运行时向用户窗体添加多个标签

棋类游戏的玩家名称;在比赛开始之前,玩家的数量是未知的。我已经设法自己弄清楚如何使用动态数组函数来创建玩家列表。我使用 For.....Next 循环来添加玩家名称。我以为我可以这样做来将标签添加到表单中,但它只添加了一个。根据声明新控件类型的位置,它要么只添加第一个玩家,要么添加最后一个玩家

此代码仅在组框内生成一个标签,最后一个玩家

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim Players_Num As Integer = InputBox("Enter the number of players")
        Dim Players(Players_Num) As String
        Dim newText As New Label

        For i = 0 To Players_Num - 1
            Players(i) = InputBox("Enter player name")
        Next

        'This piece of code was jsut for me to test that I was successfully using a For...Loop
        'to add the players names, and will be deleted later on
        For x = 0 To Players_Num - 1
            MessageBox.Show(Players(x))
        Next

        For z = 0 To Players_Num - 1
            newText.Name = "txt" & Players(z)
            newText.Text = Players(z)
            newText.Size = New Size(170, 20)
            newText.Location = New Point(12 + 5, 12 + 5)
            GroupBox1.Controls.Add(newText)
        Next
    End Sub
End Class

这个只放第一位玩家

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim Players_Num As Integer = InputBox("Enter the number of players")
        Dim Players(Players_Num) As String

        For i = 0 To Players_Num - 1
            Players(i) = InputBox("Enter player name")
        Next

        'This piece of code was jsut for me to test that I was successfully using a For...Loop
        'to add the players names, and will be deleted later on
        For x = 0 To Players_Num - 1
            MessageBox.Show(Players(x))
        Next

        For z = 0 To Players_Num - 1
            Dim newText As New Label
            newText.Name = "txt" & Players(z)
            newText.Text = Players(z)
            newText.Size = New Size(170, 20)
            newText.Location = New Point(12 + 5, 12 + 5)
            GroupBox1.Controls.Add(newText)
        Next
    End Sub
End Class

我已经在 vs 2015 和 2019 社区中尝试过这个

哪里出错了?

你把它们都放在了同一个位置

newText.Location = New Point(12 + 5, 12 + 5)

使用您的 'z' 索引将它们放在不同的位置以便能够看到它们

从代码的外观来看,您创建的控件是正确的,但它们的位置对所有控件来说都是相同的,本质上,它们被放置在另一个之上,第一个与第二个隐藏在一起,与第三个隐藏。

newText.Location = New Point(12 + 5, 12 + 5)

需要修改以将标签放置在不同的位置。

也许,像这样:

newText.Location = New Point(12 + 5, 12 + (z * 25))

这将使标签垂直对齐,标签之间的间距为 25

对我来说,在 TableLayoutPanel 中包含控件然后将 TLP 添加到任何控件集合中更容易,例如 GroupBox 这样您就可以将标签与文本框结合起来,例如.下面是一个示例,说明如何从 DataTable 创建控件。在您的情况下,您只需要 1 个 ColumnStyle 用于标签,我只是想向您展示未来快捷方式的良好做法。 (我很少用设计器来放置控件)

    'Start test data
    Dim DtTable As New DataTable
    With DtTable
        Dim NewDtRow As DataRow = .NewRow
        For i As Integer = 0 To 25
            Dim DtCol As New DataColumn With {.ColumnName = "Col" & i, .DataType = GetType(String)}
            .Columns.Add(DtCol)
            NewDtRow(DtCol.ColumnName) = "Test" & i
        Next
        .Rows.Add(NewDtRow)
    End With
    'End test data

    Dim TLP1 As New TableLayoutPanel With {.Name = "TlpFields"}
    With TLP1
        .BorderStyle = BorderStyle.Fixed3D
        .CellBorderStyle = TableLayoutPanelCellBorderStyle.Inset
        .AutoScroll = True
        .AutoSize = True
        .RowStyles.Clear()
        .ColumnStyles.Clear()
        .Dock = DockStyle.Fill
        .ColumnCount = 2
        .ColumnStyles.Add(New ColumnStyle With {.SizeType = SizeType.AutoSize})
    End With

    For Each DtCol As DataColumn In DtTable.Columns
        With TLP1
            .RowCount += 1
            .RowStyles.Add(New RowStyle With {.SizeType = SizeType.AutoSize})

            'create labels
            .Controls.Add(New Label With {
                                          .Text = DtCol.ColumnName,
                                          .Anchor = AnchorStyles.Right}, 0, .RowCount)

            'create textboxs 
            Dim TxtBox As New TextBox With {
                                .Name = "TextBox" & DtCol.ColumnName,
                                .Size = New Size(170, 20),
                                .Anchor = AnchorStyles.Left}

            'add binding
            TxtBox.DataBindings.Add("Text", DtTable, DtCol.ColumnName)
            .Controls.Add(TxtBox, 1, .RowCount)
        End With
    Next

    Controls.Add(TLP1)