closing the login form closes the application

G

Guest

I have a C# Windows Application and the solution in VS 2005 contains 2
projects. The first project handles the login and password changes and the
second project contains the main application. When a successful login occurs
I open the main application window, but when I close the login window the
entire application closes. With this happening when the user closes the main
application window the login window is still open, or hidden, and the
application doesn't close.

Here's what I'm trying to do. ( right now instead of using this.Close() I'm
using this.Hide() to get the login form out of the way without closing the
application )

// We have a successful Login
this.Close();

// open the main form
PhoneLog.PhoneLog form = new PhoneLog.PhoneLog();
form.Show();


Thanks!
 
B

Brian Schwartz

I bet you have a method somewhere that looks like this:

static void Main()
{
Application.Run(new LoginForm()); //where LoginForm is whatever the
name of your login form is
}

The form specified in the Application.Run statement is the one that will
receive the ongoing Windows message loop. When that form is closed, the
application exits.

Put your main form in this statement instead. Then you have two options: 1)
Can call the login form from the main form. If login fails, close the main
form. 2) Manually call the login form in Main() before the Application.Run
statement. In other words, it would look like this:

static void Main()
{
LoginForm login = new LoginForm();
DialogResult result = login.ShowDialog();

if (result == DialogResult.OK)
Application.Run(new PhoneLog.PhoneLog());
}
 
G

Guest

Hi Brian,

I modified my code as you suggested, but when this.Close() runs then
login.ShowDialog() returns Cancel and the entire program closes. Is there
something else I should be looking for?

Thanks,
Matt
 
B

Brian Schwartz

Instead of using this.Close, set this.DialogResult = DialogResult.OK. If you
never give the form a DialogResult, then it will always return Cancel. Sorry
I forgot to mention that detail. Take a look at the Form.DialogResult
property in the documentation for more info...there are some other details
mentioned in there about interacting with buttons and disposing the form.
Setting the dialog result only hides it, not closes it, so you'll still need
to dispose it after you've read the DialogResult.
 
G

Guest

Thanks, Brian! That did the trick!

Brian Schwartz said:
Instead of using this.Close, set this.DialogResult = DialogResult.OK. If you
never give the form a DialogResult, then it will always return Cancel. Sorry
I forgot to mention that detail. Take a look at the Form.DialogResult
property in the documentation for more info...there are some other details
mentioned in there about interacting with buttons and disposing the form.
Setting the dialog result only hides it, not closes it, so you'll still need
to dispose it after you've read the DialogResult.

--
Brian Schwartz
FishNet Components
http://www.fishnetcomponents.com
Fish Grid .NET Light: Powerful Layouts for Small Datasets
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top