BridgeGuy
12-31-2003, 10:14 AM
I am a old VB6 programmer trying to learn VB.Net.
Under VB6, when referencing a control on another form, the only code needed to reference TextBox1 control on Form1 from the Click Eevent of Form2, would be following;
Form1.TextBox1.Text = "Test"
Now for my problem.
I tried a sample VB.Net application that has two forms, Form1 and Form2, each with a TextBox1 control. Form1 is designated as the project Startup Form. In Form1's Public Class module, I placed the declaration;
Public Form2 as New Form2
In Form1's Click event I placed the following code to Show Form2 and place the message "Test" in TextBox1.
Private Sub Form1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Click
Form2.TextBox1.Text = "Test"
Form2.Show()
End Sub
In Form2's Click Event I placed the following Code in an attempt to change the text in TextBox1 on Form1.
Private Sub Form2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Click
Dim frm As Form1
frm.TextBox1.Text = "It worked"
End Sub
When I run this code I get the following error;
An unhandled exception of type 'System.NullReferenceException' occurred in WindowsApplication1.exe
Additional information: Object reference not set to an instance of an object.
My question is this, how do I get an Object Reference to Form1?
Any help would be appreciated.
Thanks,
Jim
Under VB6, when referencing a control on another form, the only code needed to reference TextBox1 control on Form1 from the Click Eevent of Form2, would be following;
Form1.TextBox1.Text = "Test"
Now for my problem.
I tried a sample VB.Net application that has two forms, Form1 and Form2, each with a TextBox1 control. Form1 is designated as the project Startup Form. In Form1's Public Class module, I placed the declaration;
Public Form2 as New Form2
In Form1's Click event I placed the following code to Show Form2 and place the message "Test" in TextBox1.
Private Sub Form1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Click
Form2.TextBox1.Text = "Test"
Form2.Show()
End Sub
In Form2's Click Event I placed the following Code in an attempt to change the text in TextBox1 on Form1.
Private Sub Form2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Click
Dim frm As Form1
frm.TextBox1.Text = "It worked"
End Sub
When I run this code I get the following error;
An unhandled exception of type 'System.NullReferenceException' occurred in WindowsApplication1.exe
Additional information: Object reference not set to an instance of an object.
My question is this, how do I get an Object Reference to Form1?
Any help would be appreciated.
Thanks,
Jim