tags:

views:

245

answers:

6
1. frmHome frm = new frmHome();
   frm.Show();
   this.Close();

I'm opening HomeForm from LoginForm. In LoginForm's form_closed event I call Application.Exit(). This allows you to open LoginForm and exit the application by clicking the X button.

The problem is when I move from LoginForm to HomeForm and call this.Close(), the form_closed event of LoginForm triggers and the application gets closed.

I am allowed to show only one form at a time. Please give me some suggestions to overcome this issue.

Many thanks in advance, Karthick

A: 

One way to accomplish this:

  1. Open the main form on startup.
  2. Hide it. (optional really, but not for you if you really can't show more than one form.)
  3. Open your login form with ShowDialog();
  4. If login is successful, then show your main form. If not, then close your main form / the application.
Aaron Daniels
Sorry i have many forms , i need to show one by closing another one in the mean time i need to take care of application.exit else EXE will be running in the task process manager. Thanks for your help, but couldn't use this.
Karthick
+1  A: 

you can use a boolean (global variable) as exit flag in LoginForm

initialize it to :

exit = true;//in constructor

set it to false before closing:

frmHome frm = new frmHome();
frm.Show();
exit = false;
this.Close();

and in form_closed:

if(exit) Application.Exit();

if a user closes the form with the 'X' button, exit will have the value true, and Application.Exit() will be called.

EDIT:

the above is not working because LoginForm is your main form used by Application.Run(loginForm).

2 suggestions:

With exit flag:

replace

Application.Run(new LoginForm())

by

LoginForm loginFrm = new LoginForm();
loginFrm.Show();
Application.Run();

Without exit flag:

replace in your current code:

frmHome frm = new frmHome();
frm.Show();
this.Close();

by

frmHome frm = new frmHome();
this.Visible = false;
frm.ShowDialog();
this.Close();
najmeddine
Thanks, even i thought about having a flag, but somehow this is not working for me , for any case it closes the application. If you have working code you can share with me. Thanks for your help in advance.
Karthick
the problem is `frm.Show()` that will return right away and the `LoginForm.Close()` that will terminate the program. I'll suggest a solution to this using `frm.ShowDialog()` that waits until the new form is closed before closing LoginForm. But frankly, it's not clean what you are doing. Can you explain more the whole problem or post more code for us to understand your problem.
najmeddine
A: 

Why don't you just stop calling application.exit in the form_closed event?

I am not sure that you really need it anyway, and if you do then you can remove the x icon from the screen and give them a close button.

Alternatively there is a CloseReason in the event args that will tell you if it is a user closing the form or code or something else.

Glenn Condron
No i can't use an Exit button by disabling X button , this is a very tricky issue. Thanks for your comment, but it is not solving my issue yet.
Karthick
and the CloseReason? From memory it is CloseReason.UserClosing when a user has closed the form, and something else when it is programatically closed.
Glenn Condron
+2  A: 

In program.cs:

void Main() {
  frmLogin fl = new frmLogin();
  if (fl.ShowModal() == DialogResult.Ok) {
    frmHome fh = new frmHome();
    fh.Show();
  }
}
AngryHacker
Sorry i have multiple forms which i need to show one after another. Thanks for your help, but couldn't make use of this piece of code.
Karthick
@Karthick. This code does exactly what you want it to do. First it shows the frmLogin form. When the frmLogin is dismissed (by pressing OK, for instance), the form disappears and frmHome is shown. I am using ShowModal method on frmLogin - this means that the code goes no further until the form is closed.
AngryHacker
@Karthick: AngryHacker's suggestion is very good, if this doesn't satisfy you you either don't understand what you're doing or should completely rewrite your question to make us understand.
Sorin Comanescu
A: 

I'm not quite sure I'm understanding this, perhaps you could do something like this if you want to loop through a specific order of forms: (pseudo code)

Dim formsList as New List(Of form)
formsList.add(Form1)
formsList.add(Form2)
formsList.add(Form3)
' etc etc etc '

For Each f as Form in formsList

    f.ShowDialog()
    ' you could have a condition here which breaks the for loop perhaps '

Next

Me.close ' close the app '

You could add a condition into the For loop which breaks the for loop to end early...

note: sorry for the vb.net code...but it should be easy to understand though

davidsleeps
A: 

you can force the form to hide, instead of close. instead of catching the form_closed event, catch the form_closing event. it would look something like this.

private void LoginFrm_Closing(object sender, FormClosingEventArgs e)
{
    e.Cancel = true;
    this.Hide();
    frmHome frm = new frmHome();
    frm.Show();
}

this will keep both open, but only one visible. somewhere in frmHome, possibly a public variable to hold the LoginFrm, so you can toggle between the two with Hide(); and Show(); (and any other forms you may wish to add)

Edit: Grammar.

Skintkingle