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 / .NET Framework / General / April 2005

Tip: Looking for answers? Try searching our database.

Accessing controls in another form

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
DaveG - 30 Apr 2005 12:29 GMT
Hi

Using VB.net 2003

I know how to access controls on a 2nd form after declaring the form in
the first.

But how do I access the controls on the original form from the 2nd form.

(in frmMain)
   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles Button1.Click
       Dim form2 As New Form2
       form2.TextBox1.Text = TextBox1.Text
       form2.Show()
   End Sub

(in Form2)
   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       Dim form1 As New Form1
       form1.TextBox1.Text = TextBox1.Text
       form1.Update()
       Me.Close()

   End Sub

Form2 textbox updates ok
The textbox in form1 does not update and still holds the original text
before opening Form2

This is just a simple example I cannot get working, once I can get this
working the I should not have problems with the actual code I need to use.

There must be a simple answer to this....???

Thanks in advance for any help given

DaveG
Morten Wennevik - 30 Apr 2005 12:56 GMT
Hi Dave,

You need to pass a reference to your 1st form when declaring the 2nd, typically

Form2 f2 = new Form2(this); // Me in VB

Overload the constructor of the 2nd form to accept a 1st form parameter.
Store the parameter for later use

> Hi
>
[quoted text clipped - 34 lines]
>
> DaveG

Signature

Happy coding!
Morten Wennevik [C# MVP]

DaveG - 30 Apr 2005 20:51 GMT
Thanks Morten

Noe the problem is I have used the overload in normal funtions and subs
but never with the constructors, so now I'm a little lost, I understand
the reasons for the overload..... so the New form2(Me) will be excepted
bur how to implement it is where I am stuck......

> Hi Dave,
>
[quoted text clipped - 43 lines]
>>
>> DaveG
Morten Wennevik - 30 Apr 2005 22:46 GMT
Dave, overloading a constructor is done the exact same way, although I'm not sure how the VB syntax is.  Store the Form1 reference for later use.

private Form1 myParent;

public Form2(Form1 f)
{
    myParent = f;   
}

then simply call

myParent.MethodOrSimilarInForm1()

whenever you need.

> Thanks Morten
>
[quoted text clipped - 50 lines]
>>>
>>> DaveG

Signature

Happy coding!
Morten Wennevik [C# MVP]


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.