Article Options
Recently Viewed
Premium Sponsor
Premium Sponsor

 »  Home  »  Upgrading  »  Multiple Forms in VB.NET. Part 4 - Accessing Controls and Data on the StartUp Form
 »  Home  »  Windows Development  »  Win Forms  »  Multiple Forms in VB.NET. Part 4 - Accessing Controls and Data on the StartUp Form
Multiple Forms in VB.NET. Part 4 - Accessing Controls and Data on the StartUp Form
by Ged Mead | Published  01/25/2004 | Upgrading Win Forms | Rating:
Ged Mead

Ged Mead (XTab) is a Microsoft Visual Basic MVP who has been working on computer software and design for more than 25 years. His journey has taken him through many different facets of IT. These include training as a Systems Analyst, working in a mainframe software development environment, creating financial management systems and a short time spent on military laptop systems in the days when it took two strong men to carry a 'mobile' system.

Based in an idyllic lochside location in the West of Scotland, he is currently involved in an ever-widening range of VB.NET, WPF and Silverlight development projects. Now working in a consultancy environment, his passion however still remains helping students and professional developers to take advantage of the ever increasing range of sophisticated tools available to them.

Ged is a regular contributor to forums on vbCity and authors articles for DevCity. He is a moderator on VBCity and the MSDN Tech Forums and spends a lot of time answering technical questions there and in several other VB forum sites. Senior Editor for DevCity.NET, vbCity Developer Community Leader and Admin, and DevCity.NET Newsletter Editor. He has written and continues to tutor a number of free online courses for VB.NET developers.

 

View all articles by Ged Mead...
Multiple Forms in VB.NET. Part 4 - Accessing Controls and Data on the StartUp Form

Article source code: multipleforms4.zip

Introduction

In previous articles, we looked at some common problems that seem to have caused problems for many newcomers to VB.NET. In this article we will take a look at a specific area which appears to contribute more than its fair share of grief to many people in the early days of the .NET learning curve.

As with all the previous articles, this one is aimed squarely at .NET Newbies and tries to provide explanation and solutions with the least possible amount of techno-speak. Inevitably this sometimes means that strict technical accuracy has to take a back seat where a tricky concept is being explained, but it's all in a good cause!

The Background

Some of the previous articles have covered methods of accessing controls and data between multiple forms. On the whole, though, those articles have tended to use examples which get information from a second form in order to use it or show it in the Startup Form. This time we are going to concentrate on the reverse requirement – getting data or accessing a control from the startup form and using it in another form. Until you know how, this is not quite as easy as most of us would think it should be.

So, what is the big difference that causes us problems when we want to refer back to our startup form, Form1? Well, what those other forms need is a reference that they can use in order to get at that first form. As I've mentioned in previous articles, a lot of the trouble is caused by the way we've come to think of Forms as being different from other objects, due to our VB.Old upbringing. There's also a rather unhelpful smokescreen unwittingly caused by the way that this startup object (Form1) is automatically created for you in Visual Studio as the project fires up and runs.

You know that when you need to create an instance of Form2, you use code such as:

Dim F2 As New Form2()

F2.Show()

and you'll be aware that F2 is a variable which holds a reference to that instance of a Form2 object. Once you have that reference in your object variable, you can access the properties or invoke the methods and events of that Form2 instance – BUT, and this is the key issue here – you can only do it via that F2 variable that you created.

At the risk of labouring it, the point I'm making here is that there is no

Form2.Show

in the example above. Form2 is the Class; F2 is a variable that holds a reference to an instance of that class. Just like any other object you would use in VB.NET.

So what we really need is the "Form1/F1" equivalent of this setup.

But where's the Form1 Variable?

But what can sometimes confuse us at this point is that the instance of Form1 that the project sets up and gets running for you is done somewhere behind the scenes. You don't see any similar code in your project that creates your startup form, Form1. There isn't a:

Dim F1 as New Form1

F1.Show

code snippet created for you automatically in Visual Studio that you can actually see. And yet, an instance of a Form1 object must have been created somehow as soon as you started to run the project.

Side-stepping the mechanism that does this for us, what we need to know is how we can create a reference to that instance of Form1 in the same way that we successfully did with Form2. We need a reference to that currently existing Form1. And of course we don't want to fall into the trap of creating another Form1 instance, as we might be tempted to do, with:

Dim F1 as New Form1

either. If you use code like this, you will get exactly what you asked for – a new Form1 – and you'll then have two of them bouncing about in your project, possibly causing all kinds of confusion and mayhem if you're not aware that this has happened.

The Solution

Having looked into the background in some detail, let's move on to the solution to the problem. By far the easiest way of doing this is to create a Friend variable in a module which holds a reference to a Form1 object. Why in a Module, do I hear you ask? The reason is that a variable located in a Module which has its scope declared as Friend can be "seen" – used and accessed - throughout this whole project or application. It makes life easier for you, the developer, and goodness knows we all want as much of that as we can get! (More on scope later).

Doing It The Module Way

Here's an example of what is needed:

1. First of all you will need a module; leave it as the default name of Module1.

Your project will already have a Form1. In order to make life easier for you, and for Intellisense to know of its existence, at this stage you should also add Form2 to the project. You can use the Add Windows Form menu item and accept the default name of Form2.

A Brief Look at Scope

In the Module, the first thing we should do is change the Scope of this module to Friend.

This simply means changing it so that it reads:

Friend Module Module1

Scope in an application is an aspect on which you should always keep a very watchful eye. In general terms, you should always aim to use the narrowest level of scope which will meet the application's needs.

In this case, our scope level of 'Friend' will mean that these variables and the forms they reference will be visible right across our current application, but no further.

Many Newbies are tempted to declare all variables as Public, or leave them at the default setting, which is often Public. It's a very easy trap to fall into. It's quick, it takes no thought, and it means you don't get those frustrating "xyz not declared" error messages at design time.

But – and it's a great big 'But' – if you fall into the habit of using wider scope than is needed, sooner or later it will come back to bite you.

An article covering many aspects of Variable Scope at the Module, Form and Method levels is planned for later publication, and this will go into much more detail of this whole area. For now, in its simplest terms, let's look at it like this: The wider the scope setting, the greater the chance that some part of the application is going to have access to it, quite possibly at a time you hadn't expected and with results you never intended. Or wanted.

So, bottom line: Keep Scope in declarations as narrow as possible.

Form Variables

What we are going to use this Module for is to create Variables that you can use to point to instances of each of the Forms in this application, one variable for each form.

The key point here is that this will include a form variable for Form1. And once we have this variable, we can treat our Form1 startup form in exactly the same way that we have become used to treating all other forms.

Friend F1 As Form1

Friend F2 As Form2

Friend F3 As Form3

Apologies for repeating myself, but it's crucial: do not use the 'New' keyword in these variable declarations. Notice also that the scope of each of the declarations is 'Friend'. (We've done this to avoid confusion. There are alternatives which produce the same result and these will be covered in the later article on Scope).

So at this point, you have declared a variable which will be used to hold a reference (i.e. a pointer) to an instance of a Form1 object. Right now, it is empty, has a value of nothing, nix, nowt, zilch. It has simply been declared, and when the time is right it will be given a reference to an instance of Form1 to hold in its care.

As it happens, in this case, that time is now.

2. In the Form_Load event of Form1, we now need to have our F1 variable hold the details of this instance of Form1. This is done with the following code:

F1 = Me

"Me" in this case referring to this current instance of Form1.

We now have a variable – F1 – which holds the reference to our current instance of Form1 (the instance that is automatically created for you at startup). Armed with this, we can get full access to what is in and on this first form from anywhere else in our project.

And we can now see this in action.

First though, let's just add some controls to Form1 which we can then use for demonstration purposes. Add the following controls to Form1:

A Button – Button1

A Label – Label1

A TextBox – TextBox1

Add this code to the various controls above:

Private Sub Button1_Click(ByVal sender As System.Object_
    ByVal e As System.EventArgsHandles Button1.Click

    If F2 Is Nothing Then

        F2 = New Form2()

        F2.Show()

    End If

End Sub

You'll probably have realized that the above code checks to see if there is already an instance of Form2 up and running, and creates one if not. One thing to note, though is this. We haven't used the usual

Dim F2 As New Form2

in the way that you normally might expect. It isn't necessary (and in fact would cause problems), because we already have the declaration of F2 in our Module – and any of the forms in the project can therefore get access to that variable.

So, to complete our small demonstration project, we need to code Form2 in a way that shows that we can access and even change data in Form1 by some action that we perform on Form2.

3. add the following two controls to this Form:

A Button - Button1

A Label – Label1

In the Form2 code window, include the following code:

Private Sub Button1_Click_1(ByVal sender As System.Object_
    ByVal e As System.EventArgsHandles Button1.Click

    '  Access the instance of Form1 and read it's textbox contents

    Me.Label1.Text = F1.TextBox1.Text

    '  Change Form1's Label1 text to prove that you

    '  can "write" to Form1 as well as get data from it

    F1.Label1.Text = "Form2's Button has been clicked"

End Sub

Important points to note in the above code are the uses of 'F1' to refer to our Form1 instance. Nothing else will do in this example – this is usually the place where .NET Newbies get stuck. As you can see, we are able to take data from Form1 and show it in Form2. We are also able to change what is shown on Form1 by invoking instructions carried out in Form2. Pretty powerful and useful stuff.

The attached demo solution takes the idea a stage further and includes a Form3, but the principles remain the same. The demo also includes some code that tidies up various minor areas that may cause problems or confusion, so I recommend that you take a look at it if you are planning to use the methods described in this article.

Summary

The key to this whole issue is the use of the Friend variables in the Module. By placing them there in the Module and giving them Friend scope, any form in the project can access those variables, and through them, can access the other Form instances.

Don't overlook the initialization of the F1 variable when Form1 is first loaded. Without that line of code:

F1 = Me

there would be no value held in F1 and the other forms would not be able to get access to the Form1 instance.

I'm not going to pretend that it's the easiest concept in the world to get to grips with first time round. But trust me, as with most things .NET related, it all eventually falls into place. If I can get to understand it, then there's hope for everyone else.

The primary aim of this article is to resolve the mystery of how to refer to the project's startup form from other forms. However, as you will see from the demo solution attached, if you create a Friend variable in the Module for each of the forms in your project, you can easily access each and every one of them at will. This can be a very versatile and easy to use tool which not only allows access to large numbers of forms in complex applications, but also helps retain control and avoids unwanted duplicate instances of forms.

As some readers will of course already know, the above method is just one way of dealing with this problem. There are others, but I thought that this approach would be of most use to .NET Newbies and I hope that many of you will find it of some use.


Acknowledgements: Thanks go to VBCity Leader Mike McIntyre for his help and encouragement with my personal .NET learning curve in general, and his guidance on this topic in particular. Any errors or omissions in the article are mine.

If you would like to see excellent examples of alternative and much more sophisticated approaches to this and similar problems, I strongly recommend that you visit the www.getdotnetcode.com web site.

Related devCity.NET articles:

How would you rate the quality of this article?
1 2 3 4 5
Poor Excellent
Tell us why you rated this way (optional):

Article Rating
The average rating is: No-one else has rated this article yet.

Article rating:4.59183673469387 out of 5
 196 people have rated this page
Article Score66991
Article Series
Related Articles
Comments    Submit Comment

Comment #1  (Posted by vb_student on 02/02/2004)

Sir,

Thank you very much for the article . It is very informative.

I have another question, and I would really appreciate if you could help me with it. In my datagrid, I have a column whose datatype is 'number'. This column has values of '0' and '-1'. Now, I would like to display this
column as a check box in the grid, in such a way that, the checkbox would appear checked if the value is '-1' and unchecked if the value is '0'. Is it possible to do this?

Thanks





 
Comment #2  (Posted by JL on 02/05/2004)

Brilliant article. Many thanks.
 
Comment #3  (Posted by betrl8thanever on 02/25/2004)

Great article!!!! This was by far my biggest point of frustration when I started learning .NET. Keep up the great work!!
 
Comment #4  (Posted by Andrew van Tilburg on 03/06/2004)

Hey Ged, Excellent article. I'm new to VB.NET and your straighforward approach to explaining things has been a big help. Keep up the good work. Regards, Andrew.
 
Comment #5  (Posted by Christopher K Crespo on 03/25/2004)

I can't tell you the numerous amounts of pain I've gone through trying to determine how module can use instances of declared forms (declared ONLY one time for each form used). The "Friend" keyword is something I've heard of yet haven't experienced with. This will bring me to a closer understanding of how to use the declared instance of each form once without having to re-creating another instance of another form (being and also understanding that the instances of each form are declared within the module variable retaining the instances without going out of memory OR WORST eating up memory by creating additonal instances of forms you do NOT EVEN NEED!!!). Regardless this article has brought me one step closer toward the successiablity of a splash screen and the use of other forms without having to create unwanted instances of them. most of all this article as allowed me the ability to interact with controls from other form as well. Now all I have to do is experiment with .ShowDialog, .Show, .Hide, and possiblity the .Dispose method to determine how I can get one form to appear out of another form when a user clicks on for example a button that is responsible for showing the other form(s).

Thanks yet again
Kenji
 
Comment #6  (Posted by mansyno on 04/25/2004)

Realy a Great article and a great idea.
can you tell us about an articel (yours or others) that describes the other ways?
thanks
Daniel

p.s.
sorry for my spelling
 
Comment #7  (Posted by Alex Hopkins on 04/29/2004)

This series of articles has been terrifically helpful. I am hanging out for the next; the prospect of still more help on variable scope is tantalising. Ged Mead, you are a great teacher. Much thanks,

Alex Hopkins
 
Comment #8  (Posted by blue_kiss on 05/26/2004)

Thank you very much for your series of articles. It's been of great help to me, and i believe it'll help my fellow newbies too. Great work, hope to see more of your articles in the future.
 
Comment #9  (Posted by anupjose on 06/03/2004)

sir
cann u help me to solve a problem

how can i close parentform without closing the child form
 
Comment #10  (Posted by anupjose on 06/03/2004)

sir
cann u help me to solve a problem

how can i close parentform without closing the child form
 
Comment #11  (Posted by XTab on 06/03/2004)

Hi anupjose
I'm no great expert in MDI, but my understanding has always been that one of the key reasons for using it in a project is that it is designed to ensure that all child forms *are* closed when the parent is closed. i.e. No orphan forms are left hanging on the system eating up resources and/or cluttering up the screen. Maybe I didn't quite understand your question, but trying to leave a child form running seems to fly in the face of the core idea of using MDI in the first place. ;-}
Without knowing exactly what your project looks like it's difficult to advise on how you can get round your problem. But as a general tilt at the answer I think I'd have to go for the obvious one of asking if it wouldnt be possible to rejig this one child form out of the MDI set and make it a free-standing form in its own right. It could be called from the "parent" form or via some other means. You could then close the other "Parent" MDI form which would only affect the remaining child forms.
Sorry if this isn't much help for your particular problem but I don't see any other solution off the top of my head.


 
Comment #12  (Posted by Hrishikesh Gokhale on 06/07/2004)

hi

i have been looking for this solution for 4 hours. this article is really good and explanation is perfect.

This has been really helpful for my project

Thanks

Hrishikesh A Gokhale
 
Comment #13  (Posted by XTab on 06/08/2004)

Thanks to all for the positive feedback. It's good to hear that so many people have found the article(s) useful. I can still remember how frustrated and confused I was in the early days, so it's a pleasure to know that I've maybe made it a little easier for some.
XTab
 
Comment #14  (Posted by Charley Clements on 06/28/2004)

Absolutely OUTSTANDING! This series of articles on multiple forms is well written, easy to understand, and and helped me get over the hurdle of how to communicate between forms.

I spent hours digging through the countless and pretty much worthless "Samples" and "Walkthroughs" in the VB.Net and MSDN files trying to figure this out, and your articles explained it in minutes.

Thank you Sir!
 
Comment #15  (Posted by Michael McCowan on 07/21/2004)

This was great! But how do I execute a button on F2 from Form1. Like this:

F2.Button1.DoDragDrop

This does not work. Can anyone help me.... Thanks in advance.
 
Comment #16  (Posted by DotNET74 on 08/17/2004)

An another code.



In Form2:

Add a locale variable like:

Friend MyParent as Form1

Add a constructor like this:

Private Sub New(ByVal Parent as System.Windows.Forms.Form)
'Put the same code of the first constructor
MyParent = Parent
End Sub

In Form1:

Private Sub Button_Click(..)
Dim oForm2 as new Form2(Me)
oForm2.Show
End Sub

So,

You have access to all controls and properties of Form1 from Form2

Bye

 
Comment #17  (Posted by padmaja yekolla on 08/25/2004)

Sir,

Your article is very good and helped me a lot.

Thanks a lot

regards
padmaja yekolla
 
Comment #18  (Posted by J.Miltenburg on 09/01/2004)

Great Example.
A very simple example that shows a lot of the problems involved.
Most examples in Vb itself are far to complex.

 
Comment #19  (Posted by RC on 09/07/2004)

Nice one - thanks a lot had been struggling with it being a novice!
 
Comment #20  (Posted by Guy Incognito on 09/12/2004)

Good tutorial. Beats the crap out of the MSDN libraries explanation. Thanks
 
Comment #21  (Posted by an unknown user on 01/03/2005)
Rating
This is just what I needed after my long strugle thaese last few days ! Happy New Year 2005.
 
Comment #22  (Posted by an unknown user on 01/07/2005)
Rating
Absolutely brilliant...
I have been banging my head for a couple of days now with this exact problem...

Thanks alot..

 
Comment #23  (Posted by Neelima on 01/09/2005)
Rating
Simply superb....much relief after a long search on how to go about with multiple forms in a single project.
 
Comment #24  (Posted by an unknown user on 01/16/2005)
Rating
As a Newbie to .NET I found this article very helpful, and it has made the project flow smoothly for me...
 
Comment #25  (Posted by an unknown user on 01/18/2005)
Rating
I have recently start learning VB.Net using a book called SAMS Teach yourself Visual Basic.Net 2003 in 21 Days. I got to day 4 when I was required to write a multi-form app (not and MDI) that used two forms each with a text box. When the textbox on one form was changed, the other had to display the same contents. This had to work both ways.

After going back over what I had learnt, I could not see where I should have picked this up.

Your article was spot on. Now I can continue my studying.

I will definately be referring to you when I get stuck again.
 
Comment #26  (Posted by an unknown user on 02/02/2005)
Rating
I have been trying to figure out how to reference the current form for days and this article answered my question!!!
 
Comment #27  (Posted by taxes on 02/06/2005)
Rating
Excellently put. One point you omitted was that any objects to be accessed in one form from another must NOT be declared Private in their Modifiers property. The default value is Friend, which is OK.
 
Comment #28  (Posted by an unknown user on 02/13/2005)
Rating
Was very helpfull
 
Comment #29  (Posted by an unknown user on 02/16/2005)
Rating
Great and informative article.
 
Comment #30  (Posted by an unknown user on 02/17/2005)
Rating
WOW, this was killing me in my .NET 'conversion.' Thank you so much for this article, it has helped IMMENSELY!!
 
Comment #31  (Posted by an unknown user on 02/22/2005)
Rating
EXACTLY what I was looking for and well-written and easily understood!
 
Comment #32  (Posted by an unknown user on 02/22/2005)
Rating
worked perfectly
 
Comment #33  (Posted by an unknown user on 03/08/2005)
Rating
It's just excellent, helping me many things. Thanks a lot, Ged Mead.
 
Comment #34  (Posted by an unknown user on 03/13/2005)
Rating
i review other articles for the same subject and only this hasfull description and is easy o do, thank you
 
Comment #35  (Posted by an unknown user on 03/15/2005)
Rating
Very useful article, very clear even for a novice. I'll pass this article around to all my new vb.net developers. Thanks a lot.
 
Comment #36  (Posted by an unknown user on 03/24/2005)
Rating
It doesnt tell how you can access, for example 50 controls and linking them together on a single form.
 
Comment #37  (Posted by Ged Mead on 03/27/2005)
Rating
[Quote]: "It doesnt tell how you can access, for example 50 controls and linking them together on a single form." [/Quote]

Good Gracious! What a terrible oversight! Let me see where I went wrong ...... hmmmm, the first two words of the title of the article are "MULTIPLE FORMS" and for some inexplicable reason it fails to deal with a problem on a SINGLE form.
Actually, it doesn't deal with many other totally unrelated problems either, my anonymous friend (brave enough to criticise; not courageous enough to put your name to the criticism, I see). It deals explicitly with what it says it deals with; no more no less.
I wish you luck in your search for single form enlightenment elsewhere.
 
Comment #38  (Posted by an unknown user on 04/13/2005)
Rating
I SEE THE LIGHT!!
 
Comment #39  (Posted by an unknown user on 04/14/2005)
Rating
finally a good programmer explained the correct way to communicate between window forms in vb.net. you dont know how many people out there will answer questions about this and give lousy answers that dont help anyone. For instance i read that you can inherit the main form to access its components in a child form. and that great and all if you want all your forms looking alike with the same controls. thank you, thank you, thank you
 
Comment #40  (Posted by an unknown user on 04/21/2005)
Rating
thanks, I'm new and this was the answer I was looking for.
 
Comment #41  (Posted by Vlado Jasovic on 04/27/2005)
Rating
wow ... was looking for something like this ~3 days.

Thank you so much, keep up the great work !!!
 
Comment #42  (Posted by an unknown user on 05/02/2005)
Rating
Slick.

I have never tried this approach before.

Thanks.
 
Comment #43  (Posted by an unknown user on 05/06/2005)
Rating
Excellently written and easy to understand. Clear and concise. I think I've finally found THE site. Thanks Ged.
 
Comment #44  (Posted by an unknown user on 05/11/2005)
Rating
Interesting...
Module1
Friend F1 As Form1
Friend F2 As Form2
Friend F3 As Form3

This is exactly how I coded my MDI application without even thinking about it! With this approach, I am able to eliminate multiple forms open (check if the instance already exists) and then set the form to nothing in the form "Closing" event.
 
Comment #45  (Posted by an unknown user on 05/19/2005)
Rating
Awsome! Awsome! Awsome! Wow! I have been agonizing over this issue for a while.Your non-cursory approach to the solution is a delight to behold. Yes, I can firmly say that a FRIEND indeed came to the rescue. Thanks for a great job!
 
Comment #46  (Posted by an unknown user on 05/23/2005)
Rating
I foun this article extremely helpful. Ged has a way with words and is an extremely good teacher. I will be keeping an eye on this site for all articles since they are so good . Thanks again.

George
 
Comment #47  (Posted by an unknown user on 05/23/2005)
Rating
THANK YOU! The only problem was that I had to download the example to find out that I needed to put F2=nothing in the form2 closing section.
 
Comment #48  (Posted by an unknown user on 05/24/2005)
Rating
I search all ofer the internet to find a simple expaination as to how to do this type of linkage. I'm teaching myself VB.NET and it has be HARD !!!!
 
Comment #49  (Posted by an unknown user on 05/24/2005)
Rating
Very simple and well explained, perfect for a newbie as intended
 
Comment #50  (Posted by Eddie Robins on 06/21/2005)
Rating
Thanks for this. I couldn't find anything in MSDN to do this and wasted hours trying.

Lifesaver!
 
Comment #51  (Posted by Philip Halton on 06/30/2005)
Rating
What a shame the MSDN explanation wasn't as clear as this. I spent wasted hours as well, thank you so much.
 
Comment #52  (Posted by an unknown user on 07/07/2005)
Rating
great! thks!
 
Comment #53  (Posted by Gord on 07/20/2005)
Rating
Question: This technique limits you to a set number of instances of a form... one from1 and one form2. You can't have mutiples of form1. Therefore the forms are not re-useable right? Am I missing something?
i.e. an app shows a form for each customer, you want to display more than one customer form at a time (to compare them side-by-side)
 
Comment #54  (Posted by an unknown user on 07/26/2005)
Rating
Its excellent
 
Comment #55  (Posted by Steven Graham on 07/27/2005)
Rating
Hi, Firstly I would like to thank you for this extemely useful code, I am fairly new to vb.NET and I am find the transition more difficult then I first thought. Secondly I have a situation: I have 4 forms, lets call then form a b c and d.

I have the startup form (form a) which loads form b. then from form b it loads form c, then form c loads form d. now from form D how do I go about closing form1(or all forms)?

One would normally assume to call it like this maybe:
f1.close
f1.dispose

I have done this and on Windows mobile 2003 (first edition) it seems to work fine, however in Windows mobile 2003 SE it seems to just sit there.

I am developing in Visual Studio.NET 2003 and I was thinking that maybe this might have been something to do with an incompatibility in my visual studio with SE. Is there something I am missing? do I need s service pack or am I missing some exquizard piece of code...

Please help me.

 
Comment #56  (Posted by Naush on 07/28/2005)
Rating
Excellent Article... Thanks!
 
Comment #57  (Posted by Ged Mead on 08/01/2005)
Rating
Steven, As far as standard VB.NET goes, the closing of the Form1 variable (F1) will cause your application to end. This applies to both 2002 and 2003 versions of VS.NET. I have no experience with the other editions of VB.NET (e.g. mobile) so unfortunately can't advise you directly on that one. It would be worth posting a question in one of the forums to get an authorative answer on this.
 
Comment #58  (Posted by an unknown user on 08/09/2005)
Rating
its very poor improve urself first!!!!!
 
Comment #59  (Posted by an unknown user on 08/14/2005)
Rating
I have been trying to find some help in updating an existing list on a form for 3 days without luck and then on a Sunday afternoon came across this. I am so glad i have as it has helped me out enormously!!! If I knew where you were I would buy you a pint!

THanks again!
 
Comment #60  (Posted by an unknown user on 08/16/2005)
Rating
This article is exactly what I needed.

Thanks
 
Comment #61  (Posted by an unknown user on 08/20/2005)
Rating
Thanks Mike (by Bizmanger)
 
Comment #62  (Posted by mery on 08/20/2005)
Rating
thanks Mike
 
Comment #63  (Posted by DDelmont on 08/31/2005)
Rating
I'd been wracking my brain all morning trying to figure this part of multiple forms out and this article was *exactly* what I was looking for. Thanks so much for your help and wonderful articles!
 
Comment #64  (Posted by an unknown user on 09/08/2005)
Rating
Just starting to program in VB.net from VBA. This was very helpful in getting me started with movement between forms.
 
Comment #65  (Posted by an unknown user on 09/15/2005)
Rating
I'm new to vb.net but it appears this takes up twice the memory as having form1 by itself. At least all of the debugging tools show Me and F1 as separate memory values. I created something similar to the following:

1. Create a module.
2. In the module enter:

Public Myform1 As New Form1
Public Myform2 As New Form2
Public MyAppClosed As Boolean = False

Sub Main

Myform1.Show()
While MyAppClosed = False
Forms.Application.DoEvents()
End While

End Sub

3. Set MyAppClosed = True as many places as needed in the Form1 code exit routines.
4. Under Project Properties, Change the Startup Object to "Sub Main" instead of "Form1".
 
Comment #66  (Posted by SamuelK on 09/17/2005)
Rating
Very helpful article. Thanks !
 
Comment #67  (Posted by an unknown user on 09/17/2005)
Rating
You helped me alot.
 
Comment #68  (Posted by nags on 09/21/2005)
Rating
Really Very Very Professional Help for Growing Programmers.Thank you Very Much Mr.Ged Mead.
 
Comment #69  (Posted by BleaT! on 10/09/2005)
Rating
Hi, i've been doing this for a while, as i knew i had to do Public F1 as new Form1 in my module but this made it more clear what i was doing wrong, i did it in my start up form(Splash) as it wasn't as we would call it public enough, Cheers :) for shedding light on this for those of us who needed it
 
Comment #70  (Posted by an unknown user on 10/19/2005)
Rating
Nice Job Explaining how to talk to multiple forms !!!!

I have not found anything close to this on many searchs on the internet and message boards.

Great Work! A+
 
Comment #71  (Posted by an unknown user on 10/26/2005)
Rating
very helpful. thanks to author.
 
Comment #72  (Posted by Pete on 11/03/2005)
Rating
This is THE BEST multi form control article series available.
Please, take my word for it.
Ged, you are the only one who actually cares about us poor beginners. And you uderstand the frustration of not understanding the Hi-Tech talk, while desperately trying to proceed with learning.
Fantastic Job.
 
Comment #73  (Posted by an unknown user on 12/07/2005)
Rating
Hi,
This article is great and it helped me a lot in solving one of my dearest friend's problem. Keep up the good work.
 
Comment #74  (Posted by an unknown user on 12/18/2005)
Rating
Article is good.
How can i close a parentform and open a childform in VB.NET
 
Comment #75  (Posted by an unknown user on 12/27/2005)
Rating
worked like a charm. Thanks ...
 
Comment #76  (Posted by jaredson on 01/06/2006)
Rating
Sir, I have an MDI displaying a form (call it firstchild) on which you could click a link to various form (call it child1of1, child2of1...) all related to firstchild. How can I close child1of1 and return back to mdi child firstchild.
 
Comment #77  (Posted by Mr Lacy Brown on 01/20/2006)
Rating
Thank you very much for the information on forms. I have been struggling with alot of those issues you mentioned. I need one more piece to the puzzled, I need would like to use the 'module' item that you suggested in one of your parts. But I need to access the events of those forms. I tried declaring the form var in the module as "Public WithEvents f2 As form2" but the main form1 gives me the squiggly mark saying "Handle clause requires a WithEvents variable".
I appreciate any help. Thank you again, Lacy
 
Comment #78  (Posted by an unknown user on 03/08/2006)
Rating
Very good article,
I have not found anthing as good as this one.
jassi
 
Comment #79  (Posted by an unknown user on 03/09/2006)
Rating
The whole series are excellent articles. Great job of explaining key concepts in a very easy to follow manner with clear examples.
 
Comment #80  (Posted by an unknown user on 03/23/2006)
Rating
Outstanding! Thanks a bunch. The way I was doing this was horrible and resulted in "orphaned" forms hiding in the background. This is a straightforward, easy solution for us Noobs. Thanks!!!!
 
Comment #81  (Posted by an unknown user on 04/13/2006)
Rating
This article saved me a lot of hunting and searching to resolve this issue...Thanks!!
 
Comment #82  (Posted by an unknown user on 05/13/2006)
Rating
Clear and accessible.
 
Comment #83  (Posted by an unknown user on 05/16/2006)
Rating
At last, i'm understanding the concept. Thank you very much. Feel confident to move on now..
 
Comment #84  (Posted by SYED on 05/28/2006)
Rating
as am new to vb.net learning myself, i hav a question, i've created 5 forms in vb.net in design form, now how to call those forms from a button click event or through menu's pls help me..
thnx
 
Comment #85  (Posted by Ged Mead on 05/28/2006)
Rating
I think you will find the answer to your problem in Article 1 of this four part series. Follow this link: http://www.devcity.net/Articles/94/multipleforms.aspx

I hope you find the Part 1 article more than averagely helpful ;-}

 
Comment #86  (Posted by an unknown user on 06/20/2006)
Rating
i guess this is very basic level ... that's why...
 
Comment #87  (Posted by Ged Mead on 06/20/2006)
Rating
>> (Rating of 2) i guess this is very basic level ... that's why...

------------------------------------------------
(From the Introduction) As with all the previous articles, this one is aimed squarely at .NET Newbies and tries to provide explanation and solutions with the least possible amount of techno-speak. Inevitably this sometimes means that strict technical accuracy has to take a back seat where a tricky concept is being explained, but it's all in a good cause!
-----------------------------------------------
(Sigh)


 
Comment #88  (Posted by Ged Mead on 06/20/2006)
Rating
>>> How do you open an EXISTING form in this Vb .NEt they really made it crap .
(Answer) Check out the FIRST article in this series and you will see ways of accessing existing forms.

 
Comment #89  (Posted by Chandan Roy on 06/21/2006)
Rating
This article is quite good, but i have a question.
After creating calulator, i have problem in the backspace of calculator, if u can help please send me the code of backspace of calculator, means when one click backspace one integer from right side will remove.
Please send it to r_chandan12@rediffmail.com

 
Comment #90  (Posted by an unknown user on 08/05/2006)
Rating
this series of articles has helped me go from VB illiterate to VB semi-literate... next you'll be teaching chimps to talk :)
 
Comment #91  (Posted by an unknown user on 10/17/2006)
Rating
Way of explaination, use of terms, jargons, languages :-).

Up to the point, exact and accurate coverage of a solution to a specific and very common problem.
 
Comment #92  (Posted by an unknown user on 10/26/2006)
Rating
Good idea putting this in a module..
 
Comment #93  (Posted by an unknown user on 12/22/2006)
Rating
Exactly what I needed to know without having to dig through a lot of books.
 
Comment #94  (Posted by an unknown user on 03/13/2007)
Rating
Down to earth for us simple minded folks who are cutting our teeth on dotNet
 
Comment #95  (Posted by ruth on 04/13/2007)
Rating
hii... ur tutorial is very good..

i have a form wherein there is a close button when it is clicked it gives a confirmation yes or no.
if i click yes, the window is closed.
but if i click no i need to see the existing window , but its working same as yes button click ...
ours is a vb.net project 1.1

now my question how can we redirect to the same page with existing information when we click no..

can u help me...

thnq

 
Comment #96  (Posted by an unknown user on 05/04/2007)
Rating
The title of this article is very clear. It's all about accessing controls and data on the *STARTUP* form. So as you were looking for something completely different it's no big surprise that this article didn't have what you were looking for.
You have rated this article as Poor because it doesn't do something it never claimed to do.
Can I suggest that next time you spend a few seconds thinking about what an article is actually about before you decide that it is "Poor"?
 
Comment #97  (Posted by an unknown user on 05/08/2007)
Rating
Hi This is Santosh from Mumbai, India. Article is too good and it helps me lot for my project.
But Sir, I have a problem for you...
I am working on project where I have 10 responses in single form which need to be randomize. How i can do it? please explain me.
And another issue which i am facing is about database... I am looking for code which helps me to add record in single row from multiple forms. Please provide me the solution.

My email ID is santosh_kakade2004@yahoo.co.in

Thank you...
 
Comment #98  (Posted by an unknown user on 05/08/2007)
Rating
Hi This is Santosh from Mumbai, India. Article is too good and it helps me lot for my project.
But Sir, I have a problem for you...
I am working on project where I have 10 responses in single form which need to be randomize. How i can do it? please explain me.
And another issue which i am facing is about database... I am looking for code which helps me to add record in single row from multiple forms. Please provide me the solution.

My email ID is santosh_kakade2004@yahoo.co.in

Thank you...
 
Comment #99  (Posted by an unknown user on 05/16/2007)
Rating
i need a more explanatory and concise one. there should also be a picture of what is been explained
 
Comment #100  (Posted by an unknown user on 05/19/2007)
Rating
its great, well written - even me, a newbie from qbasic can get it
 
Comment #101  (Posted by an unknown user on 06/07/2007)
Rating
Well written and sooooooo helpful - THANKS for a great article.
 
Comment #102  (Posted by an unknown user on 06/12/2007)
Rating
This has been very helpful indeed. but just a question. is it possible to link a window form and a web form togther in Visual Basic?
 
Comment #103  (Posted by Ged on 06/12/2007)
Rating
You can insert a Web Browser control in a WinForms app and access the web that way.
But essentially they are two different technologies - Web apps and WinForms apps.
 
Comment #104  (Posted by an unknown user on 09/12/2007)
Rating
Excellent way of explaination with example and stick point to point.

Thanks

Satyendra singh

 
Comment #105  (Posted by an unknown user on 11/08/2007)
Rating
this article is great and easy to understand thanks to the author..

-newbies from phils.-
 
Comment #106  (Posted by an unknown user on 11/12/2007)
Rating
Great example! Really Helpful!
Great Job! =)
 
Comment #107  (Posted by an unknown user on 11/30/2007)
Rating
Hi
This is julian, am the software developer at Kyros Soft Tech Ltd, Hyderabad, India.
I rated this article 5 as all the 4 parts gives a complete basic idea of how to use multiple forms in vb.net, and i would like to suggest that by taking examples on data grid view also readers may understand the logic better.
warm regards
Julian

 
Comment #108  (Posted by an unknown user on 12/12/2007)
Rating
Well Done.

Clear and Concise
 
Comment #109  (Posted by an unknown user on 12/17/2007)
Rating
I'm a newbie and the explanation was clear and right on topic for my need.
 
Comment #110  (Posted by an unknown user on 01/01/2008)
Rating
so where do i sign up for the Ged Mead fan club?
 
Comment #111  (Posted by an unknown user on 01/06/2008)
Rating
good
 
Comment #112  (Posted by an unknown user on 01/07/2008)
Rating
what is mean by Orphaned??
it is not explained clearly So...
 
Comment #113  (Posted by Ged Mead on 01/07/2008)
Rating
>> so where do I sign up for the Ged Mead fan club? << Hmm, well with you and my wife, there's a membership of 2 ; I think that's about it though!
Thanks! It's comments like yours that encourage me to keep taking the time to create articles and courses .
Unlike the grading of 1 given by the last reader, just because he/she couldn't be bothered to look up a fairly common word on e.g. Dictionary.com :-{ Now *that's* the kind of comment that makes me wonder if it's really worth bothering!
 
Comment #114  (Posted by an unknown user on 01/25/2008)
Rating
This is so good for me and my project. Thank you so much.
 
Comment #115  (Posted by an unknown user on 01/28/2008)
Rating
This is most informative and clearest post I have seen. I have spent days searching through pages of complicated code trying to find a way to put a status bar on the parent of a MID application to keep the user informed while background processes are running on the child forms.
Thank you very much.
You are a gifted teacher.
 
Comment #116  (Posted by an unknown user on 02/01/2008)
Rating
Very good content. A bit too detailed even for beginners, I would say...
 
Comment #117  (Posted by an unknown user on 03/02/2008)
Rating
very informative.
 
Comment #118  (Posted by an unknown user on 04/18/2008)
Rating
This a very big help for us very new newbies.
 
Comment #119  (Posted by an unknown user on 04/24/2008)
Rating
this solved a problem i has having on a project spend ages looking 4 a solution n trying loads of ideas n this solved it in less than 10 mins
 
Comment #120  (Posted by an unknown user on 05/08/2008)
Rating
This answered a basic question that that we newbies encounter. The problem seems to be perpetuated by a popular SAMS book on VB.NET that has examples expecting you to use Form1.Name etc. but excludes the critical piece shown here.
 
Comment #121  (Posted by an unknown user on 05/21/2008)
Rating
This code saved me! I had to explain modules to a student and had only 15 minutes to make the code work. WHEW! THANK YOU for giving a concise, easy-to-follow example. YOU ROCK!
 
Comment #122  (Posted by an unknown user on 05/22/2008)
Rating
not accurate
 
Comment #123  (Posted by Ged on 05/22/2008)
Rating
I'm sorry that you didn't find it to be accurate (although it does look from previous comments that many people did :-} ). If you would like to contact me via the vbcity message system with your specific problem with this area and I would be more than happy to do my best to help you. egards. Ged Mead
 
Comment #124  (Posted by an unknown user on 06/12/2008)
Rating
ace!!

thats been bugging me all week!!
 
Comment #125  (Posted by an unknown user on 08/11/2008)
Rating
stupid answer
not working
 
Comment #126  (Posted by Ged Mead on 08/11/2008)
Rating
Hi, My response to you is the same as my previous one. Although nearly 100 readers have said that it works fine for them you are clearly having a problem with it. If you would like to post the details here (or better still in a post in the vbcity forums and notify me here that you've done so) I'll be more than happy to try and help you out.
 
Comment #127  (Posted by Brian on 08/12/2008)
Rating
Because of the MDI nature of the application, there would, in most circumstances, be only one instance of Form1 (which will be the main/global form). So why not just refer to Form1 directly in Form2 instead of creating a separate variable that references it?
 
Comment #128  (Posted by Ged on 08/12/2008)
Rating
Hi Brian,
Thanks for taking time to comment. Actually though, the example is for an SDI application, not MDI. That said, I take your point that in most cases there will only be one instance of a Form1 class. However - and here's the key to why the article was written in the way it was at the time - in earlier versions of VB.NET it wasn't possible to refer directly to Form1, Form2, etc by their names (as was possible in VB6 and as you're suggesting). Since VB2005 of course, the technique I used isn't needed because - after a lot of pounding on MS doors - the default instance of forms was reintroduced. Therefore it is now possible to refer to the first/default instance of a form by using the 'Form1' approach, as you suggest.
Hope this clarifies things.
 
Comment #129  (Posted by an unknown user on 08/12/2008)
Rating
I wasn't aware that this functionality was non-existent at one point -- thanks for the clarification.
 
Comment #130  (Posted by an unknown user on 11/16/2008)
Rating
I am a VB 6 programmer trying to learn VB .Net. I bought two books to help me and neither one explained this process. Thanks so much!
 
Comment #131  (Posted by an unknown user on 12/01/2008)
Rating
c4tlet
 
Comment #132  (Posted by an unknown user on 12/05/2008)
Rating
visual studio 9 in vb mode won't close the app if you dim new form2 Thank you for this info
 
Comment #133  (Posted by an unknown user on 12/16/2008)
Rating
JUST WHAT I NEEDED, THANX FOR MAKING IT CLEAR!!!!!!!!!!!!
 
Comment #134  (Posted by an unknown user on 01/28/2009)
Rating
Well written
Key points really emphasized well
Complexity in topic/tdchnique simplified
 
Comment #135  (Posted by an unknown user on 03/02/2009)
Rating
it would be more advantacge if used along with videos or diagrams or pic
 
Comment #136  (Posted by Andre Proulx on 04/12/2009)
Rating
Excellent, thank you I would like to submit you a problem that i have, readind your comments you say you are using Labelb and TextBox for your example and will work wih other controls what about DataGridView. I would like to read data from a Dgv on Form1 and write that data on another Dgv on Form2
I just don't know how to write the proper VB syntax
I can send you a zip file if it please you. Again many thanks for your good and apreciate work.
 
Comment #137  (Posted by an unknown user on 06/07/2009)
Rating
i want to know how to open another applicaation instead of form and click its button
 
Comment #138  (Posted by an unknown user on 06/07/2009)
Rating
i want to know how to open another application instead of form and click its button
 
Comment #139  (Posted by an unknown user on 06/07/2009)
Rating
What you need to use for this is Process.Start.
If you need more info, please post a question in the forums on www.vbcity.com, our main Q&A site.
Regards
Ged Mead
 
Comment #140  (Posted by an unknown user on 07/26/2009)
Rating
I've been scouring the internet and vb2008 help for a way to do this... I'm so delighted to finally find the answer! Thank you!
 
Comment #141  (Posted by an unknown user on 09/14/2009)
Rating
i thank this is a great article but how can i do this when i am receiving return data from form1 and need to pass it as a parameteer in a form_load in form 2 to use in a query
 
Comment #142  (Posted by an unknown user on 09/30/2009)
Rating
Good Day. I've never quite believed that one chance is all I get.
I am from Togo and bad know English, give true I wrote the following sentence: "Unibanco rather itau unibanco and citibank with a mastercard."

Best regards :-), Candide.
 
Comment #143  (Posted by an unknown user on 10/15/2009)
Rating
Hi Ged, Thank you for taking the time and effort in writing this - very easy to understand and easy to follow, and very helpful. Much appreciated.
Steve
 
Comment #144  (Posted by an unknown user on 11/02/2009)
Rating
This article detailed a solution that was exactly what I was seeking, in order to get around what appeared at first to be a complex problem. In fact, the issue is incredibly simple and this solution made that clear.

Thanks for posting this solution.
 
Comment #145  (Posted by an unknown user on 12/06/2010)
Rating
Thanks for posting this great article~~~ i use spire.xls for .net which is also a great vb.net component, and now, how to be better~~~
 
Comment #146  (Posted by Stephen on 12/06/2010)
Rating
Thanks for posting this great article~~ i use Spire.XLS for .NET, which is also a great VB.NET component~ now, i know how to be better~~
 
Comment #147  (Posted by VB2010_confused on 11/28/2011)
Rating
All these articles are great but I have a different problem. I created a two-player game in VB2010 EXPRESS and wanted to add two additional forms to display the graphics when either player won - a different form for each winner. The two forms display but the picture boxes do NOT display. Worse, BOTH Winner's forms display BEFORE the main, start-up, form! (I tried adding a module to load them correctly but VB 2010 EXPRESS does not allow the module to be the start up form!) I've tried everything. BUT nothing fixes either problem. What am I doing wrong? I am going from VB 4.0 to VB 2010 EXPRESS and it has NOT been an easy learning curve but I'm getting there. But, this I don't understand. Please help! Thank you.
 
Sponsored Links