Solved

vb.net Help closing all open forms-Quit Function

Posted on 2003-11-07
15
283 Views
Last Modified: 2012-06-27
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
Comment
Question by:dibblm
  • 5
  • 5
  • 4
  • +1
15 Comments
 
LVL 42

Expert Comment

by:frodoman
ID: 9701841
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
 
LVL 42

Expert Comment

by:frodoman
ID: 9701878
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
 
LVL 3

Expert Comment

by:sony1464
ID: 9702334
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
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 

Author Comment

by:dibblm
ID: 9702473
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
 

Author Comment

by:dibblm
ID: 9702537
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
 

Author Comment

by:dibblm
ID: 9702645
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
 
LVL 42

Accepted Solution

by:
frodoman earned 250 total points
ID: 9702787
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
 

Author Comment

by:dibblm
ID: 9702819
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
 
LVL 2

Expert Comment

by:UberDeveloper
ID: 9702824
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
 
LVL 2

Expert Comment

by:UberDeveloper
ID: 9702834
Ok nevermind...I guess I was to late.   :D
0
 
LVL 2

Expert Comment

by:UberDeveloper
ID: 9702856
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
 
LVL 2

Expert Comment

by:UberDeveloper
ID: 9702863
oh yeah and then just set your startup object to frmStartup
0
 
LVL 42

Expert Comment

by:frodoman
ID: 9702911
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
 
LVL 2

Expert Comment

by:UberDeveloper
ID: 9702967
ok i guess you are right but my solution still works.  :P

Tim
0
 

Author Comment

by:dibblm
ID: 9703479
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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question