[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

vb.net Help closing all open forms-Quit Function

Posted on 2003-11-07
15
Medium Priority
?
287 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 

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 1000 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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

649 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