Solved

vb.net Help closing all open forms-Quit Function

Posted on 2003-11-07
15
271 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:dibblm
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:dibblm
Comment Utility
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
Comment Utility
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
Comment Utility
Ok nevermind...I guess I was to late.   :D
0
 
LVL 2

Expert Comment

by:UberDeveloper
Comment Utility
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
Comment Utility
oh yeah and then just set your startup object to frmStartup
0
 
LVL 42

Expert Comment

by:frodoman
Comment Utility
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
Comment Utility
ok i guess you are right but my solution still works.  :P

Tim
0
 

Author Comment

by:dibblm
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

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…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

744 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now