Referencing text values from other forms

dwilliams
01-30-2004, 07:56 AM
VB .NET seems very different from straight VB5/6. I can't seem to figure out how to reference a value from another form.

I have form "A" where user enters text into a textbox and presses a button to invoke form "B". In the loading of form "B", I want to get the text from the textbox on form "A" the user entered. In VB5/6 I could just do something like:

formA.textbox1.text

With .NET I get "Reference to a non-shared member requires an object reference."

How do I get my code to recognize formA?

Csharp
01-30-2004, 08:07 AM
assuming you have 2 Forms ( Form1 and Form2 ) with a TextBox on Form1 that you wish to access from Form2 ....
in your main form when opening Form2 ....

'/// in Form1 ( your Main Form )
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim frm2 As New Form2()
MyBase.AddOwnedForm(frm2) '/// make sure you add Form2 as Owned by this one.
frm2.Show()
End Sub

in Form2 , to Access Form1's Controls ....

'/// in Form2 , to Access TextBox1 on Form1...
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim frmMain As Form1 = DirectCast(Me.Owner, Form1)
frmMain.TextBox1.Text = "some text from form2 to Form1"
End Sub

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum