Code Library A repository of sample code

Go Back  Xtreme .NET Talk > Knowledge Base > Code Library > Closing the Main Form , without Closing the Application.


 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #1  
Old 05-06-2004, 04:26 PM
dynamic_sysop's Avatar
dynamic_sysop dynamic_sysop is offline
Senior Contributor

Preferred language:
C#
 
Join Date: Oct 2002
Location: Ashby, Leicestershire.
Posts: 1,039
dynamic_sysop is on a distinguished road
Wink Closing the Main Form , without Closing the Application.

Hi guys n gals , i have knocked up a little bit of code which may provide usefull.
i guess most people are aware that if you try closing your Main Form, while trying to leave a second Form open ( ie: Form2.Show , Form1.Close ) , you end up with the Application closing completely. well not anymore
in a Module ( with a Sub Main set as the Start-up object ) ...
Code:
'/// add a module to your application , then set it's Sub Main as your start-up object
Imports System.Windows.Forms

Module Module1
    Sub main()
        Dim frmMain As New Form1
        frmMain.Show()
        Application.Run() '/// NOTE : i run the application without a start-up Main form.
    End Sub
End Module
in Form1 ( which will load as your default form in this instance ) ...
Code:
Private closeApp As Boolean = True

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    closeapp = False
    Dim frm2 As New Form2
    frm2.Show()
    Me.Close()
End Sub
Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
    If closeApp Then
        Application.Exit()
    End If
End Sub
Form2 will now show and Form1 will close without the Application exiting , in form2 , to close the app i put a simple ...
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Application.Exit()
End Sub
included is a sample source.
Attached Files
File Type: zip Project1.zip (4.7 KB, 45 views)

Last edited by PlausiblyDamp; 07-07-2007 at 04:42 AM. Reason: Fixed colour mark up
Reply With Quote
 

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Closing an Application Jay1b General 11 05-07-2004 09:20 AM
Closing my windows.form application andycharger Windows Forms 3 03-26-2004 08:07 AM
Closing my application andycharger Windows Forms 2 06-30-2003 07:25 AM
just closing the main form yaniv Windows Forms 10 05-22-2003 02:04 PM
Closing the entire application from a form torisch Windows Forms 6 04-09-2003 12:40 AM

Advertisement:

Powered by liquidweb