How to display a form from another one

Hi all,
I am developping an application in VB .Net 2003 with 4 forms. I have crated a main form with some menus. One of those menus should show another form. The problem is that when i referred to the name of this form, i can't acces the "show" method. The only way i found is to create a new instance of my form in the main form code but each time i use the show methos a new window is displayed.

If anyone could help :-(

PS: I think i missed something somewhere.


Answer this question

How to display a form from another one

  • lorijean44

    You're going to have to instantiate the form.  Show() is not a static method, and therefore it requires an instance of the form to call it.

    There are two ways to do this:

    1.) Create one instance of each form in the main form, and just call it's Show() method when needed.  This is good if you only ever need one instance of the form -- you'll get the same form every time, so any changes you make to it will be reflected on it.

    2.) Create a new instance of each form in the appropriate menu event.  Good for when you need a fresh copy of the window every time.

    Here are examples for both:

    Method #1:

    Public Class MyForm
       Inherits System.Windows.Forms.Form
       
       Dim first as MyFirstForm
       Dim second as MySecondForm
       Dim third as MyThirdForm
       Dim fourth as MyFourthForm

       Public Sub New()
          MyBase.New()
          first = new MyFirstForm()
          second = new MySecondForm()
          third = new MyThirdForm()
          fourt = new MyFouthForm() 
       End Sub

       ' In each menu's Click event, you'd just call the appropriate form's Show() method.
       ' Here is an example for the first form.
       Private Sub MenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem1.Click
          first.Show()
       End Sub
    End Class


    Method #2:

    Public Class MyForm
       Inherits System.Windows.Forms.Form

       Public Sub New()
          MyBase.New()
       End Sub

       ' In each menu's Click event, you'd instantiate the appropriate form and call the 
       ' appropriate form's Show() method.  Here is an example for the first form.
       Private Sub MenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem1.Click
          Dim f as new MyFirstForm()
          f.Show()
       End Sub
    End Class


    Hope this helps.  Let me know if I missed anything.

    -Ari

  • Chad Bumstead

    You're welcome. :)  The problem here is that when you close the form, the system disposes of it, so the form is gone.  The way to get around this is to handle the closing event to hide the form and then cancel the event, thus preventing the rest of the closing process from happening.

    Before I add some example code, there are a few caveats I need to add:
    1.) This only works with method #1 -- you must have a persistant instance of the form hanging around.  If you instantiate the form every time you need it, this becomes a moot point since the form variable will go out of scope at the end of the event call and the form will disappear anyway.

    2.) Don't handle the closing event on your main form -- if you use this code on your main form, your application will never exit by clicking on the X.  If you do use this code, you're going to need a button or menu item somewhere that will exit the application.

    Ok, an example.  I'm only adding the bare minimums necessary for the class, so things like InitializeComponent and whatnot are assumed to be there:

    Public Class MyClass
       Inherits System.Windows.Forms.Form

       ' Your normal class stuff goes here

       Dim f as MyLoginForm

       Public Sub New()
          MyBase.New()
          f = new MyLoginForm()
       End Sub

       Public Sub Login()
          f.Show()
       End Sub
    End Class

    Public Class MyLoginForm
       Inherits System.Windows.Forms.Form

       ' Normal class stuff, including constructor, goes here

       Private Sub MyLoginForm_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
          Me.Hide()
          e.Cancel = True
       End Sub
    End Class


    That should do it.  Hope this helps.

    -Ari

  • David Pinero

    Thank you for this for this very clear example. This time when i use your example my form appears. Now the problem is that when i close the form (using the close on the top right of the form) and i want to display it again with the show command, i receive an error message telling me that it's impossible to access my form because it has been supressed. Those it mean that when you use the standard close button of a form it doesn't "hide" it but supresses the instance of that form

    So my next question will be, how is it possible to close a form without delting the instance.

    Anyway thank you very much for the help ;-)

  • How to display a form from another one