By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,719 Members | 1,046 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,719 IT Pros & Developers. It's quick & easy.

Acces from one form to another form's controls.

P: n/a
Hi

I have frm1. On this form button.Click code for this
button is:

Dim frm2 as New frm2
frm2.show

So after click, frm2 form is shown.

And I want to click on this shown form(frm2) button, and then textbox
on frm1
(this main form ) immediately change. How?? When I add to button on
frm2 this code, it isn't working:

dim frm1 as new frm1
frm1.textbox.text="Hello"
Thx Mrozu

Dec 20 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Hi, Mrozu,
Form2 doesn't know about Form1. What you're doing in your code below is defining a *new* form1 and trying to set the value on that, which won't work.

Instead (assuming that you're using VS2005), remove all of the code you wrote in form2's button code and instead add the following line:

My.Forms.Form1.TextBox1.Text = "hello"

That will get it done for you.

Hope this helps!
--Matt Gertz--*
VB Compiler Dev Lead

-----Original Message-----
From: Mrozu
Posted At: Tuesday, December 20, 2005 11:31 AM
Posted To: microsoft.public.dotnet.languages.vb
Conversation: Acces from one form to another form's controls.
Subject: Acces from one form to another form's controls.
Hi

I have frm1. On this form button.Click code for this
button is:

Dim frm2 as New frm2
frm2.show

So after click, frm2 form is shown.

And I want to click on this shown form(frm2) button, and then textbox
on frm1
(this main form ) immediately change. How?? When I add to button on
frm2 this code, it isn't working:

dim frm1 as new frm1
frm1.textbox.text="Hello"
Thx Mrozu
Dec 20 '05 #2

P: n/a
Sorry but I'm using VB 2003:/ What then?? Your code isn't working:/
Thx Mrozu

Dec 20 '05 #3

P: n/a
Ohhh I think that i found the way.

Code for button on frm2:

frm1.definstance.textbox1.text="hello"

And it is working. But maybe you know better solution;)) If yes, tell
me it.
Thx Mrozu

Dec 20 '05 #4

P: n/a
Oh yes now I see that it isn't very good way.

I get it, when I convert simple VB6 project which can do what i want,
to VB 2003 by converter. And it is working, but converter add very
strange code:

Region "Upgrade Support "
Private Shared m_vb6FormDefInstance As Form2
Private Shared m_InitializingDefInstance As Boolean
Public Shared Property DefInstance() As Form2
Get
If m_vb6FormDefInstance Is Nothing OrElse
m_vb6FormDefInstance.IsDisposed Then
m_InitializingDefInstance = True
m_vb6FormDefInstance = New Form2
m_InitializingDefInstance = False
End If
DefInstance = m_vb6FormDefInstance
End Get
Set(ByVal Value As Form2)
m_vb6FormDefInstance = Value
End Set
End Property
#End Region

to all forms. I think that it isn't very good. Any idea how do that
better??

Thx Mrozu

Dec 20 '05 #5

P: n/a
So, if you're not using VS2005, there are several things you can do:

(1) You can find Form1 (or whatever it's name is) from Application.OpenForms.Item("Form1"), iterate through the resulting form object to find the "TextBox1" control (or whatever you called it), cast it to a TextBox control, and set the text. That's kind of a clumsy way to do it.

(2) You can create a property on Form2 which gets/sets a value of type Form1 (for example, public property FirstForm(ByVal frm as Form1), which uses a field you create, for example Private _FirstForm as Form1). Then, after you create Form2, but before you Show() it, call the property on it to set the reference to Form1 (for example, frm2.FirstForm() = Me). You can then change your button code in Form2 to be FirstForm.TextBox1.Text = "hello". This is probably the way I'd do it -- only the forms which care about this sort of access will be exposed through it.

(3) You could create a global variable of type Form1 which is set by your instance of form1 on its Load event. You can then use this variable in form2 code. This is not an elegant way of doing it, in my opinion, since I feel that global variables should be avoided. This is essentially what the upgrade code you mention below does, though -- it's accounting for the fact that there are no default instances in VB.NET, and so it's creating a way to get at the default instance of a given form.

Etc... there are many other ways of doing this as well, some of which depend on whether or not Form1 is the startup form.

--Matt--*

-----Original Message-----
From: Mrozu
Posted At: Tuesday, December 20, 2005 1:24 PM
Posted To: microsoft.public.dotnet.languages.vb
Conversation: Acces from one form to another form's controls.
Subject: Re: Acces from one form to another form's controls.
Oh yes now I see that it isn't very good way.

I get it, when I convert simple VB6 project which can do what i want,
to VB 2003 by converter. And it is working, but converter add very
strange code:

Region "Upgrade Support "
Private Shared m_vb6FormDefInstance As Form2
Private Shared m_InitializingDefInstance As Boolean
Public Shared Property DefInstance() As Form2
Get
If m_vb6FormDefInstance Is Nothing OrElse
m_vb6FormDefInstance.IsDisposed Then
m_InitializingDefInstance = True
m_vb6FormDefInstance = New Form2
m_InitializingDefInstance = False
End If
DefInstance = m_vb6FormDefInstance
End Get
Set(ByVal Value As Form2)
m_vb6FormDefInstance = Value
End Set
End Property
#End Region

to all forms. I think that it isn't very good. Any idea how do that
better??

Thx Mrozu
Dec 20 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.