Results 1 to 3 of 3

Thread: Switching main forms

  1. #1

    Thread Starter
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Switching main forms

    Here is a simple method to allow you to start an app with one form and then close that form and open another without exiting the app. I have implemented it with two forms here but you could have as many as you like. This is what would have been termed a dialogue-based application in times gone by I think. You would make the module the startup object for the project:
    VB Code:
    1. Module Module1
    2.  
    3.     Public mainForm As Form 'The current primary form.
    4.  
    5.     Sub Main()
    6.         'Start the app with a new instance of Form1
    7.         mainForm = New Form1
    8.  
    9.         'Keep showing the current main form until there isn't one.
    10.         Do
    11.             Application.Run(mainForm)
    12.         Loop While Not mainForm Is Nothing
    13.     End Sub
    14.  
    15. End Module
    16.  
    17. Public Class Form1
    18.  
    19.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    20.         'Open a new main form and close this one.
    21.         mainForm = New Form2
    22.         Me.Close()
    23.     End Sub
    24.  
    25.     Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
    26.         If mainForm Is Me Then
    27.             'This is the main form so let the app exit.
    28.             mainForm = Nothing
    29.         End If
    30.     End Sub
    31.  
    32. End Class
    33.  
    34. Public Class Form2
    35.  
    36.     Private Sub Form2_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
    37.         If mainForm Is Me Then
    38.             'This is the main form so let the app exit.
    39.             mainForm = Nothing
    40.         End If
    41.     End Sub
    42.  
    43. End Class
    Note that you cannot use this method to switch the main form to one that is already open because the call to Application.Run will try to display it. This will cause an exception to be thrown if the form is already displayed.

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Switching main forms

    John, what about
    VB Code:
    1. Module Module1
    2.     Public Sub main()
    3.         Application.Run(New Form1)
    4.         Application.Run(New Form2)
    5.     End Sub
    6. End Module

  3. #3

    Thread Starter
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Switching main forms

    Quote Originally Posted by kleinma
    John, what about
    VB Code:
    1. Module Module1
    2.     Public Sub main()
    3.         Application.Run(New Form1)
    4.         Application.Run(New Form2)
    5.     End Sub
    6. End Module
    That would work fine but it means that you're stuck with showing just those two forms in that order. I created the code above to give you the freedom to show whatever form you like and as many or as few forms as you like. If you know exactly what forms you want to show and in what order then code like yours is certainly preferable.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width