2014-11-14 54 views
-2

我有這個
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
但當我點擊「添加圖片框」的文本框不出現 請,任何人都可以幫我嗎?

+0

由於問題至少始於picturebox點擊事件,請顯示*代碼 – Plutonix 2014-11-14 15:30:59

+0

增加您的位置X和Y座標,否則您將在舊的座標上添加新的位置 – 2014-11-14 15:41:32

回答

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 

現在您只需將此事件操作連接到您所需的按鈕即可。

+0

這是行得通! 謝謝! – 2014-11-15 08:44:32