|
-
Apr 19th, 2013, 09:32 AM
#4
Re: Program Wont Close
 Originally Posted by rpmaurer
I have a program starting with FormA, and opens to Forms B, C, D, E, etc... When I close forms B, C, D, E, it doesn't close the program. So I added this tidbit of code:
Code:
Private Sub AnalysisMain_Closing(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.FormClosing
Me.Close()
End Sub
This just causes the program to freeze.
Im sure theres a simple solution here
Ryan
The very first thing I see is that you're calling the form's Close method in the FormClosing event, the funny thing about this is that the only way the FormClosing event fires is if the Close method is called, when your form is already closing there's no need to call its close method.
That being said, I *think* I understand what you're wanting to do, so correct me if I'm wrong.
I think you have two forms open: the "base" form (the one that when closed will end the program) and a secondary form, of which you want to end the program if this form is closed, correct?
If the above is correct, then this should work nicely for you:
vb Code:
Public Class Form1 Private WithEvents frm2 As New Form2 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click frm2.Show(Me) End Sub Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing If frm2 IsNot Nothing Then frm2.Dispose() End If End Sub Private Sub Frm2_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles frm2.FormClosed Me.Close() End Sub End Class
 Originally Posted by .paul.
Code:
Private Sub AnalysisMain_Closing(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.FormClosing
Application.Exit
End Sub
You should avoid calling 'End' and 'Application.Exit' in applications as that causes a hard-stop of your app. None of the Closing and cleanup events are fired and it doesn't allow your apps forms to clean up the objects on them (fonts, labels, textboxes, things in the form's dispose event).
 Originally Posted by MSDN
CAUTION The Form.Closed and Form.Closing events are not raised when the Application.Exit method is called to exit your application. If you have validation code in either of these events that must be executed, you should call the Form.Close method for each open form individually before calling the Exit method.
Last edited by JuggaloBrotha; Apr 19th, 2013 at 10:30 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|