Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / VB.NET / December 2005

Tip: Looking for answers? Try searching our database.

Acces from one form to another form's controls.

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Mrozu - 20 Dec 2005 19:31 GMT
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
Matthew.Gertz@feedback.microsoft.com - 20 Dec 2005 19:44 GMT
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
Mrozu - 20 Dec 2005 19:55 GMT
Sorry but I'm using VB 2003:/ What then?? Your code isn't working:/

Thx Mrozu
Mrozu - 20 Dec 2005 20:59 GMT
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
Mrozu - 20 Dec 2005 21:23 GMT
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
Matthew.Gertz@feedback.microsoft.com - 20 Dec 2005 23:03 GMT
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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.