Karthick
Karthick

Reputation: 89

Winform Forms Closing and opening a new form

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.

Upvotes: 8

Views: 61687

Answers (8)

Ahmed Tarek
Ahmed Tarek

Reputation: 104

try that it might help you

frmHome frm = new frmHome();
this.hide()
frm.ShowDialog();
this.Close();

Upvotes: 3

Rohan Loomis
Rohan Loomis

Reputation: 11

All you need to do is this it closes the 2 forms without problem. Form1 fin = new Form1(); fin.Close(); this.Visible = false; Form2 win = new Form2(); win.Visible = true;

Upvotes: 1

Skintkingle
Skintkingle

Reputation: 1579

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.

Upvotes: 1

davidsleeps
davidsleeps

Reputation: 9513

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

Upvotes: 0

manji
manji

Reputation: 47968

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();

Upvotes: 18

AngryHacker
AngryHacker

Reputation: 61646

In program.cs:

void Main() {
  frmLogin fl = new frmLogin();
  if (fl.ShowModal() == DialogResult.Ok) {
    frmHome fh = new frmHome();
    fh.Show();
  }
}

Upvotes: 7

Glenn
Glenn

Reputation: 1167

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.

Upvotes: 0

Aaron Daniels
Aaron Daniels

Reputation: 9664

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.

Upvotes: 0

Related Questions