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

时间:2019-06-12 09:18:49

标签: vb.net visual-studio-2015 visual-studio-2019

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

用于棋盘游戏的玩家名称;直到游戏开始之前,玩家的数量是未知的。我设法自己弄清楚了如何使用动态数组功能来创建玩家列表。我使用了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

我已经在2015年和2019年社区中尝试过此操作

哪里出问题了?

3 个答案:

答案 0 :(得分:1)

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

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

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

也许是这样的:

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

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

答案 1 :(得分:0)

您将它们全部放置在同一位置

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

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

答案 2 :(得分:0)

对我来说,将控件包含在TableLayoutPanel中然后将TLP添加到曾经有过的控件集合(例如GroupBox)中比较容易,例如,您可以将Label与TextBox耦合。这是一个示例,您可以如何从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)