How do I refer to Startup Form Controls from another form.

J

JAPSTER

An application has a startup form (Form1). A second form (frm2),
created from within Form1. Each form has a textbox.
To access the text in TextBox1 in frm2 from Form1, you use:

frm2.TextBox1.Text = "XXXXXX"

How do I refer to the TextBox1 controls Text property in Form1 from
frm2?

Form1.TextBox1.Text doesn't work and generates a designtime error:
Reference to a non-shared member requires an object reference.
 
L

Larry Woods

Here's the way I do it....

In form2: Add a form-level variable and change the constructor for form2:
Private ff As Form
#Region " Windows Form Designer generated code "
Public Sub New(ByVal fx As Form)
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
ff = fx
'Add any initialization after the InitializeComponent() call
............... ETC.
Then in the Load method of form2 you can do something like this:


Dim f As Form1

f = CType(ff, Form1)

TextBox1.Text = f.TextBox1.Text

......................

The above can be further simplified of you KNOW that you will ONLY be
instatiating this form from form1:

Private ff As form1
#Region " Windows Form Designer generated code "
Public Sub New(ByVal fx as Form1)
.....
ff=fx
.... etc.

Then in Load:

Textbox1.text=ff.Textbox1.Text



HTH,

Larry Woods
 
J

JAPSTER

I found a way to do it.

Add a new friend module.

Friend Mudule Module1
Friend Frm1 as Form1
Friend Frm2 as Form2
End Module

In Form1_Load add:

Frm1 = Me
Frm2 = New Form2
Frm2.Show()

In Textbox1_TextChanged on Form1 add:

Frm2.Textbox1.Text = Textbox1.Text

In Textbox1_TextChanged on Form2 add:

Frm1.Textbox1.Text = Textbox1.Text

That, I found, was the easiest method to solve this problem.
Thank all for your input.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top