Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 2005 > Visual Basic 2005 Basics
Visual Basic 2005 Basics If you are new to Visual Basic programming with version 2005, this is the place to start your questions. For questions about the book: Beginning Visual Basic 2005 by Thearon Willis and Bryan Newsome, ISBN: 0-7645-7401-9 please, use this forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Visual Basic 2005 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old November 15th, 2006, 07:33 AM
Registered User
Join Date: Nov 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Reference to parent form from child

Hi All

I got a small problem with VB 2005

I have let's say Form1 .Form1 has a Text Box1 and a Button1. When I click
the Button1 in a Form1, Form2 opens which has a button2 and a text box2 as
When i click the button2 in the Form2 , Form2 should close and value in the
text box2 should be copied to Form1 Textbox1.
I know that I have to pass the Text box control to Form2 constructer when I
open the Form2

But what is the standard way of doing this?

Can you reference the Form1 current instance in some way?

/Best Regards

Old November 15th, 2006, 02:58 PM
Friend of Wrox
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts

Forms are specialized classes.

I haven't tried this, but it might do. In the code VB generates to create the controls, change the declaration to Public. The form itself is a publicly available object, and after this change, the textbox ought to be a publicly exposed field.
Alternately, you could add a public Property to the form's class which manipulates the text box.
Old November 15th, 2006, 03:54 PM
Friend of Wrox
Join Date: May 2006
Posts: 643
Thanks: 0
Thanked 0 Times in 0 Posts

You ask "what is the standard way of doing this". There is no standard way - but there are lots of ways to do this.

This is a common requirement of dialog windows - to capture data that is used by the code that displayed the dialog.
What I typically do is add a method to Form2 whose purpose is to display the form and pass back the values that the form is collecting, so to speak.
For example - in your case you are only working with a single value:
You could have a method on form2 named ShowDetail that has a signature and body as follows:
Public Function ShowDetail(ByRef value As String) As DialogResult
    value = TextBox1.Text
    Return DialogResult.OK
End Function
You would instantiate an instance of Form2 and call its ShowDetail method. Of course, the return would not be hardcoded to OK - you would set the return based on the user clicking an OK button, or Cancel button, or whatever makes sense for your code. Your calling code would then proceed based on the return value from the ShowDetail call.

In a more complex situation, instead of passing a string variable, you would pass an object that is specificaly designed for the use of the dialog. This object would contain member fields and properties for each value of interest. It would typically also contain validation code and any other logic that allows the UI (form2) to merely act as a display and user input mechanism, without containing any business logic. As the properties are set on this object by the changing of values on the form, the object would alert the form when it is valid to enable the OK button... and so on.

Woody Z http://www.learntoprogramnow.com

Similar Threads
Thread Thread Starter Forum Replies Last Post
Posting asp form variables from child to parent aspless Javascript How-To 0 July 20th, 2007 10:36 AM
Submitting a parent form from a child window in IE livehed Other Programming Languages 0 February 15th, 2007 01:07 PM
Reload Parent form from Child form maulik33 ASP.NET 2.0 Basics 2 February 6th, 2007 12:53 PM
parent form from child form - urgent plz netfresher C# 1 November 8th, 2006 06:59 PM
parent child form data passing maddy137 ASP.NET 1.0 and 1.1 Basics 0 April 26th, 2006 01:00 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.