在 VB.NET 中动态添加文本框

Add TextBoxes Dynamically in VB.NET

我想以表格格式动态创建文本框。到目前为止,我已经成功地创建了 10 个垂直格式的文本框。但我想创建 10X10 的文本框网格。这是代码。此代码成功运行但仅创建 10 个文本框。我知道代码中有一个小错误,但我不明白。请帮助

Dim XPos, YPos As Integer
    Dim i As Integer = 1
    Dim j As Integer = 1
    Dim newBox As TextBox
    XPos = 20
    YPos = 30
    For i = 1 To 10
        For j = 1 To 10
            newBox = New TextBox
            newBox.Name = "txtR" & i & "C" & j
            newBox.Size = New Drawing.Size(54, 22)
            newBox.Location = New Point(XPos, YPos)
            newBox.Text = newBox.Name
            Me.Controls.Add(newBox)
        Next
        YPos += 30
    Next

下面的代码可以帮到你

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim XPos, YPos As Integer
    Dim i As Integer = 1
    Dim j As Integer = 1
    Dim newBox As TextBox
    XPos = 20
    YPos = 30
    For i = 1 To 10
        XPos = 20
        For j = 1 To 10
            newBox = New TextBox
            newBox.Name = "txtR" & i & "C" & j
            newBox.Size = New Drawing.Size(54, 22)
            newBox.Location = New Point(XPos, YPos)
            newBox.Text = newBox.Name
            Me.Controls.Add(newBox)
            XPos += newBox.Width + 5
        Next
        YPos += 30
    Next
End Sub

您好,添加任务EXAMPLE 这里的代码在运行时将文本框添加到面板 (container_control) 然后将面板的滚动条移动到当前和最后一个文本框将添加:

Dim txt As TextBox
Dim newLine As Integer = 50
Dim taskN As Integer = 1




Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Panel1.AutoScrollPosition = New Point()

    txt = New TextBox()
    txt.Name = "note1"

    'txt.BringToFront()

    txt.Location = New System.Drawing.Point(111, newLine)
    txt.Size = New System.Drawing.Size(318, 68)
    txt.Text = "Task" + taskN.ToString()

    txt.Font = New Font("Verdana", 30, FontStyle.Regular)

    Panel1.Controls.Add(txt)

    Me.Text = Panel1.AutoScrollPosition.ToString()
    Label1.Text = txt.Location.ToString()

    newLine += 60
    taskN += 1
    'Me.SuspendLayout()

    Panel1.ScrollControlIntoView(txt)