Update Form2.Textbox1.Text from Form1

Alientude

New member
Joined
Mar 10, 2008
Messages
2
Programming Experience
Beginner
I'm pretty damn new to programming, but I have a basic understanding of the concepts. Right now though, I'm really struggling to update the text of a textbox on Form2 with a button on Form1 (names changed for ease of understanding).

Form1 and Form2 are both declared as Public Shared forms.
Form1 creates Form2 with this code:

VB.NET:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim NewForm1 As New Form2()
        NewForm1.Show()
    End Sub

In Form2, I created Textbox1 and set the Modifiers property to Public.

I know I can read the text from Form2.Textbox1.Text from Form1, because my first test with the button was:

VB.NET:
Private Sub btnTest_Click(ByVal sender As System.Object,_
 ByVal e As System.EventArgs) Handles btnTESTFindFight.Click

MessageBox.Show(Form2.Textbox1.Text)

End Sub

And the MessageBox correctly displays whatever is in the textbox. However, I'm running into trouble adding text to the textbox with that button. My code for that is simpe:

VB.NET:
Private Sub btnTest_Click(ByVal sender As System.Object,_
 ByVal e As System.EventArgs) Handles btnTESTFindFight.Click

Form2.Textbox1.Text = "TEST"

End Sub

When I click the button, nothing happens in the textbox. Can anybody tell me what I'm doing wrong? Thanks.
 
Last edited:
Form2.Textbox1 <--- you're using the default instance of Form2 class
Dim NewForm1 As New Form2() <--- you have created a new instance of the Form2 class

Only create new instances of Form2 if you need multiple Form2 instances at the same time, else just use the default instance ---> Form2.Show()
 
Back
Top