Results 1 to 6 of 6

Thread: Program Wont Close

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2012
    Posts
    73

    Program Wont Close

    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

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,381

    Re: Program Wont Close

    Well something's obviously causing the program not to close. What you should do is first take the Me.Close out of your FormClosing. Next, set up a breakpoint at your form closing just to make sure your form is indeed trying to close. Some info that may be useful: A)How are you closing the program?(Clicking the red x or in code?) B)What do you expect to happen when you close a form?(Just close that single form or the whole application?) C)Is there perhaps some long running process going on that we don't know about?
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,419

    Re: Program Wont Close

    you have 5 forms. when exactly should the app. exit?
    when all forms are closed, or something different?

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Oct 2012
    Posts
    73

    Re: Program Wont Close

    A) Pressing red X
    B) Close the whole application
    C) Not on this application, I have one on another form, but it isnt running in this case. Ill tackle that problem when I get there

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,419

    Re: Program Wont Close

    Code:
    Private Sub AnalysisMain_Closing(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.FormClosing
        Application.Exit
    End Sub

  6. #6
    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