【问题标题】:Add TextBoxes Dynamically in VB.NET在 VB.NET 中动态添加文本框
【发布时间】:2015-05-08 00:09:24
【问题描述】:

我想以表格格式动态创建文本框。到目前为止,我成功地创建了 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

【问题讨论】:

  • 您没有在j 循环中增加XPos,您的文本框是在彼此之上创建的。

标签: vb.net visual-studio-2012 textbox


【解决方案1】:

下面的代码会帮助你

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

【讨论】:

    【解决方案2】:

    您好,添加任务示例 此处代码将运行时中的文本框添加到面板(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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多