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.
dibblmAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.