Все это и, по крайней мере, два других перечислены на странице инструментов wan beanstalkd:
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.
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();
In program.cs:
void Main() {
frmLogin fl = new frmLogin();
if (fl.ShowModal() == DialogResult.Ok) {
frmHome fh = new frmHome();
fh.Show();
}
}
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.
Один из способов сделать это:
Я не совсем уверен, что понимаю это, возможно, вы могли бы сделать что-то вроде этого, если хотите пройти через определенный порядок форм: (псевдокод)
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 '
Вы могли бы добавьте в цикл For условие, которое прерывает цикл for для преждевременного завершения ...
примечание: извините за код vb.net ... но это должно быть легко понять
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.