Закрытие Форм Winform и открытие новой формы

Все это и, по крайней мере, два других перечислены на странице инструментов wan beanstalkd:

https://github.com/kr/beanstalkd/wiki/Tools

6
задан GEOCHET 12 February 2014 в 13:59
поделиться

6 ответов

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();
12
ответ дан 8 December 2019 в 05:22
поделиться

In program.cs:

void Main() {
  frmLogin fl = new frmLogin();
  if (fl.ShowModal() == DialogResult.Ok) {
    frmHome fh = new frmHome();
    fh.Show();
  }
}
7
ответ дан 8 December 2019 в 05:22
поделиться

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.

0
ответ дан 8 December 2019 в 05:22
поделиться

Один из способов сделать это:

  1. Открывать главную форму при запуске.
  2. Скрывать ее. (На самом деле необязательно, но не для вас, если вы действительно не можете показать более одной формы.)
  3. Откройте форму входа в систему с помощью ShowDialog ();
  4. Если вход прошел успешно, то покажите вашу основную форму. Если нет, закройте основную форму / приложение.
0
ответ дан 8 December 2019 в 05:22
поделиться

Я не совсем уверен, что понимаю это, возможно, вы могли бы сделать что-то вроде этого, если хотите пройти через определенный порядок форм: (псевдокод)

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 ... но это должно быть легко понять

0
ответ дан 8 December 2019 в 05:22
поделиться

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.

1
ответ дан 8 December 2019 в 05:22
поделиться
Другие вопросы по тегам:

Похожие вопросы: