• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 295
  • Last Modified:

vb.net Help closing all open forms-Quit Function

I have  a simple App with 2 forms.

How do I close both forms with 1 close event.

I start a project with one forms that opens another as a dialog.

 Dim Login As New frmLogin
        Login.ShowDialog()

If someone X's out of the dialog. I need both forms to close and the app to quit..?

I know this has to be and easy question but for a newbie. I have to say i am lost.
0
dibblm
Asked:
dibblm
  • 5
  • 5
  • 4
  • +1
1 Solution
 
frodomanCommented:
If someone X's out of the dialog it should return a DialogAbort value.  Just watch for this and close your form if it's found.

   Login.ShowDialog()
   If Login..DialogResult = DialogResult.Abort Then
      me.close
   End If
0
 
frodomanCommented:
Oops - should ony be one period in Login.DialogResult - sorry for the typo.

Also, you may want to consider making sure your OK (or whatever) button on your dialog form returns the DialogOK result.  Then you can make a check like this:

Login.ShowDialog()
If Login.DialogResult <> DialogResult.DialogOK Then
   me.close
End If

Or if you want to force a response:

DO
   Login.ShowDialog
LOOP WHILE Login.DialogResult <> DialogResult.DialogOK
0
 
sony1464Commented:
Assuming u want to close both the forms from form1 from where u r calling frm2 then add this bit of code

Private Sub EntryForm_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
 
 If MessageBox.Show("Do you want to exit the program?", "Exit", MessageBoxButtons.OKCancel) = DialogResult.OK Then
Me.close
else
e.cancel=true
           
 End If
 End Sub
0
Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 
dibblmAuthor Commented:
This is not closing my 2nd form though.

 I created the sub as follows.
---------------------------------------------------
 Sub main()
        Dim frmstart As New frmStartup
        frmstart.ShowDialog()
        If frmstart.DialogResult <> DialogResult.OK Then
            'DialogResult.DialogOK Then
            'me.close()
            frmstart.Close()
        End If

        'Or if you want to force a response:

        Do
            frmstart.ShowDialog()
        Loop While frmstart.DialogResult <> DialogResult.OK

    End Sub
------------------------------------------------
OK> Now I have one form (frmstart) open as a dialog. "Why a dialog?" so that it takes precidense and consumes the screen. IF I am correct.

This then opens a 2nd form. (frm login) as dialog as well so that forms consumes teh focus of anything behind it.

IF I close the login form. The frmstart still remains open. I will gladly grant you the 50 points for getting me this far and not realizing it wasn't just something simple, If you tell me how to alter the points being awarded for this I will gladly grant 250.
0
 
dibblmAuthor Commented:
point are increased. My solution is still open.

If anyone is open to the challenge of helping osmeone as new as me step where he yet belongs I will be happy to email or post my project or forms to anyone who wants them.
0
 
dibblmAuthor Commented:
Sony's idea doesn't work.

After the form closes. It bounces back to the other forms load event which fires the load event of the form that just closed and re-opens it.
0
 
frodomanCommented:
If what you really want to do is close everything and terminate the application, why not just end it?

      If frmstart.DialogResult <> DialogResult.OK Then
            End
      End If

.Net will close all open forms and the garbage collector will dispose of the objects.


By the way, you have above:

        Dim frmstart As New frmStartup
        frmstart.ShowDialog()
        If frmstart.DialogResult <> DialogResult.OK Then
            'DialogResult.DialogOK Then
            'me.close()
            frmstart.Close()
        End If

But the If...Then statement won't execute until frmstart has *already* been closed so trying to close it based on the dialogresult isn't doing anything.  I think what you meant may be:

        Dim frmstart As New frmStartup
        frmstart.ShowDialog()
        If frmstart.DialogResult <> DialogResult.OK Then
            me.close()
        End If

0
 
dibblmAuthor Commented:
Beautiful. That was what I needed. I didnt know that the end statement could be used in that way. Thank you so much.

 I thought at first it was something very easy. But...Can I use the Garbage collector in this way without causing any damage further down the road? Right now nothing is open. All the data connections have already been closed up to this point.
0
 
UberDeveloperCommented:
i don't think this will work becuase a regular form and not an acutall "Dialog Box" such as the MessageBox will not have a dialog result as it is a FORM and not a Dialog BOX.  I will have a working solution for you in the next few minutes.

0
 
UberDeveloperCommented:
Ok nevermind...I guess I was to late.   :D
0
 
UberDeveloperCommented:
well here is an alternate way to do it....I spent some time on it so i'll put it here anyway.   :D

Put this in frmStartUp:

    Private Sub frmStartup_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim login As New frmLogin
        login.ShowDialog()
        If login.DialogResult <> DialogResult.OK Then
            Me.Close()
        End If
    End Sub

and just put this in frmLogin:

 Private Sub frmLogin_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed
        DialogResult = DialogResult.Abort
    End Sub
0
 
UberDeveloperCommented:
oh yeah and then just set your startup object to frmStartup
0
 
frodomanCommented:
UberDeveloper:

A "dialog box" is nothing more than a form that has been opened with the "ShowDialog" method.  This means you *can* add "DialogResult" properties to the controls (usually buttons) or to the form itself (as you've done at the end of your example) and use it as a dialog box.

Cheers,

Frodoman
0
 
UberDeveloperCommented:
ok i guess you are right but my solution still works.  :P

Tim
0
 
dibblmAuthor Commented:
Actually. I am using the dialog method only for the fact that it is a log on form. It needs to be top most and modal so nothing can happen behind it until it checks the userid.

 Everyone here has put much effort into this question and I only regret that I could only issue points to one person. So from here all I have left for everyone else is a thank you. And thank all of you for even coming to place to be able to help people like myself just starting to program. I am working on a database driven app at the moment and I will have more questions to come and will gladly be handing out more points. I dont know what the point are good for other than rankings but if you see my name you know that I will be gracefully handing whatever points are needed by those of you with answers.
0

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

  • 5
  • 5
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now