Solved

close an windows app

Posted on 2011-03-25
6
467 Views
Last Modified: 2012-05-11
to start , i am pretty new at the c# windows app world

when my app starts in the constructor of the form1 i check to see if two variables are populated)
(uname and password)

the problem i am having is if the user closes the login form or cancels i need the app to go away
i thought in the else of the dialogresult i could do
                   System.Windows.Forms.Application.Exit();

but that does not work.

looks for suggestions on the best way to do this?
thanks




       public Form1()
        {
            InitializeComponent();
            if (username == null || passwd == null)
            {
                DialogResult dr = new DialogResult();
                LoginForm login = new LoginForm();
                dr = login.ShowDialog();
                if (dr == DialogResult.OK)
                {
                }
                else
                {
                }
            }
        }

0
Comment
Question by:paries
6 Comments
 
LVL 6

Accepted Solution

by:
kswathi earned 500 total points
ID: 35214885
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 35214909
Is the following what you want?  Closing the main form of an app will close the app automatically.


 public Form1()
        {
            InitializeComponent();
            if (username == null || passwd == null)
            {
                DialogResult dr = new DialogResult();
                LoginForm login = new LoginForm();
                dr = login.ShowDialog();
                if (dr == DialogResult.OK)
                {
                }
                else
                {
this.Close();  //Close this form
                }
            }
        }
0
 

Author Comment

by:paries
ID: 35214967
Andy
i tried the this.Close();
it threw the exception  ::


System.InvalidOperationException was unhandled
  Message=Starting a second message loop on a single thread is not a valid operation. Use Form.ShowDialog instead.
  Source=System.Windows.Forms
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:paries
ID: 35214981
This works..
Use Environment.Exit(0)
thanks, found from kswathi:
0
 
LVL 7

Expert Comment

by:mkobrin
ID: 35214983
If you are going to use this you must remember that any code in the block after this.close(); will still execute, so you will need to put a return; afterwards to leave this block of code immiediatly:

public Form1()
        {
            InitializeComponent();
            if (username == null || passwd == null)
            {
                DialogResult dr = new DialogResult();
                LoginForm login = new LoginForm();
                dr = login.ShowDialog();
                if (dr == DialogResult.OK)
                {
                }
                else
                {
this.Close();  //Close this form
return; //Break out of this code block and force the close to happen immiediatly
                }
            }
        }
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 35215179
Exception - wierd.  You must have some more code that I wasn't expecting based on your question.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

839 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