none
how to close parent form when child form is closed?

    Question

  • Hi everyone!

    I am using Visual Studio 2005. What I am trying to achieve is the following:

    I have a login form (form1) for the user. On successful login, a new form (form2) pops up which allows the user to add details of a student (name, age, address, courses taken, etc).

    Following is what I tried to achieve the above said:

    1) File>New>Project>VB Windows Application ................for form1
    2) Project>Add Windows Form>Windows Form ...............for form2

    What is happening is when i close form2, the application is not getting closed, form1 still exists. What i wish is that when I close any form (form1 or form2 or form3 etc ), the entire application must close.

    Please, can anyone help me with this?

    Sunday, May 16, 2010 6:53 PM

Answers

  •  

    If you want Form1, Form2 and Form3 to stop the application when ithe form is closed,

    Put on Form1, form2 and Form3 this code

      Private Sub Form_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Application.Exit()
      End Sub
    Sunday, May 16, 2010 9:59 PM

All replies

  • Hi,

    usually you have 2 posobilities. End the application when the start form is closed, or end the application when the last form is closed. You can switch from one to the other by changing the project properties ( Application tab ).
    If you want to do it the way you wish, you need to create your forms withevent and then you need to handle the FormClosed event to dispose the created forms. Then after your last form is closed by code the application ends.


    Hannes

    If you have got questions about this, just ask.
    Mark the thread as answered if the answer helps you. This helps others who have the same problem !
    C# to VB.NET: http://www.developerfusion.com/tools/convert/csharp-to-vb/
    Sunday, May 16, 2010 7:08 PM
  •  

    If you want Form1, Form2 and Form3 to stop the application when ithe form is closed,

    Put on Form1, form2 and Form3 this code

      Private Sub Form_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Application.Exit()
      End Sub
    Sunday, May 16, 2010 9:59 PM
  • Or as another idea, on Form1 create a Public boolean variable.

    On the close of the other form(s), write a value to that boolean variable on Form1, then (assuming you're opening the other form modally using "ShowDialog") on the return back from calling that second form, check to see if the variable is set to to true, and if so then in Form1 you close the application.

    Sunday, May 16, 2010 11:54 PM
  • Hi Frank

    I am not sure how to check the boolean variable on form1 because when other forms (form2, form3, etc) open, the main form (form1 == login form) is set invisible.

    Thanks.

    Monday, May 17, 2010 3:00 AM
  • Thanks crazypennie!! it works just exactly like i want!! thanks so much :) :) :)
    Monday, May 17, 2010 4:09 AM
  • thanks a lot to all u guys!!! :)
    Monday, May 17, 2010 4:10 AM
  • sorry crazypennie! I could not find that where( or at which event of the form), this code, if I want when child form is closed, parent is also closed.
    Saturday, August 31, 2013 4:45 PM
  • sorry Viral Malinka! I could not find that where( or at which event of the form), this code, if I want when child form is closed, parent is also closed.
    Saturday, August 31, 2013 4:48 PM