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