如何使用Code添加一些文本框

时间:2014-11-14 15:27:59

标签: vb.net

我遇到这个问题 Click Here for picture
守则

Dim Tex1 As TextBox = Nothing  
Dim Tex2 As TextBox = Nothing
Tex1 = New Windows.Forms.TextBox
Tex1.Name = "TextBox"
Tex1.Location = New System.Drawing.Point(12, 119)
Tex1.Size = TextBox1.Size
Tex1.TabIndex = 4
Tex2 = New Windows.Forms.TextBox
Tex2.Name = "TextBox"
Tex2.Location = New System.Drawing.Point(110, 119)
Tex2.Size = TextBox2.Size
Tex2.TabIndex = 5
Me.Controls.Add(Tex1)
Me.Controls.Add(Tex2)

我想通过点击图片框添加新文本框,就像这样 Click Here for Picture
但当我点击"添加图片框"文本框不会出现 拜托,有人帮帮我吗?

1 个答案:

答案 0 :(得分:0)

如果您希望能够在表单中添加文本框,那么这就是您应该做的。首先,您需要确定要用于此插入事件的按钮。我会假设绿色加号。

然后我会创建一个像这样的鼠标事件动作:

Public Sub Mouse_Click(sender As Object, e As EventArgs) Handles MyButton.Click
    ' Some action...
End Sub

从那里,您可以将代码插入点击事件,如下所示:

Public Sub Mouse_Click(sender As Object, e As EventArgs) Handles MyButton.Click
    Dim Tex1 As TextBox = Nothing  
    Dim Tex2 As TextBox = Nothing
    Tex1 = New Windows.Forms.TextBox
    Tex1.Name = "TextBox"
    Tex1.Location = New System.Drawing.Point(12, 119)
    Tex1.Size = TextBox1.Size
    Tex1.TabIndex = 4
    Tex2 = New Windows.Forms.TextBox
    Tex2.Name = "TextBox"
    Tex2.Location = New System.Drawing.Point(110, 119)
    Tex2.Size = TextBox2.Size
    Tex2.TabIndex = 5
    Me.Controls.Add(Tex1)
    Me.Controls.Add(Tex2)
End Sub

现在这个代码仍有一个大问题,即文本框的位置。这是一个问题的原因是因为它们是静态的,这意味着它们将始终出现在同一个位置,因此,一种解决方案是声明全局Point()以保持最新文本框的位置,如下所示:

Dim tbLocation1 As Point = New Point(12, 199)
Dim tbLocation2 As Point = New Point(110, 199)

从那里你需要做的就是有一些你希望移动文本框的边距金额,如下所示:

Dim marginAmt As Int32 = 30

既然存在所有碎片,就把它放在一起:

Dim tbLocation1 As Point = New Point(12, 199)
Dim tbLocation2 As Point = New Point(110, 199)
Dim marginAmt As Int32 = 30

Public Sub Mouse_Click(sender As Object, e As EventArgs) Handles MyButton.Click
    Dim Tex1 As TextBox = New Windows.Forms.TextBox 
    Dim Tex2 As TextBox = New Windows.Forms.TextBox

    ' Modifies Tex1
    Tex1.Name = "TextBox"
    Tex1.Location = tbLocation1
    Tex1.Size = TextBox1.Size
    Tex1.TabIndex = 4

    ' Modifies Tex2
    Tex2.Name = "TextBox"
    Tex2.Location = tbLocation2
    Tex2.Size = TextBox2.Size
    Tex2.TabIndex = 5

    ' Updates form
    Me.Controls.Add(Tex1)
    Me.Controls.Add(Tex2)

    ' Updates the point locations
    tbLocation1 = New Point(tbLocation1.X, tbLocation1.Y + marginAmt)
    tbLocation2 = New Point(tbLocation2.X, tbLocation2.Y + marginAmt)

End Sub

现在您只需要将此事件操作连接到您想要的按钮。