Results 1 to 6 of 6

Thread: Program Wont Close

Threaded View

  1. #4
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: Program Wont Close

    Quote Originally Posted by rpmaurer View Post
    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:
    1. Public Class Form1
    2.  
    3.     Private WithEvents frm2 As New Form2
    4.  
    5.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    6.         frm2.Show(Me)
    7.     End Sub
    8.  
    9.     Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    10.         If frm2 IsNot Nothing Then
    11.             frm2.Dispose()
    12.         End If
    13.     End Sub
    14.  
    15.     Private Sub Frm2_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles frm2.FormClosed
    16.         Me.Close()
    17.     End Sub
    18.  
    19. End Class

    Quote Originally Posted by .paul. View Post
    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).
    Quote 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.
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

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