As you state Hiding is NOT closing. I have two ways of doing what you ask and both require you to change your project properties on the application tab. On the menu click Project and then click on the last item in the list which will be [Your Projects name]Properties.
This first method changes when your application will shutdown the default is "When Startup Form Closes" you will change that to "When Last Form Closes". Make this change in the Shutdown mode listbox. Then place the following code in your project. For this demo you need two forms (form1 & form2) in your project.
Public Class Form1
Private doneOnce As Boolean
Private Sub Form1_FormClosing(ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.FormClosingEventArgs) _
Handles MyBase.FormClosing
If e.CloseReason = CloseReason.UserClosing Then
If Not doneOnce Then
Dim frm As New Form2
doneOnce = True
Form2.Show()
End If
End If
End Sub
End Class
That is it.... The formClosingevent handles any attempt to close the form. Use the closeReason to screen why the form is being closed so that say a windows shutdown will be allowed to close without spawning another window.
The key to this method is opening a new form2 BEFORE allowing form1 to close. Once the formClosing event completes the form1 is allowed to close.
FYI If you want to provide an are you shure you want to close prompt you can use this event and the e.cancel property to abort the close by setting e.cancel = true.
Because of the changes made to the project properties the application will continue to run until the last form is closed in this case that is the new form2 that you just started up
Next method is a little more complicated but can provide you with many more options as to how your program starts. It uses a module with a Sub Main() proceedure as the start up point for your program. Again this requires changes to the program properties and this time we are goint to point the Sub Main() routine as the startup point for the program.
On the Application tab UNcheck the "Enable Application Framework" Then in the Startup Object drop down list select the module which has the Public Sub Main defined. The example below uses default names (form1, form2, module1)
Place the following code in module1:
Module Module1
Public Sub main()
Dim itm As String
If My.Application.CommandLineArgs.Count > 0 Then
itm = My.Application.CommandLineArgs.First
Else
itm = ""
End If
If itm = "form2" Then
Application.Run(Form2)
Else
Application.Run(Form1)
End If
End Sub
End Module
Place the following code in form1:
Public Class Form1
Private Sub Form1_FormClosing(ByVal sender As Object, _
ByVal e As System.Windows.Forms.FormClosingEventArgs) _
Handles Me.FormClosing
If e.CloseReason = CloseReason.UserClosing Then
Dim myPath As String = My.Application.Info.DirectoryPath
Dim myName As String = My.Application.Info.AssemblyName
Dim fPath As String = myPath & "\" & myName & ".exe form2"
Shell(fPath, AppWinStyle.NormalFocus, False)
End If
End Sub
End Class
This version works by using command line arguments which are strings that placed after the .exe.
This is similar to the way DOS command let you add options to a command. When the main sub routine is called VB looks to see if any command line arguments are present and if so read it and tests if it is "form2" if so form2 is opened other wise form1 is opened.
This method starts a new instance of the application by using the shell command so the advantage here is allowing the entire application to close with the first form and starting fresh with a new application the stars up the second form.
The form1 closing event constructs a path and file name with a command line argument that will signal the new instance of the application to use form2
Public Class Form1
Private Sub Form1_FormClosing(ByVal sender As Object, _
ByVal e As System.Windows.Forms.FormClosingEventArgs) _
Handles Me.FormClosing
If e.CloseReason = CloseReason.UserClosing Then
Dim myPath As String = My.Application.Info.DirectoryPath
Dim myName As String = My.Application.Info.AssemblyName
Dim fPath As String = myPath & "\" & myName & ".exe form2"
Shell(fPath, AppWinStyle.NormalFocus, False)
End If
End Sub
End Class