469,636 Members | 1,970 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,636 developers. It's quick & easy.

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

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.

Nov 21 '05 #1
3 2305
"JAPSTER" <wh*******@hotmail.com> schrieb:
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?


Providing a reference to an application's main form
<URL:http://dotnet.mvps.org/dotnet/faqs/?id=accessmainform&lang=en>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #2
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

"JAPSTER" <wh*******@hotmail.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
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.

Nov 21 '05 #3
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.

Nov 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Simon Harvey | last post: by
4 posts views Thread by Tony Vitonis | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.