Article Options
Recently Viewed
Premium Sponsor
Premium Sponsor

 »  Home  »  Upgrading  »  Multiple Forms in VB.NET. Part 1
 »  Home  »  Windows Development  »  Win Forms  »  Multiple Forms in VB.NET. Part 1
Multiple Forms in VB.NET. Part 1
by Ged Mead | Published  06/28/2003 | 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 1

Article source code: multipleforms.zip

_________________________________

 Authors' Note February 2006:  

     This article was written in June 2003 and was aimed at users of the VB.NET 2002 and 2003 editions who were upgrading from VB6.   Now that VB2005 has arrived, much of the content has been overtaken by improvements in that new edition and won't be applicable if this is the version you are using.  

________________________________

Introduction

One of the first hurdles you're going to come up against when you move from Classic VB to VB.NET is getting to grips with the new ways of dealing with forms.

Your approach to forms in VB.NET is fundamentally different and hinges on the fact that the Framework implements forms as classes, so now you need an object variable to instantiate it. This article is aimed at helping you through some of those basic steps.

Instead of loading and showing a form in the old way (eg. Form2.Show, what you have to do is create an instance of the form and you can then manipulate this form object.

Method 1: Show More Than One Instance of a Second Form

Here's one way. Let's assume that you have a project that contains a Form1 and a Form2. You have already set Form1 to be the startup form and it contains a button which, when clicked, will display an instance of Form2 to the user. Assuming this button is called ShowForm2Button, the code you need is as follows:

Private Sub ShowForm2Button_Click(ByVal sender As System.Object_
    ByVal e As System.EventArgsHandles ShowForm2Button.Click
    Dim F2 As New Form2()
    F2.Show()
End Sub

If you try this code out, you'll find that it works fine, as far as it goes. But you will also discover that it is possible to have several instances of Form2 displayed at the same time if you click the ShowForm2Button before you have closed down the current instance of Form2. Each click of the button does exactly what the code tells it to do - create a new instance of a Form2 Class object and show it.

Of course, this may not be what you need in your project. You may want to have only a single instance available at any one time.

This can be achieved in several ways. Each has advantages and disadvantages, and some may yield unexpected results to the OOP-unwary developer. The following way might be an acceptable fix for you in many situations:

Method 2: Show Second Form Modally

There is a potentially easy way round the problem. Show the second form modally (a concept you'll be familiar with from VB.OLD, I'm sure). The .NET syntax is:-

Private Sub ShowForm2Button_Click(ByVal sender As System.Object_
    ByVal e As System.EventArgsHandles ShowForm2Button.Click
    Dim F2 As New Form2()
    F2.ShowDialog(Me)
End Sub

Now, as you'd expect, the user can't get focus back to Form1 to click that pesky button again until they've closed down this instance of Form2.

However, there will probably be times when you want the best of both worlds, that is you want the user to only have one instance of your second form instantiated at any one time, but you want them to be able to see and use both Form1 and Form2 at the same time.

The solutions for showing forms in this way are easy. Where the problems start to kick in are that you may get side effects that are not so welcome if you don't take steps to deal with them.

Method 3: Allow Only One Instance of Second Form.

Type this into the main body of the form (i.e. outside any Subs, Events or Methods)

    Dim F2 As New Form2()

And use this code in the button's click event:

Private Sub ShowForm2Button_Click(ByVal sender As System.Object_
    ByVal e As System.EventArgsHandles ShowForm2Button.Click

    F2.Show()

End Sub

As promised, this will only show the single instance of Form2, because it isn't instantiated afresh every time you click the button. That's the up side.

The down side is that if you close Form2, then try and display it again by clicking on the button you will generate an exception.

The error message tells you why - you instantiated the Form2 object when Form1 was first created and you disposed of it when you closed it by clicking on the little x.

Trying to show it again will not succeed now because clicking on the button doesn't actually create another instance - it's simply trying to show a non-existent instance.

Let's find a couple of workarounds for this little glitch.

Method 4: Showing/Hiding a Second Form.

Instead of closing Form2, why don't we simply hide it? In many situations this may be an acceptable solution.

The code for Form1 is the same as for the above method. Your Form2 will need to be rigged so that it hides itself. Here's the easiest way:

  1. Change Form2's ControlBox Property to False. (This removes the ability of the user to close this form by using the little x).
  2. Add a button to Form2. Call this button FinishedButton.
  3. Use the following code in the button's click event:
Private Sub FinishedButton_Click(ByVal sender As System.Object_
    ByVal e As System.EventArgsHandles FinishedButton.Click
    Me.Hide()
End Sub

What we've done here is to create a situation where there is only one instance of Form2 and this instance is shown when the ShowForm2Button is clicked and hidden when the FinishedButton is clicked. The user is no wiser as to whether a form is closed or hidden.

So, does this solve all possible requirements? Of course not - this is programming; there's always another problematic scenario just around the corner!

Method 5: Showing/Hiding a Second Form (Alternative)

What if you need to allow the user to have access to the Minimize or Maximize buttons in the ControlBox? As far as I know, although you can disable them, you can't have just these two buttons visible without also showing the exit button.

My fix for this involves cutting out the middleman. If there has to be a ControlBox and it has to contain the little x, then let's short circuit that little x. Here's how:

Leave Form2 with it's ControlBox available and dispense with the FinishedButton. What we'll do is change the code that gets fired when the user clicks on that little x. This is the form's Closing event. Add this code to Form2.

Private Sub Form2_Closing(ByVal sender As Object_
    ByVal e As System.ComponentModel.CancelEventArgsHandles MyBase.Closing
    '  Bypass the instruction to Close this form
    e.Cancel = True
    '  But hide it from the user.
    Me.Hide()
End Sub

Be aware of a not so obvious knock-on effect here, though, if Form1 is not the startup form for your application. In this situation, when you close Form1 it will NOT automatically close Form2 (which is what always happens if Form1 is the startup form). So, something to keep in mind there - if you're happy to have Form2 still open once Form1 has closed, then that's fine; if not, then add this code to Form1's Closing event:

    If Not IsNothing(F2Or Not F2.IsDisposed Then F2.Close()

And it will take Form2 away with it when it closes.

Method 6: Check Current Status First

There is another way. Well, there's almost always another way, isn't there? This is based very closely on code provided by DevCity.NET member DrDave. It steps through a checklist of the possible states of the second form and takes the appropriate action depending on what it finds.

I've tried to make the steps as clear as possible with the commenting, but if you're like me you'll probably have to read it and try it a few times before it all clicks into place.

Here's the code:

Declaration outside of any methods:

    Private WithEvents F2 As Form2

And this code goes in the button's click event:

Private Sub ShowForm2Button_Click(ByVal sender As System.Object_
    ByVal e As System.EventArgsHandles ShowForm2Button.Click

'  If the instance still exists... (ie. it's Not Nothing)
    If Not IsNothing(F2Then
        '  and if it hasn't been disposed yet
        If Not F2.IsDisposed Then
            '  then it must already be instantiated - maybe it's
            '  minimized or hidden behind other forms ?
            F2.WindowState = FormWindowState.Normal  ' Optional
            F2.BringToFront()  '  Optional
        Else
            '  else it has already been disposed, so you can
            '  instantiate a new form and show it
            F2 = New Form2()
            F2.Show()
        End If
    Else
        '  else the form = nothing, so you can safely
        '  instantiate a new form and show it
        F2 = New Form2()
        F2.Show()
    End If
End Sub

Method 7: Flip-Flop Form1 and Form2

Another possible scenario might be where you need Form1 hidden while Form2 is on view, and for Form1 to be redisplayed when Form2 is closed.

This gets a bit more complicated, but only a bit. In the same way that we needed an object variable to reference our Form2s in the examples above, we will need a reference back to the instance of Form1 which we can use in Form2.

There are various ways of doing this, but we're going to overload the Form's constructor to achieve our aim. Less technically, this simply means that we'll create an alternative version of the 'New' Sub that all forms contain. This one will make a note of which form called it into existence.

Starting with Form1, add this code to the form:

Private Sub ShowForm2Button_Click(ByVal sender As System.Object_
    ByVal e As System.EventArgsHandles ShowForm2Button.Click
'  Note the "Me" in brackets.  Very important!
        Dim F2 As New Form2(Me)  ' Instantiate it
        F2.Show()   ' Show it
        Me.Hide()   ' Hide Form1
End Sub

Don't worry if you get a wiggly blue line error at this stage in Form1's code. The next bit of code in Form2 will resolve that. Form2 needs this code:

Add an additional Sub New:

Public Sub New(ByVal caller As Object)
    MyBase.New()
    InitializeComponent()
    '  Note which form has called this one
    CallingForm = caller
End Sub

Note that it doesn't replace the code already in the "Windows Form Designer generated code" region; this is an additional (overloaded) constructor.

Next, Form2 needs this declaration in the usual declarations area (ie. outside of any other code blocks) :

    Private CallingForm As Object

For this example, I have used the variation which bypasses the Closing event and simply hides Form2, but this is optional if it doesn't suit your purposes. The logic is the same if you do want Form2 to be closed, not merely hidden.

Private Sub Form2_Closing(ByVal sender As Object_
    ByVal e As System.ComponentModel.CancelEventArgsHandles MyBase.Closing
    e.Cancel = True
    Me.Hide()
    '  If the CallingForm still exists then show it now
    If Not Is Nothing(CallingFormThen CallingForm.Show()
    '  then dispose of it's reference here.
    CallingForm = Nothing
End Sub

(If you don't want to bypass the Closing event as shown above, then you can put a button on your Form2 and use the code above, but without the e.Cancel = True line.)

A happy side effect of using this approach is that it also gets round the problem of unwanted multiple instances of Form2. Obviously, because Form1 is hidden whenever Form2 is on display, the user can't get to the button to fire up another Form2 instance.

Summary

This article has only covered a sample cross-section of some of the ways you can open multiple forms. There are other techniques (MDI, 'Form within a form' and owned forms, for example) that haven't been included here. But hopefully there is enough information here for most purposes in the early days of your journey up that long and tiring .Net learning curve.

If you do want to see some working code for MDI or owned forms, then you can check out the attached solution. It includes all seven methods above, plus these two.

Of course, opening forms is only part of the story. You will also need a grasp of how to pass data between forms and also how to access one form's controls from another form. And finally you will also need to learn a few safe and trusted ways of closing down the forms you want closed, and leaving open those you still need available. Disposal of finished forms is an important part of good development technique. It's planned that these topics will be covered in later parts of this series.

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.46099290780146 out of 5
 423 people have rated this page
Article Score259844
Comments    Submit Comment

Comment #1  (Posted by George on 07/05/2003)

Great job, Ged! I had found method 6 in the forum here, but I didn't know that there were so many different ways. Maybe you could do a follow-up on this using more than two forms. The first time I did that with two forms wasn't easy, so I think with three - or even more forms the thing get quite complicated.

Thanks a lot for this article. It was about time someone knowledgelable wrote about that. Carry on!
 
Comment #2  (Posted by Prateek on 07/07/2003)

Good research !

Never realised that there could be so many possibilities and different requirements.


 
Comment #3  (Posted by Amintas on 07/10/2003)

Excellent article.
Very illustrative!
 
Comment #4  (Posted by Ged on 07/10/2003)

Thanks all for the supportive comments. I too was surprised to discover how many ways there seemed to be of doing more or less the same thing!

 
Comment #5  (Posted by Florin on 07/10/2003)

I have 3 forms: Form1 main form, Form2 opened modally (with ShowDialog) from Form1, Form3 opened modally (with ShowDialog)from Form2.
I have a button (button1) on Form3 to close the form (Form3). In button1_Click() event I have Me.Close() to return to Form2. Unfortunatelly Form2 is also closed and I'm returned to Form1.
Sometimes this works as expected and most of the times it's closing both "modal" forms.
 
Comment #6  (Posted by Ged on 07/10/2003)

I've tried replicating your code as you've described but, no matter how much I fiddled around with it, I couldn't make it fail in the way you are experiencing. Each subsequent form opened or closed as expected and clicking on Form3's Close button only ever closes Form2.

If you want to zip up your Project and email it me at xtab@vbcity.com, I'd be happy to take a look and see if I can find something in there that is causing your problem.

 
Comment #7  (Posted by Gidon on 07/10/2003)

Nice article Ged.
I always like to place my form variables into a seperate module. In this manner I can address any form from any other form.

1. In module:
Friend SetModeForm As frmSetMode

2. In calling form:
Private Sub menuSetMode(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem47.Click

Try
'We only want one instance of form.
'Make sure to assign form a value of nothing on closed event.
If IsNothing(SetModeForm) Then
SetModeForm = New frmSetMode
End If
SetModeForm.MdiParent = Me 'This example the callee is an MDI parent form.
SetModeForm.Show()
SetModeForm.Focus()

Catch ex As Exception
MessageBox.Show(ex.ToString, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
End
End Try
End Sub

3. In closed event of form that was called, assign a value of nothing to the form variable. We can't be sure that when we closed the form that it is destroyed on the heap (Garbage Collection only activate when short on memory):

Private Sub ClosedSetModeForm(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed

SetModeForm = Nothing
End Sub
 
Comment #8  (Posted by hitesh joshi on 07/11/2003)

it was a very helpful article, well documented and well written.
cheers!
 
Comment #9  (Posted by J Barber on 07/23/2003)

Great Article!! But I still can't swith between two forms. I put all the correct code and put the "calling form" method in a command button, but it just closes the form and doesn't reload form1. Any help would be great
 
Comment #10  (Posted by XTab on 07/23/2003)

If you zip up your project and email it to me xtab@vbcity.com I'll run it through the XTabulator and see if I can spot the glitch!
 
Comment #11  (Posted by angiras on 07/28/2003)

the example is very good for 2 forms , but what you will do (like 90% of cases) if you have a full application with ... let's say , 20 forms !?

in each form you have to write the 19 possibilities to close other forms ??? is there any way to close all other (unknown) form on load

and it was with buttons ....

what can you do if you want (like 90% of cases) a mainMenu in each form and have a common class for it ?

thanks
 
Comment #12  (Posted by XTab on 07/28/2003)

My first reaction was to advise you that the best policy (like 90% of cases) is to adapt the various code examples given so that they meet your particular needs. However, you may think me rude if I did.

In a forthcoming article, I plan to cover a way of keeping track of a Forms Collection which dynamically adds or removes Form items from its list. If you wis, you can contact me by email and I will send you a copy of the VS Solution which demonstrates this technique. (xtab@vbcity.com)

I'm not sure I understand what you mean by "in each form you have to write the 19 possibilities to close other forms ". Perhaps you could give more detail of the particular scenario that you are having problems with?

Finally, I don't really see how your question about Menus in Forms relates directly to what this particular article set out to achieve. Maybe you could clarify this also and if I can help, I will be happy to do so.

thanks


 
Comment #13  (Posted by Eagle Keeper on 07/30/2003)

Ged,

Fantastic Job, you don't know how timely it was. I am just starting out learning .NET (been using VS6 for years) and was stumped on multi forms. It was clear, concise and well done.

Hats off to you sir, I look forward to more articals from you.


Dale LeBlanc
 
Comment #14  (Posted by vijay kodati on 08/01/2003)

Hai Ged,
Thanks alot for such wonderful article. I have been searching for this info for couple days. Its really informative.
 
Comment #15  (Posted by Ged Mead on 08/02/2003)

Thanks for all the supportive comments. It's nice to know that many people have found the article useful (can't help wondering sometimes if you're talking to an empty room, so the reassurance is good to hear).

 
Comment #16  (Posted by M Hafner on 08/06/2003)

This article was just what I was looking for!
 
Comment #17  (Posted by chowdary on 08/10/2003)

Tanks lot for valuble information.......i never seen this type of explanation till now
 
Comment #18  (Posted by Neschae Fernando on 08/14/2003)

wow this has been very helpful to me. Thanx. I have a related question, so if i wanted to run an application with multiple forms on a remote computer (after installing the .Net framework) would i still just copy the executable to the remote computer? My question is whether the exe includes the code for all the multiple forms or do i have to compile them in a library. thank you
 
Comment #19  (Posted by Ged on 08/16/2003)

Hi, Yes, once you have the Framework installed on a system then the compiled .Net exe application will run on it. Of course, if you create a more complex project than the very simple examples used in these articles, then you may have to create a Setup package in order to deploy all the required files to another computer. Visual Studio has this type of project built in and you can put the main project and the setup project together in a single Solution. (You select 'Setup and deployment Projects' from the list of Project types).

There is no need to reinstall the Framework (or to include it with a deployment package) once it's been successfully installed on the receiving system.


 
Comment #20  (Posted by Tamer on 08/20/2003)

Good for beginners, but it lacks a real example that could take care of handling events for each control in each form.
 
Comment #21  (Posted by Vijay Balan on 08/25/2003)

I am using 2 forms, and when I build it says I need a sub Main with proper signature even though I have it.

No accessible 'Main' method with an appropriate signature was found in 'WindowsApplication3.Welcome_Form'.

Any tips ??

cheers
-v

 
Comment #22  (Posted by Greg on 08/25/2003)

I liked the article but what is wrong with this way:

This in Form1 Show Form2Button:

Private Sub ShowForm2Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ShowForm2Button.Click
' Note the "Me" in brackets. Very important!
Dim F2 As New Form2 ' Instantiate it
F2.Show() ' Show it
Me.Hide() ' Hide Form1
End Sub

And this in Form2 In a Close Button:

Private Sub cmdClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClose.Click
Dim F1 As New Form1
F1.Show()
Me.Close()

End Sub


I know it doesn't use the forms X but I couldn't get that to work. I keep getting an error in the If Not Is Nothing Statement. VB blueunderlines Is? Anyone can help me withthis?

Thanks,

Greg
 
Comment #23  (Posted by XTab on 08/26/2003)

Hi Greg, The answer to this is rather long, so I've posted it up in the Net Newbies Forum. The URL is http://www.vbcity.com/forums/topic.asp?tid=38283&#RID113135
and the Topic title is 'Multiple Forms Question'.

Let me know if this doesn't answer all your queries.

XTab

 
Comment #24  (Posted by XTab on 08/26/2003)

Hi Vijay,

Sorry, I don't see an immediate answer based on the info you've given. Suggest you post this up in .Net Newbies forum (with your project as an attachment) and it will be easier to pinpoint the problem area from that.

Cheers

Xtab
 
Comment #25  (Posted by Michael Lee on 09/22/2003)

I was just wondering how would you display two forms simultaneously. I mean I would like to show two forms when my application start up. One of the form would be a splash screen containing a bitmap or similar showing the name of the application and some other details. This form does not have a title bar/border (ie no min, no max or close buttons) and would display for a set period of time say 10 seconds and then close itself out. In parallel to all these, the main form with user interface with all the buttons and controls would also load up and stay visible until the user decided to close the application or take other actions.

Your help would be greatly appreciated.

Thanks
ML
 
Comment #26  (Posted by XTab on 09/23/2003)

Hello Michael,

There are various ways you can do this, but one of the easiest ways is to:
1. Use Sub Main as your project's Startup Object
2. Load and Show the Splash Form from Sub Main's code
3. Load and Show the Main Form from the Splash Form's Form_Load event.
This way your app won't close down on you when the Splash Screen closes ;-}

Regards

XTab
 
Comment #27  (Posted by Michael Lee on 09/23/2003)

XTab,

Thanks for your prompt reply.

However, I could not get your suggestion to work. This is what I did.

I have a Sub Main to load the splash screen using:

Sub Main()
frmSplash.ShowDialog()
End Sub

Then I use the form_load event for frmSplash to show and load the main form using:

Private Sub frmSplash_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
frmMain.ShowDialog()
End Sub

When I run the Sub Main procedure, it would show and load the main form only, and I only get to see the splash form when I close down the main form. The splash form only come up when the main is closed because I tried to minimise or move the main form away from the splash form, I could not see the splash form. I even made the splash form topmost property =true but did not help.

Do you know why is this?

Thanks
ML
 
Comment #28  (Posted by XTab on 09/24/2003)

Hi Michael

There's a good sample demo and description by VBCity member HotDog which might help you create your Splash/Main Form setup. You can find it at : http://www.vbcity.com/forums/topic.asp?tid=36074

If that doesn't solve your problem, let me know and I'll look further into it for you.

 
Comment #29  (Posted by Michael Lee on 09/25/2003)

Hi XTab,

Thanks for your help. I have incorporated that example into my code now and it is working fine except that when the frmSplash is shown and then closed instantly, the frmMain will not be displayed properly ie the full form is not displayed. I worked around this by setting the form visble property to false and then to true again in the load form event. Now it is working like a charm. Thanks for your help.

I have one more question for you. I hope you don't mind.

Could you point me in the right direction to any demos on writing codes to do animation, I mean real animation not just like moving gif files. I need to simulate the movements of traffic in a road network system. I was wondering how you could draw coloured boxes to represent the vehicles and also how is the animation of the vehicles moving could be accomplished in VB.Net. Is VB.Net the way to go?

Your help would be much appreciated.

Michael
 
Comment #30  (Posted by darc on 10/12/2003)

Hi,

I find that its not a good option to just hide a form, at least not always. Suppose you have an aplication with hundreds of forms, and every time you open and hide a form it remains in memory lowering your aplication's performance. I believe that creating a new instance of the form every time you need it is a better option at least most of the time. If you don't want for an error to occur because of a call to a none existing instance of a form, simply create a new instance of this form every time you wish to open it.

Example code:

Public Class Form1

Inherits System.Windows.Forms.Form

Public frm2 As Form2

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button1.Click

frm2 = New Form2
frm2.Show()

End Sub

End Class

Now, every time you click on the button a new instance of the form is created, thus a fresh instance of the form and no errors. Another tip is to declare the form variable public (e.i. Public frm2 As Form2) on a separate module so you can reference to it on other forms and keep things organized.

Anyways I think it was a very insightfull article.

Best Regards,
darc
 
Comment #31  (Posted by DianeB. on 10/13/2003)

How do I close Form1 when I am done running the solution. With the code you have given after I close Form2,
Form1 is still running and to quit the solution I have to go to Stop Debugging.
 
Comment #32  (Posted by xtab on 10/14/2003)


Depending on the particular needs of your project (and on which of the 7 Methods shown in the article you're using :-} ), I'm assuming that you're using one of the ways that hides Form1 from the user when Form2 is on view. To close the application from Form2 , you can do it by putting the following line in the Closing event of Form2:-

Private Sub Form2_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
Application.Exit()
End Sub

Hope this helps.

XTab
 
Comment #33  (Posted by Vinith on 10/20/2003)

Thanks a lot for this article,which is really helpfull for developers.We anticipate such type of articles in future.
 
Comment #34  (Posted by Gary George on 10/24/2003)

Thank you for creating a basic and clear tutorial for using multiple forms. I teach an introductory class at a local community college and used your tutorial instead of the textbook for this section. A couple of my students read both the book and your tutorial and were pleased to have had such a useful reference. Have you considered writing a textbook?
 
Comment #35  (Posted by XT on 10/29/2003)

Thanks for the positive comments - much appreciated. I don't know if I'm the person to write it, but I've thought for a while now that there should be a book for beginners in VB.Net called "Frustration Busters". There's a whole bunch of dotNet Gotchas out there (like the multiple forms ones) and it would be real nice to have them all dealt with in an easy to understand way, all in one book. Dates & Times as objects, Replacing substrings in Strings, Control Arrays (aka creating controls at runtime), just to name the first three that come into my head.


 
Comment #36  (Posted by James on 11/05/2003)

Great article, especially point 6. I need to be able to do all this but in a MDI forms. An article on MDI forms would be wonderfull.
 
Comment #37  (Posted by pallav on 11/18/2003)

The article is great for first time users...
great work
pallav
 
Comment #38  (Posted by square on 11/24/2003)

I m pretty new to VB.Net..
I want to post data from one form to another ...

On the OnClick event of a button ,
when I write
dim f2 as ....then the option of the form doesnt show up...iy only shows the option of webform.
Do i have to import some class/library or do something else...
I dont know where I m wrong...

Please let me know
Thanks !


 
Comment #39  (Posted by Karthik on 12/06/2003)

how can I validate the form..In the sense...chk for Null Values?
 
Comment #40  (Posted by Stumped on 01/28/2004)

Having trouble getting form2 to stay on top. On form1, I have a datagrid control. When I click on the row header, I want form 2 to popup. It pops up (form2.show) but immediately loses focus and form1 with the data grid regains the focus overlaying form2. Interestingly, if I run in debug, adding a breakpoint at the form2.show statement, then F8 my way through the rest of the sub, form2 stays on top. I'm stumped. Any tips?
 
Comment #41  (Posted by WarrenB on 01/29/2004)

Good Work GED, have just started VB.net and don't know why they still call it VB as the changes have been fairly major, but this article was super and covered every scenario I would have or will hit changing our legacy code. Thanks Again
WB
 
Comment #42  (Posted by Low Chee Meng on 02/10/2004)

I'm newbies to VB.net. I found another solution to this problems. At first, create a vb6 project with 2 forms and the 2 forms can call each others. Then open this vb6 project using vb.net, then the vb6 project will be converted to vb.net project as well as the source code. Go through those code, you will find a method to access multiple forms just like what vb6 does. ..........but it is a bit more complicated than above methods

I'm not recommending this method, i just post it out to share with you all. Hope any expert here can comment on this
 
Comment #43  (Posted by an unknown user on 02/25/2004)

Very helpful tutorial. Thank you.
 
Comment #44  (Posted by Ghufran Sajjad on 03/19/2004)

You make my life easier !!!
Thank u
 
Comment #45  (Posted by an unknown user on 05/10/2004)

This was a great help, im not sure if this is the correct place to post. I want to make variables available to different forms. Any help would be great, Thanks
 
Comment #46  (Posted by an unknown user on 05/10/2004)

Great artical...thanks a lot , very helpful ...
 
Comment #47  (Posted by Ashwin on 05/19/2004)

Excellent Article. Helped me fix all the issues I had with showing and hiding forms.


 
Comment #48  (Posted by Peter on 06/17/2004)

Absolutely Brilliant in its simple clarity!
 
Comment #49  (Posted by bo2k on 06/18/2004)

I noticed this code written below, but there's a much simpler way to do the same thing

[code=vb]
' If the instance still exists... (ie. it's Not Nothing)
If Not IsNothing(F2) Then
' and if it hasn't been disposed yet
If Not F2.IsDisposed Then
' then it must already be instantiated - maybe it's
' minimized or hidden behind other forms ?
F2.WindowState = FormWindowState.Normal ' Optional
F2.BringToFront() ' Optional
Else
' else it has already been disposed, so you can
' instantiate a new form and show it
F2 = New Form2()
F2.Show()
End If
Else
' else the form = nothing, so you can safely
' instantiate a new form and show it
F2 = New Form2()
F2.Show()
End If
End Sub
[/code]

I suggest:
[code=vb]
Try
F2.Show()
Catch
F2 = New Form2
F2.Show()
Finally
F2.Focus()
End Try
[/code]

Much easier :)
 
Comment #50  (Posted by prakash on 06/22/2004)

Tremendous
 
Comment #51  (Posted by Mike Nielstroem on 07/03/2004)

Very good article. Helped me to get immediately right with OOP. It makes also (not only) GUI programming very clear. Thanks
 
Comment #52  (Posted by Valentino on 07/20/2004)

I have a question. Can one load a Form into a Panel Control? I have a tabbed control with a panel control on each tab, and need to load a form into some of the panel controls. Does anyone know how to accomplish this?


Thanks,
Valentino

 
Comment #53  (Posted by Doug on 07/21/2004)

Hi, I just want to say that the person who made this article seems like he does not know English very well. Because his English seems like it is broken.
 
Comment #54  (Posted by Paul C on 07/22/2004)

Here ya go.. I'm a newbie to vb.net but this seems to work for me.. good luck.
Ged.. Thanks for article.. It's very good.


Public Class frmMain
Inherits System.Windows.Forms.Form
Dim FormsList As New ArrayList 'Used to hold what Forms are open and which are not.
Dim oForm2 As New Form2

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

If FormAlreadyOpen(oForm2.Name) = True Then
oForm2.Activate()
Else
oForm2.Show()
End If
End Sub

Private Function FormAlreadyOpen(ByVal formName As String) As Boolean
Dim bExists As Boolean
bExists = False

Dim nFoundAt As Integer

nFoundAt = FormsList.IndexOf(formName)
If nFoundAt < 0 Then
'Not Found
FormsList.Add(formName)
Else
bExists = True
End If
FormAlreadyOpen = bExists
End Function

End Class

 
Comment #55  (Posted by George Poth on 07/26/2004)

Doh, or whatever be you names, you is right, English are difficults. But, you, too have also done goofed big time in substituting “write” for “make”. Howsomeever, for this egregious undersight you can’t hardly blame us benighted editorialists as we just make it out of a hobby. Natheless, you have printed out your shocked-and-dismayed epistle which I will forthwith fashion into a wet noodle with which to beat the scrivener upon the pate. Enuff wormwood and gull is there also for others, including a brace of editors who have signed-off on this masterpiece.
 
Comment #56  (Posted by jason kid on 07/28/2004)

good work! My code looks simular to this one. I wish i would have found this link sooner. I wasted an entire night, and an entire 2 liter of Mountain Dew on this problem! I'm having a tough time finding code examples for VB.NET.
 
Comment #57  (Posted by Nidhi on 08/24/2004)

I am creating an application using multiple forms. Your Article helped me a lot. Good Work.
 
Comment #58  (Posted by Jimmy on 09/16/2004)

thats a good article. it solved my prob. i am slipping in the learning curve to .net . cheers
 
Comment #59  (Posted by Marnie Parker on 09/23/2004)

Fantastic article! All four articles (1-4)! I spent big bucks buying books on VB.Net and did countless searching on the web until I found this series of articles. EXACTLY what I was trying to figure out -- how to acess controls and input on one form from another form. How to access one form's datagrid and data components from another form and from elsewhere (re modules). You would think all those books I bought would have told me. Nope. Maybe it's buried in those books (most highly recommended) somewhere under layers of obtuse language. But I never found it. Wish I had found these articles sooner. :-( Oh, well.

Concise articles that explain very clearly how to do something that seemed very complicated, but once explained, turns out to be quite easy.

Thanks! My extreme VB.Net frustration is now over. And I don't know why I couldn't find this type of simple explanation elsewhere. Great job!
 
Comment #60  (Posted by John Sørensen on 09/24/2004)

Hi,
I find your articals vere interesting and at the moment I am only missing one situation.
How do you accomplish to change focus between a main form and a subform owned by the main form? The subform is not modal. 'Alt'+'TAB' is not achiving this.
 
Comment #61  (Posted by an unknown user on 10/20/2004)

it still does'nt work! this is crap i hate VB.NET!

"Source code"
Private Sub ShowForm2Button_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles ShowForm2Button.Click
' Note the "Me" in brackets. Very important!
Dim F2 As New Form2(Me) ' Instantiate it
F2.Show() ' Show it
Me.Hide() ' Hide Form1
End Sub
"End of Source code"
 
Comment #62  (Posted by XTab on 10/21/2004)

LOL. As the saying goes - "Two out of three aint bad"
It may be crap and you may hate VB,Net, (There's the two) but actually it really does work if you follow the instructions . Just to be sure, this morning I used the article instructions to create a brand new project and ran it. It worked fine for me,
However here are some details you might like to check - if you haven't already thrown your machine out of the window, that is :-}
1. Basics
Did you remember to put the " Private CallingForm As Object" declaration at the top of Form2.
The article implies that you have a button on Form1 named "ShowForm2Button". (Of course, you don't need to stick to this; you could use any event you like on Form1to fire up the code).
Did you add the second Sub New to Form2.
2. Other possible problems
If you have Option Strict On, then you will get errors when you try to compile and run. (I wrote that article when I was very much a newbie myself and I have to admit that I hadn't covered that angle.)
If you do have Option Strict On, then change the appropriate lines so that the generic Objects are declared as Forms: I have capitalized the important changes)
Public Sub New(ByVal caller As FORM)
MyBase.New()
InitializeComponent()
' Note which form has called this one
CallingForm = caller
End Sub
and
Private CallingForm As FORM
3. Minor typo.
I see that in the final code block of the article it has been printed as
If Not IS NOTHING(CallingForm) Then CallingForm.Show()
- again I've put the important bit in capitals for you.
There should NOT be a space between IS and NOTHING.
Therefore it should be
If Not ISNOTHING(CallingForm) Then CallingForm.Show()
4. If all else fails .... once you've got your sense of humour back, why don't you email me at xtab@vbcity.com with details of where you get the errors and what the exception or compilation error messages say. I will be happy to email you a copy of a working example of this and/or help you out with any ongoing problems you have.
I know, it's frustrating. Nobody said it would be easy and in my experience over the past two years it hardly ever is . But - trust me - if you stick at it, it really does eventually get easier :-}
Best wishes
XTab



 
Comment #63  (Posted by Donna on 11/19/2004)

This series is outstanding. Excellent. Unbelievable. If only I had teachers like you. I can't thank you enough!!!!
 
Comment #64  (Posted by Yash on 11/21/2004)

I got to say ... this is awsome coding anou've done an awsome job explaining it . I was stuck on my project for a good 2 days and without you I would have been at the same spot for another 2 days
 
Comment #65  (Posted by Ernesto on 12/07/2004)

Hi, i need to close multiple windows opened from a menu in a mdi form. I can open several windows, and work with each one of them, but there's a option menu 'close all windows' that i don't know how to make it work.

Thank you.
 
Comment #66  (Posted by an unknown user on 12/29/2004)
Rating
It got to the point. however, one item not covered (which is what I am after) is...how do you open a second form from the first and then close form1. If you have set the reference to Form2 in form1 then does this reference not get distroyed when Form1 is closed and therefore close form2 also? You may have covered it above and if so I apologise - I am quite thick and am used very much with VB6.Thanks,adrianrpollock@hotmail.com
 
Comment #67  (Posted by XTab on 01/04/2005)

Hi Adrian, As I understand it, closing the startup form of a VB.Net application will automatically close the running application. I have tried experimenting with using Application.Run() from a Module and a Class, but that particular law seems pretty inflexible. Part 4 of the "Multiple Forms" series deals with some ways of referencing back to the startup form, but the examples there also require that Form1 is left running, whether visible or not. If I misunderstood your question, please don't hesitate to post a follow up comment. Ged
 
Comment #68  (Posted by an unknown user on 01/07/2005)
Rating
THis is the only place i could find that explained how to flip-flop between two forms (Option 7). Thank you.
 
Comment #69  (Posted by an unknown user on 01/09/2005)
Rating
This article was just what I was looking for!
 
Comment #70  (Posted by an unknown user on 01/16/2005)
Rating
Brilliant. He should write a book. The explanations are a hundred times better than anything else I have found. My degree has been saved!
 
Comment #71  (Posted by an unknown user on 01/29/2005)
Rating
builds step-by-step; very clear
 
Comment #72  (Posted by an unknown user on 02/19/2005)
Rating
In example 7 where is the Me in brackets?
 
Comment #73  (Posted by an unknown user on 02/28/2005)
Rating
Nicely done! By far the best instructional FAQ I have seen on the Net in hours spent searching on Google. However, I am curious as to why an article this in depth on multiple form handling did not include examples on MDI parent/child instances. Oh well, off to Google again!
 
Comment #74  (Posted by an unknown user on 03/02/2005)
Rating
This is what I've been looking on the Internet and many ebooks for an entire week! Thanks a lot!
 
Comment #75  (Posted by an unknown user on 03/08/2005)
Rating
Good Job
 
Comment #76  (Posted by an unknown user on 03/11/2005)
Rating
All the answers here, not just one. Excellent work!
 
Comment #77  (Posted by an unknown user on 03/15/2005)
Rating
I have a library in my house of programming books up to VB6. I have NEVER seen or read a book so far that explains code as nicely as this group of examples does. Thank you!
 
Comment #78  (Posted by an unknown user on 03/21/2005)
Rating
Clear and simple.
 
Comment #79  (Posted by an unknown user on 03/26/2005)
Rating
Ged- you are the best
 
Comment #80  (Posted by an unknown user on 04/01/2005)
Rating
it's really helpful. 100% running good with this code except this thing: IsNothing instead of Is Nothing in if statement.
by the way do you know how to display one form at a time in vb.net? thank you
 
Comment #81  (Posted by Ged Mead on 04/01/2005)
Rating
If you want only to have one form on display at a time then you can hide the first form with Me.Hide at the same time as you use .Show for a second one. Check out the article in Part 4 for situations where the first form is the Project's StartUp form as this is a more difficult scenario.
Don't quite understand what you mean about "Is Nothing". It wasn't a typo - IsNothing is a Function available in VB.Net so it's correct to use it without a space between Is and Nothing.
 
Comment #82  (Posted by an unknown user on 04/06/2005)
Rating
This showed me exactly how to toggle back and forth between forms with no problems. Thanks!!
 
Comment #83  (Posted by an unknown user on 04/15/2005)
Rating
Excellent; this article helped me to figure out the question (problem) I had. I will book mark this website. Thank you. :)
 
Comment #84  (Posted by an unknown user on 04/26/2005)
Rating
Great article loved the way you mentioned the gotcha's we programmers have grown to expect. I liked reading them instead of crashing into them! Good job!
 
Comment #85  (Posted by an unknown user on 04/28/2005)
Rating
I were having problems with multiple forms that i wanted one to hide when i'm using another one and thne get back to previous one. i consulted msdn and some books but the problem didnt go away. then i searched the internet and found couple of sites but that didn't do any good. then i came across ur website and u have noooooooo idea how happy i am that my work is done in a snap. keep up the good work you are the best. :) bye
 
Comment #86  (Posted by vreezman on 05/12/2005)
Rating
i'v read this tutorial of form, and it's just work in 2 form (form2 + frmMain), it's work fine.. but what happen if i work with 50 form, this method is difficult to implemented... anyone can give another solution ...

[code=vb]

Private WithEvents F2 As Form2


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' If the instance still exists... (ie. it's Not Nothing)
If Not IsNothing(F2) Then
' and if it hasn't been disposed yet
If Not F2.IsDisposed Then
' then it must already be instantiated - maybe it's
' minimized or hidden behind other forms ?
F2.WindowState = FormWindowState.Normal ' Optional
F2.BringToFront() ' Optional
Else
' else it has already been disposed, so you can
' instantiate a new form and show it
F2 = New Form2()
F2.Show()
End If
Else
' else the form = nothing, so you can safely
' instantiate a new form and show it
F2 = New Form2()
F2.Show()
End If

If IsNothing(F2) Then MessageBox.Show("Is Nothing")
If F2.IsDisposed Then MessageBox.Show("Is Disposed")


End Sub

Private Sub frmMain_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
If Not IsNothing(F2) Then
If Not F2.IsDisposed Then
F2.Close()
End If
End If
End Sub
[/code]
 
Comment #87  (Posted by an unknown user on 05/17/2005)
Rating
very helpful
 
Comment #88  (Posted by an unknown user on 06/05/2005)
Rating
As a migrator to .Net I found it very useful to give a fresh insight into what looks like a whole new scarey world
 
Comment #89  (Posted by J Crossman on 06/08/2005)
Rating
My app starts off with a Sub Main which creates the first form, then F1.showdialog() and waits for F1 to close. When the first form does the me.hide(), after showing a second form, the sub Main program picks up as if F1 had closed. Any solutions?
 
Comment #90  (Posted by an unknown user on 06/12/2005)
Rating
But, how can the calling form know when the Form2 is closed? I want a process to react to the closing of the form2 in the calling form.
 
Comment #91  (Posted by an unknown user on 07/06/2005)
Rating
Great articles for someone moving over from VB6. Cleared up a lot of issues for me
 
Comment #92  (Posted by an unknown user on 07/26/2005)
Rating
This is Excellent.
 
Comment #93  (Posted by an unknown user on 08/15/2005)
Rating
very usefull
 
Comment #94  (Posted by an unknown user on 08/16/2005)
Rating
It's giving me info I need to transit from .old to .NET.
 
Comment #95  (Posted by an unknown user on 08/22/2005)
Rating
Just clear to follow cannot be any simpler
 
Comment #96  (Posted by an unknown user on 08/27/2005)
Rating
As a beginner googled it, came here, and got my answer.
 
Comment #97  (Posted by dumba on 08/31/2005)
Rating
i dont know if u can help me with the code of my project. i want to run a check in the textbox when the command button is click at the same time if its true then it goes and load the next form automatically

 
Comment #98  (Posted by an unknown user on 08/31/2005)
Rating
solved my problem with 2 lines of code
 
Comment #99  (Posted by an unknown user on 09/07/2005)
Rating
Just awesome. The explanation for the basic principle of passing variable from form to form is so clear and easily understood. I am a newbie and have not problem understanding it. Great Work and Thank you.

Cheers,
Lennie
 
Comment #100  (Posted by an unknown user on 09/08/2005)
Rating
It's Good
 
Comment #101  (Posted by Dave on 09/21/2005)
Rating
Great article! I'd been having trouble finding a simple way to keep all my forms under one umbrella, and this worked perfectly.
 
Comment #102  (Posted by an unknown user on 09/21/2005)
Rating
Yes! Finally someone EXPLAINS it ALL. So many articles attempt to convey this info but fail to tell you what you don't already know. You assume we know NOTHING and consequently you wrote the best article available on the subject. Good Job! Please keep up your articulate and clear explainations!
 
Comment #103  (Posted by jim on 09/28/2005)
Rating
nicely done !
 
Comment #104  (Posted by an unknown user on 10/07/2005)
Rating
NIce
 
Comment #105  (Posted by an unknown user on 10/10/2005)
Rating
clarity
 
Comment #106  (Posted by an unknown user on 10/11/2005)
Rating
Great job, Ged! I had found method 6 in the forum here, but I didn't know that there were so many different ways. Maybe you could do a follow-up on this using more than two forms. The first time I did that with two forms wasn't easy, so I think with three - or even more forms the thing get quite complicated.

 
Comment #107  (Posted by an unknown user on 10/16/2005)
Rating
Clear simple
 
Comment #108  (Posted by an unknown user on 10/16/2005)
Rating
Because I found it useful for controlling forms in my project
 
Comment #109  (Posted by an unknown user on 10/19/2005)
Rating
Very informative. I was directed here from experts-exchange.
 
Comment #110  (Posted by an unknown user on 10/27/2005)
Rating
Excellent work. very useful.
 
Comment #111  (Posted by an unknown user on 10/27/2005)
Rating
Very good information, for somebody who is new to .net
 
Comment #112  (Posted by an unknown user on 10/29/2005)
Rating
nice work dude
 
Comment #113  (Posted by ralphie on 11/01/2005)
Rating
Good article.

This would be the ultimate solution - - communicate with the forms by reference. When a form is being created, you can create a reference to another form. Then you can used the reference to communicate with the original instance of the other form. This gives us the fexibility of good old VB6, and no worry about duplicated forms. I've managed to do this by reference, but haven't figured out how to make the reference a public variable so that all forms can be accessed by all other forms and modules......

Any ideas?
 
Comment #114  (Posted by ralphie on 11/01/2005)
Rating
Hey, I just found the ultimate solution:
http://visualbasic.about.com/od/usingvbnet/l/aa080703a.htm
This creates a Public Class to communicate to all forms through reference. Just a few lines of code, and you'll be communicating between forms just like in VB6.
 
Comment #115  (Posted by an unknown user on 11/09/2005)
Rating
Found the new Property "IsDisposed" which is not there in the drop down list of .NET instance help.
 
Comment #116  (Posted by an unknown user on 11/10/2005)
Rating
It gives all options
 
Comment #117  (Posted by John on 11/13/2005)
Rating
This is all good stuff but here is my situation. On form1 I have a click event on a listbox that goes to a "history list" which launches a .CGM graphic when clicked. Everytime I click on the history list listox which is, we will say (form2) to launch my graphic, I still have lingering forms. Also, the listbox gets cleared everytime form1 gets displayed again. I want to retain the information in the history list's listbox. Anybody have any suggestions drop me a line at the_grove_man@yahoo.com
 
Comment #118  (Posted by an unknown user on 11/17/2005)
Rating
solved my error
 
Comment #119  (Posted by an unknown user on 12/07/2005)
Rating
A direct answer to the exact issues I was having
 
Comment #120  (Posted by an unknown user on 12/26/2005)
Rating
in easy steps to use
 
Comment #121  (Posted by an unknown user on 01/03/2006)
Rating
Thank you for such a great article, it helped me out a lot.
 
Comment #122  (Posted by an unknown user on 01/03/2006)
Rating
the best!
 
Comment #123  (Posted by an unknown user on 01/15/2006)
Rating
Mgnific way to explain things, thanks
 
Comment #124  (Posted by David Luu on 01/16/2006)
Rating
This is an informative article, but more for VB.NET developers. Would it be too much to ask for a C# version of some of the code? For me the code in question is primarily the code to override the default form close function of the x button. I haven't tested the VB.NET code given on this page but I've tried converting it to C# and it doesn't seem to work. Maybe I'm missing something in my conversion to C#:

private void Form2_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
e.Cancel = true;
this.Hide();
}

it disposes the form instead of hiding it.
 
Comment #125  (Posted by an unknown user on 01/24/2006)
Rating
Fantastic article - wish I'd come across it sooner, but still valuable to see the solutions side by side with clear explanations. Thanks!
 
Comment #126  (Posted by an unknown user on 02/01/2006)
Rating
very helpfull !!! Thanks...
 
Comment #127  (Posted by an unknown user on 03/02/2006)
Rating
because it has help to find some possible solution to my problem in working with multiple forms as a young programmer
 
Comment #128  (Posted by an unknown user on 03/02/2006)
Rating
because it has help to find some possible solution to my problem in working with multiple forms as a young programmer
 
Comment #129  (Posted by an unknown user on 03/05/2006)
Rating
its small but very informationable for me......author has really don a marvalous job
 
Comment #130  (Posted by an unknown user on 03/06/2006)
Rating
any one can understand all four articles. and good explaination to young programmer
 
Comment #131  (Posted by an unknown user on 03/10/2006)
Rating
Clear and succinct information with a good flow through the possibilities
 
Comment #132  (Posted by an unknown user on 03/13/2006)
Rating
Genious
 
Comment #133  (Posted by SP on 03/15/2006)
Rating
Nice article Ged, I am a first time visitor to your site, I am impressed the way you went about it. Thank you. But what I am trying to do is lil bit different than what was shown here. I am lookig to enter a number, say, 4 in a text box in Form1 and when I click the "Submit" button on Form1 it should instantiate the Form2 4 times and likewise. I want that Form2 to be reiterated. Also at one time I should be seeing only one form and should be able to navigate myself with Back and Next buttons on each form. Any thoughts? Could you please help? Can anyone help me please?
 
Comment #134  (Posted by an unknown user on 03/15/2006)
Rating
1st 2 methods good but last dosnt work as it is explained, needs a bit more explanation to where things go fot the more novice user. see vb.net for dummies for help on this
 
Comment #135  (Posted by an unknown user on 03/15/2006)
Rating
How to show only one instance of a base form, dialog...? Please help!
 
Comment #136  (Posted by an unknown user on 03/23/2006)
Rating
Ged gives very precise explanations along with well commented code. Also, his explanations of what is going on is fairly straight forward and easy to understand.
 
Comment #137  (Posted by an unknown user on 03/24/2006)
Rating
I'm learning stage in VB.NET but It is very usefull for my study thanks By Sasi from Bangalore
 
Comment #138  (Posted by an unknown user on 03/29/2006)
Rating
simple examples, great uses! Thanks- naresh
 
Comment #139  (Posted by an unknown user on 03/29/2006)
Rating
Thanks, i was looking for some simple answers to these questions and here it is, outstanding had weeks wondering about some little things.
 
Comment #140  (Posted by an unknown user on 03/31/2006)
Rating
Certainly clears up some qs for us newbies!!
 
Comment #141  (Posted by an unknown user on 04/07/2006)
Rating
Great job , It Really helped with my assigment!!
dmytro
 
Comment #142  (Posted by an unknown user on 04/10/2006)
Rating
very nice
 
Comment #143  (Posted by an unknown user on 04/27/2006)
Rating
This is good but i want how to control multiple instance of MDI child form within the MDI Parent form. please give me solutions fast(in c#).
my mail id is ankur_add@sify.com
 
Comment #144  (Posted by an unknown user on 05/12/2006)
Rating
just great
 
Comment #145  (Posted by an unknown user on 05/16/2006)
Rating
Been struggling with this for a while and your Method 6: Check Current Status First solved my problem. Did the vb 2005 change tis approach.
 
Comment #146  (Posted by an unknown user on 05/16/2006)
Rating
>>Did the vb 2005 change tis approach?
Yes, VB2005 has reinstated the Default Instance approach for forms (i.e. the way you can access new forms in VB6). So you can now view a new form by using code such as:-
Form1.Show
that is, without having to create the variable yourself, e.g.:
Dim F1 as New Form1
F1.Show
Of course you still can do this and in some cases it is better to do so.
But you have the choice now
 
Comment #147  (Posted by an unknown user on 05/17/2006)
Rating
Been struggling with this for a while and your Method 6: Check Current Status First solved my problem. Did the vb 2005 change tis approach.
 
Comment #148  (Posted by an unknown user on 05/22/2006)
Rating
The code is simple and working fine.
 
Comment #149  (Posted by an unknown user on 05/22/2006)
Rating
The explenation is very clear
 
Comment #150  (Posted by an unknown user on 05/25/2006)
Rating
Solved all my doubts about form manipulation. Very well explained with practical examples, and workarrounds on errors.
 
Comment #151  (Posted by an unknown user on 05/29/2006)
Rating
I would like to modify bit the 6 point in the following way.. may be its more easy..

Private F2 As New Form2

And this code goes in the button's click event:

Private Sub ShowForm2Button_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles ShowForm2Button.Click

If F2.IsDisposed Then
F2 = New Form2()
F2.Show()
End If

F2.Show

End Sub
 
Comment #152  (Posted by an unknown user on 06/02/2006)
Rating
Thanks for the article -- it helped me alot.
 
Comment #153  (Posted by an unknown user on 06/29/2006)
Rating
This is an Excellent... from two months i was looking for this solution and finally i got the soultion here in the form of devcity. i never seen this type of explanation till now .it was a very helpful article, well documented and well written. Thanks alot for such wonderful article.

 
Comment #154  (Posted by an unknown user on 07/30/2006)
Rating
Really on the money address the various situation a developer encounters during navigation between forms and provides thorought solutions!
 
Comment #155  (Posted by an unknown user on 08/02/2006)
Rating
Very helpful to a newcomer in vb.net :-)
 
Comment #156  (Posted by an unknown user on 08/29/2006)
Rating
Thanks alot for help! AvAzbeiGi
 
Comment #157  (Posted by an unknown user on 09/06/2006)
Rating
Just great. All the pains of a programmer and pitfalls of a program neatly addressed. Thanks a lot.
Avinash Karandikar
avinash_karandikar@yahoo.com
 
Comment #158  (Posted by an unknown user on 09/11/2006)
Rating
Very useful and effective tutorial. Thank you.
 
Comment #159  (Posted by an unknown user on 09/20/2006)
Rating
Thanks a lot. This article is very useful.
 
Comment #160  (Posted by an unknown user on 09/30/2006)
Rating
The only form of help available to me. I am a university student attempting to complete my end-of-semester project. Thanks!
 
Comment #161  (Posted by an unknown user on 10/01/2006)
Rating
Private Sub Button1_Click...
My.Forms.Form2.ShowDialog(Me)
End Sub
 
Comment #162  (Posted by Ged Mead on 10/02/2006)
Rating
Re the last comment. I usually let negative comments pass because we're all entitled to our opinions, aren't we?
But as the low rating and the comment are based on an alternative that won't actually work then I feel it needs a reply.
If the commenter had taken the ten seconds needed to read the Author's Note which has been visible in great big letters at the top of this article since February 2006 he or she would have seen that it says quite clearly: "This article was written in June 2003 and was aimed at users of the VB.NET 2002 and 2003 editions who were upgrading from VB6. Now that VB2005 has arrived, much of the content has been overtaken by improvements in that new edition and won't be applicable if this is the version you are using. "
And the "solution" you have posted above applies to ....... Oh Duh! VB2005 only. It will not compile in VB2003 or earlier versions.

 
Comment #163  (Posted by an unknown user on 10/02/2006)
Rating
easy to understand, used layman's language to expalain things which cud get some people confused.
 
Comment #164  (Posted by an unknown user on 10/10/2006)
Rating
It took you throught the steps that you go through in attemping this task, the possible errors and solutions
 
Comment #165  (Posted by an unknown user on 10/11/2006)
Rating
Many people knows how to solve a problem, fewer knows how to teach.
Thanks,
José
jfranco.neto@terra.com.br
 
Comment #166  (Posted by an unknown user on 10/18/2006)
Rating
I'm an old VB 6 guy and this really cleared up how VB.NET handles forms. Even better than the book I bought.
 
Comment #167  (Posted by an unknown user on 10/20/2006)
Rating
This is a great help! i have a question i try to have a 4 forms but the other 2 didn't run. is it only applicable for 2 forms?
 
Comment #168  (Posted by an unknown user on 10/20/2006)
Rating
Hi, The same techniques will work more than just two forms. You just need to create separate instances of each form and then manipulatethem as shown above. If you continue to have problems with this, the best thing would be to post details in a Topic in the .NET Newbies forum here on the VBCity website.
 
Comment #169  (Posted by an unknown user on 10/23/2006)
Rating
I've been searching for the answer to this for over six hours. I've read dozens of articles on using multiple windows forms and this is the first article that actually dealt with the entire problem from start to finish and explained it in good English with clear and consise language (both prose and programming code). Given how fundamental this is (using more than one windows form without having hundreds of copies of the second form) you'd have thought it would be easy to find a solution, but Ged's is the only one I've found that makes full and comprehensive sense - excellent work!
 
Comment #170  (Posted by Ged Mead on 10/23/2006)
Rating
Thanks for the comment. I had to smile as I read it through - the reason being that your post made me remember why I set myself the task of writing that article in the first place. I was just so frustrated that a fundamental task just didn't seem to get a mention or explanation in any of the books I'd bought for VB.NET. So I know exactly what you mean!
A couple of years down the line, VB 2005 has made the job easier with things like default instances of forms and the My.Application class, but I atill think it's probably worth understanding the most basic "hand-built" ways covered in the article.
 
Comment #171  (Posted by an unknown user on 10/25/2006)
Rating
some examples dont seem to work correctly. Or i got errors due to the author not specifying how instatiations are supposed to be done: Dim f2 as form2 ?? was form2 previously instantiated or is this just the name of the form. If you would like to tell me please write to vadik.vargas@yahoo.com
 
Comment #172  (Posted by Ged Mead on 10/25/2006)
Rating
I am fairly sure that none of the examples use the syntax you are quoting in your comment:
"Dim F2 as Form2".
Every example in the article instantiates a New form in one way or another. In the majority of cases this is by using "Dim F2 as New Form2"
or similar syntax.
So I am really not sure which particular examples you have found not to work. (Judging by the previous 171 comments, it does seem that most people seem not to have had this problem).
Perhaps you could point to those specific ones that are causing you trouble?


 
Comment #173  (Posted by an unknown user on 10/25/2006)
Rating
Excellent, direct and to the point.
 
Comment #174  (Posted by an unknown user on 11/02/2006)
Rating
Told me the basic uses of the form
 
Comment #175  (Posted by an unknown user on 11/08/2006)
Rating
because it is not a good aproch that u hide a from and misuse the memory
 
Comment #176  (Posted by mcc on 11/08/2006)
Rating
The other 6 alternatives were Ok with your critical assessment then? :-}
 
Comment #177  (Posted by an unknown user on 11/10/2006)
Rating
I found everything I was looking for in this article.
 
Comment #178  (Posted by an unknown user on 11/12/2006)
Rating
I wasted so much time in msdn looking for answers to this very problem.... Thank you for clearing this up:)
 
Comment #179  (Posted by Ken on 11/12/2006)
Rating
Well finally I have found how to open and use a second form in my project. I was also impressed that it worked with Option Explicit On and Option Strict On. Most articles I read that claim to be solutions to diferent subjects in VB.Net have those options off. They use late binding and such and will never work.

Thank you for taking the time to post this article
 
Comment #180  (Posted by an unknown user on 11/17/2006)
Rating
Thanks so much for your thorough and very informative article. However, I do have a follow-up question for you regarding this “form within a form” or “forms inside a form” area:

o If I place multiple forms within a control (say a TabControl) on a main or parent form, is there any way to have the active sub-form highlighted. The [default] ability of the .NET 2.0 framework to highlight the currently active Windows Sub-Form does not work when I set “TopLevel = false”, which is necessary in order to get the sub-form appear at all.

By the way, I’m using VS 2005 and C# (sorry all you VB’ers out there).

I’m fairly new at this .NET stuff, and have been struggling with this most frustrating problem for well over a week now. :-(

Thanks in advance.

Regards,

Darwin



 
Comment #181  (Posted by an unknown user on 11/23/2006)
Rating
Thanks that was Great.
I got answers to all my questions in this Article
Keep it Up!!!
Thsnks a lot.
Regards Aalok (India)
 
Comment #182  (Posted by an unknown user on 11/24/2006)
Rating
really great
 
Comment #183  (Posted by NagaT on 11/28/2006)
Rating
Is useful for me.
 
Comment #184  (Posted by an unknown user on 12/25/2006)
Rating
i can use it for my work
 
Comment #185  (Posted by an unknown user on 12/28/2006)
Rating
Hello Ged,
Great work. Ur article helped me alot to slove my prob. Thanks a lot.

Lakshitha from Sri Lanka
 
Comment #186  (Posted by an unknown user on 01/05/2007)
Rating
Perfect! I love you!
 
Comment #187  (Posted by an unknown user on 01/05/2007)
Rating
Perfect! I love you!
 
Comment #188  (Posted by an unknown user on 01/06/2007)
Rating
Great Job!
 
Comment #189  (Posted by an unknown user on 01/09/2007)
Rating
this is pretty good and simple example.however, its quite tedious to handle large No. of forms.
 
Comment #190  (Posted by an unknown user on 01/29/2007)
Rating
It was exactally what I needed plus it gave me more options of what I might try and do. Thank you!!!
 
Comment #191  (Posted by an unknown user on 02/01/2007)
Rating
Clear, concise, newbie friendly - excellent job, Ged.
How do you reference "wanted, multiple instances of Form2" (VB2005) - I can get their handles but can't figure out how to use them ? (Sorry, for the simple Q. - probably missing something obvious). Thanks, Rob
 
Comment #192  (Posted by an unknown user on 02/08/2007)
Rating
simply ... good
 
Comment #193  (Posted by Craig Hilles on 02/08/2007)
Rating
This gave me the clue to solve my problem.

But an easier way to pass the form to the next form. Instead of having an overloaded constructor passing the form, you can just pass in "showdialog", then in form2 you can reference the owner by using me.owner:

dim f as new frmNewForm
me.hide
f.showdialog(me)

In frmNewform_closing you can have:
me.owner.visible = true


 
Comment #194  (Posted by an unknown user on 02/19/2007)
Rating
complex
 
Comment #195  (Posted by an unknown user on 03/02/2007)
Rating
Explained the 'problem' exactly as I had found it and gave the solution in way I could understand. Thank you
 
Comment #196  (Posted by an unknown user on 03/06/2007)
Rating
Great stuff Ged. "Very illustrative" Love It
 
Comment #197  (Posted by an unknown user on 03/12/2007)
Rating
Very good, easy to understand.
 
Comment #198  (Posted by an unknown user on 03/16/2007)
Rating
I have so Confuse but you are solve my problem
Thanks
 
Comment #199  (Posted by an unknown user on 04/01/2007)
Rating
thank you man, im searched this for two days, its very helpful
have good days
 
Comment #200  (Posted by an unknown user on 04/03/2007)
Rating
1. Straight to the point.
2. Multiple, good examples.
3. Smart links to pertinent articles on the developing company's website.
 
Comment #201  (Posted by an unknown user on 05/15/2007)
Rating
helped me :-)
 
Comment #202  (Posted by an unknown user on 06/07/2007)
Rating
Method 3: Allow Only One Instance of Second Form. - you can also put this code within a try...catch.
eg
Private Sub BShowSc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BShowSc.Click
Try
If myform Is Nothing Then
myform = New Form2
myform.myCaller = Me
End If
myform.Show()
Catch ex As Exception
myform = New Form2
myform.myCaller = Me
myform.Show()
End Try

End Sub
 
Comment #203  (Posted by phubducked on 06/13/2007)
Rating
Visit www.Phubducked.com to hear phubducked stories, see phubducked pictures and view phubducked videos.

Phubducked isnt that whay you want to be.
 
Comment #204  (Posted by an unknown user on 06/21/2007)
Rating
i like the e.cancel
 
Comment #205  (Posted by an unknown user on 07/02/2007)
Rating
as this gives me lots of technique to do this
 
Comment #206  (Posted by an unknown user on 07/06/2007)
Rating
Thank you for clearing this subject for me
 
Comment #207  (Posted by an unknown user on 07/17/2007)
Rating
Excellent Krab!!

 
Comment #208  (Posted by an unknown user on 07/17/2007)
Rating
Excellent Krab!!

 
Comment #209  (Posted by an unknown user on 08/07/2007)
Rating
Comming from VB 6, I have to get used to VB.NET. This article helped me with understanding how forms react. Very useful !!
 
Comment #210  (Posted by an unknown user on 08/08/2007)
Rating
This article is very helpful!
 
Comment #211  (Posted by an unknown user on 09/17/2007)
Rating
Thanks
 
Comment #212  (Posted by an unknown user on 10/01/2007)
Rating
ver bad information.........
 
Comment #213  (Posted by an unknown user on 10/21/2007)
Rating
usefull

maksud.
 
Comment #214  (Posted by an unknown user on 11/13/2007)
Rating
Excellent article. Very straight forward and simple to unerstand
 
Comment #215  (Posted by an unknown user on 11/19/2007)
Rating
Good
 
Comment #216  (Posted by an unknown user on 11/21/2007)
Rating
I not only got solution for my problem, i even got more information...!!
 
Comment #217  (Posted by an unknown user on 11/26/2007)
Rating
Plenty of options to choose from.
 
Comment #218  (Posted by an unknown user on 12/18/2007)
Rating
Jhakaaasss !! (meaning Too good)
 
Comment #219  (Posted by an unknown user on 01/14/2008)
Rating
very helpful, easy to understand. thank you.
 
Comment #220  (Posted by an unknown user on 01/16/2008)
Rating
I searched for this a long time.
 
Comment #221  (Posted by an unknown user on 01/18/2008)
Rating
Talk about a gift that keeps on giving...it's 2008 and 2003 INCREDIBLY clear examples helped me get two forms simultaneously up and running.

Love the samples side by side in the zip file...boy, that helps. Usually I do that to understand what I'm doing, but felt luxurious to just say "Hey, that's the one" and start being able to use it AND understand it immediately.

Thanks again for taking the time -- and giving us your knowledge. That loch view must really be inspiring!
 
Comment #222  (Posted by an unknown user on 01/18/2008)
Rating
Talk about a gift that keeps on giving...it's 2008 and 2003 INCREDIBLY clear examples helped me get two forms simultaneously up and running.

Love the samples side by side in the zip file...boy, that helps. Usually I do that to understand what I'm doing, but felt luxurious to just say "Hey, that's the one" and start being able to use it AND understand it immediately.

Thanks again for taking the time -- and giving us your knowledge. That loch view must really be inspiring!
 
Comment #223  (Posted by an unknown user on 01/27/2008)
Rating
Contained all the info I needed in a concise and easily understood format.
 
Comment #224  (Posted by an unknown user on 02/19/2008)
Rating
youre good!
 
Comment #225  (Posted by an unknown user on 02/28/2008)
Rating
code for vb for login in vb 6.0

 
Comment #226  (Posted by an unknown user on 02/29/2008)
Rating
really, one of the best-written articles I've ever seen; probably out of thousands.

on point, understandable, useful, excellent.
 
Comment #227  (Posted by an unknown user on 03/15/2008)
Rating
Thangs , very inspirative. i hore it wil help me iv managsd forms
 
Comment #228  (Posted by an unknown user on 04/02/2008)
Rating
Clear and simple for beginners
 
Comment #229  (Posted by an unknown user on 04/03/2008)
Rating
Its very very Informative
 
Comment #230  (Posted by an unknown user on 04/17/2008)
Rating
The explanation was perfect
 
Comment #231  (Posted by an unknown user on 05/19/2008)
Rating
eku
 
Comment #232  (Posted by an unknown user on 05/27/2008)
Rating
Well written and very informative.
 
Comment #233  (Posted by an unknown user on 05/29/2008)
Rating
how do you make the loading & switching or forms faster? is there something like double buffering for form load?
 
Comment #234  (Posted by an unknown user on 06/05/2008)
Rating
Very detailed and helped greatly!! Also gave me an idea how to tackle another similar problem with success!
Thanks much for writing this article!!!
 
Comment #235  (Posted by an unknown user on 06/07/2008)
Rating
u8tsginyy3qe ep0c5ruuz nvy1le695sl
 
Comment #236  (Posted by an unknown user on 06/08/2008)
Rating
Really is a great job.
It help me a lot and very appreciated it.
Thanks again!!
 
Comment #237  (Posted by an unknown user on 06/16/2008)
Rating
really good
 
Comment #238  (Posted by an unknown user on 07/29/2008)
Rating
Very clear and concise. This was quite a hurdle for me when upgrading from VB6. Many thanks!
 
Comment #239  (Posted by an unknown user on 09/10/2008)
Rating
I want to close the login form & open the main form.
 
Comment #240  (Posted by an unknown user on 09/12/2008)
Rating
if you look at the FAQs on vbcity you will see one by HotDog that covers this
 
Comment #241  (Posted by an unknown user on 09/28/2008)
Rating
good
 
Comment #242  (Posted by an unknown user on 11/17/2008)
Rating
it help me a lot. I was searching this solution from many days.
 
Comment #243  (Posted by an unknown user on 11/22/2008)
Rating
Good work helps me lot
 
Comment #244  (Posted by an unknown user on 11/27/2008)
Rating
Great article. It's so different from the VB6 way.
 
Comment #245  (Posted by an unknown user on 12/01/2008)
Rating
Great explanation!!!
 
Comment #246  (Posted by an unknown user on 12/03/2008)
Rating
coc4tcolir
 
Comment #247  (Posted by an unknown user on 01/14/2009)
Rating
Solid info - I needed method 6
Thanks
 
Comment #248  (Posted by an unknown user on 01/18/2009)
Rating
this is very useful
 
Comment #249  (Posted by an unknown user on 01/26/2009)
Rating
exactly what I needed
 
Comment #250  (Posted by an unknown user on 03/04/2009)
Rating
i had wrote the same one for the datagridview in two forms but it doesn't work every time in big project..
please solve it for me..
thank u.
 
Comment #251  (Posted by an unknown user on 03/29/2009)
Rating
It show a number of examples that demonstrate various methods that might be applied give user requirements
 
Comment #252  (Posted by an unknown user on 04/02/2009)
Rating
Thorough... Been browsing the web for material on VB, has not been easy to find articles geared towards making this transition (VB6 to VS2005).
 
Comment #253  (Posted by an unknown user on 04/24/2009)
Rating
generally good. specific + / - points:
+ good balance between "why" and "how-to"

- lacks link(s) to more advanced (MDI, owned) topics mentoned
 
Comment #254  (Posted by an unknown user on 04/28/2009)
Rating
Thank you very much for writing this article, it has the answers to my questions about creating multiple forms.
 
Comment #255  (Posted by an unknown user on 05/13/2009)
Rating
Thanks for all the usfull information provided.
 
Comment #256  (Posted by an unknown user on 07/05/2009)
Rating
Helped me solve my problem
 
Comment #257  (Posted by an unknown user on 07/29/2009)
Rating
very good article.the codes are easy to understand
 
Comment #258  (Posted by an unknown user on 08/14/2009)
Rating
U R the light! thanks man! U saved my life! xD this was the easiest to understand of all articles in the web, thanks for not forgeting the noobs :D
 
Comment #259  (Posted by an unknown user on 08/25/2009)
Rating
Very user friendly with brief step by step solutions that are easy to follow and master. ndatshymoyo@yahoo.com

 
Comment #260  (Posted by an unknown user on 10/05/2009)
Rating
very good article
 
Comment #261  (Posted by an unknown user on 10/06/2009)
Rating
first of al thanks.i want both form1 and form2 both are run separately without any link at the same time
 
Comment #262  (Posted by an unknown user on 10/13/2009)
Rating
easy to understand. I had no problem with it all all.
 
Comment #263  (Posted by an unknown user on 11/05/2009)
Rating
Succinct and to the point
 
Comment #264  (Posted by an unknown user on 12/15/2009)
Rating
what if press The X button to close the form? it would still dispose the form and the glitch will start to appear again.
 
Comment #265  (Posted by an unknown user on 12/21/2009)
Rating
awesome information
 
Comment #266  (Posted by an unknown user on 12/29/2009)
Rating
You didnt tell me how to add buttons or text to the new form, you just taught how to open or hide it. Thanks for helping me out anyway the info was really helpful.
 
Comment #267  (Posted by sgafar on 06/19/2010)
Rating
Another method, you can combine between method 2 and method 3 and it work fine.
Dim F2 As New Form2() //outside any Subs.
F2.ShowDialog(Me) // inside Subs
No any exception will occur sepcialy like that occurs in method 3.
 
Comment #268  (Posted by oem software on 02/12/2012)
Rating
gRjnCz Is anybody strong in radio here? We need a colleague who would tell us briefly about the transistor T2. I hope there are radio amateurs here. If it`s not on the subject at all, then I`m sorry. I have to write because I have no choice. PS: if the spelling is not right then also I'm sorry, I'm just 13 years old!....
 
Comment #269  (Posted by Above Ground Pools on 02/24/2012)
Rating
Stupid article..!
 
Comment #270  (Posted by Tori Richard on 02/24/2012)
Rating
Current blog, fresh information, I read it from time to time!!...
 
Comment #271  (Posted by Theme Wedding Invitations on 02/24/2012)
Rating
Strange but true. Your resource is expensive. At least it could be sold for good money on its auction!...
 
Comment #272  (Posted by Mobile Marketing Software on 02/24/2012)
Rating
Heartfelt thanks..!
 
Comment #273  (Posted by Spinach Salad on 02/24/2012)
Rating
Author, Shoot yourself a knee..!
 
Comment #274  (Posted by Bean Salad on 02/24/2012)
Rating
Informative, but not convincing. Something is missing but what I can not understand. But I will say frankly: bright and benevolent thoughts!...
 
Comment #275  (Posted by Buy Cheap OEM Software on 03/07/2012)
Rating
O6051c Thanks again for the article.Thanks Again. Great.
 
Comment #276  (Posted by Bristol Airport Hotels on 04/18/2012)
Rating
YwdqF3 Thank you ever so for you article post.Really thank you! Keep writing.
 
Sponsored Links