通过调用按钮单击事件处理 winForm 中的关闭事件会导致两种不同的行为
Handling Closing event in winForm by calling a button click event results in two different behaviour
我有一个简单的 winForm,想要使用关闭事件并将用户重定向到注销过程。注销过程由按钮单击事件处理。当用户按下注销按钮并选择否时,它将保留在页面上,用户可以继续使用该应用程序。在关闭事件中,我调用了注销按钮事件方法,但是当用户选择 NO 时,window 关闭并且程序仍然 运行。我想知道发送到方法的事件是否不同或者基本上为什么会发生?
using System;
using System.Drawing;
using System.Windows.Forms;
namespace myApp {
public partial class frmMain : Form {
readonly private string password = "user";
readonly private string username = "pass";
public frmMain() {
InitializeComponent();
this.MaximumSize = new Size(348, 247);
this.MinimumSize = new Size(348, 247);
}
private void frmMain_Load(object sender, EventArgs e) {
txtPass.MaxLength = 8;
txtPass.PasswordChar = '*';
}
private void btnLogin_Click(object sender, EventArgs e) {
bool goOn = txtPass.Text.Equals(this.password) && txtUser.Text.Equals(this.username);
if (goOn) {
this.Visible = false;
Form2 f = new Form2();
f.Show();
}
else {
MessageBox.Show("Username or Password is not correct", "Invalid Input", MessageBoxButtons.OK);
}
}
private void frmMain_FormClosing(object sender, FormClosingEventArgs e) {
Environment.Exit(0);
}
} // end main form
}
以及登录后的第二个表单:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Net;
using System.Net.Mail;
using System.Runtime.Serialization.Formatters.Binary;
using System.Windows.Forms;
namespace myApp{
public partial class Form2 : Form {
public Form2() {
InitializeComponent();
this.MaximumSize = new Size(1032, 691);
this.MinimumSize = new Size(1032, 691);
}
private void Form2_Load(object sender, EventArgs e){
Button btnLogout = new Button();
btnLogout.Text = "Logout";
btn.Location = new Point(10, 10);
this.Controls.Add(btn);
}
private void btnLogout_Click(object sender, EventArgs e) {
DialogResult result = MessageBox.Show("Do you want to logout?", "Logout", MessageBoxButtons.YesNo);
if (result == DialogResult.Yes) {
this.Visible = false;
frmMain f = new frmMain();
f.Show();
}
else {
return;
}
} // end logout
private void Form2_FormClosing(object sender, FormClosingEventArgs e) {
btnLogout_Click(sender, e);
}
这张图片显示了 运行 在没有从 关闭事件中选择任何选项,但没有 window 打开之后的过程。
使用 tasklist
命令检查 CMD 显示应用程序仍然 运行。 myApp.exe
在任务列表中。
FormClosing
方法将在结束时关闭表单,您应该取消表单关闭过程。
试试这个 Form2_FormClosing:
private void Form2_FormClosing(object sender, FormClosingEventArgs e) {
DialogResult result = MessageBox.Show("Do you want to logout?", "Logout", MessageBoxButtons.YesNo);
if (result == DialogResult.Yes) {
this.FormClosing -= new System.Windows.Forms.FormClosingEventHandler(this.Form2_FormClosing);
this.Visible = false;
frmMain f = new frmMain();
f.Show();
}
else {
e.Cancel = true;
}
}
和btnLogout点击:
private void btnLogout_Click(object sender, EventArgs e) {
this.Close();
}
在窗体关闭事件中,窗体关闭后,所有关联的资源将被释放,窗体将被销毁。为避免表单的 Cancel 属性 可以设置为 true 以停止关闭表单。
private void Form2_FormClosing(object sender, FormClosingEventArgs e){
button1_Click( sender, e);
e.Cancel = true;
}
我有一个简单的 winForm,想要使用关闭事件并将用户重定向到注销过程。注销过程由按钮单击事件处理。当用户按下注销按钮并选择否时,它将保留在页面上,用户可以继续使用该应用程序。在关闭事件中,我调用了注销按钮事件方法,但是当用户选择 NO 时,window 关闭并且程序仍然 运行。我想知道发送到方法的事件是否不同或者基本上为什么会发生?
using System;
using System.Drawing;
using System.Windows.Forms;
namespace myApp {
public partial class frmMain : Form {
readonly private string password = "user";
readonly private string username = "pass";
public frmMain() {
InitializeComponent();
this.MaximumSize = new Size(348, 247);
this.MinimumSize = new Size(348, 247);
}
private void frmMain_Load(object sender, EventArgs e) {
txtPass.MaxLength = 8;
txtPass.PasswordChar = '*';
}
private void btnLogin_Click(object sender, EventArgs e) {
bool goOn = txtPass.Text.Equals(this.password) && txtUser.Text.Equals(this.username);
if (goOn) {
this.Visible = false;
Form2 f = new Form2();
f.Show();
}
else {
MessageBox.Show("Username or Password is not correct", "Invalid Input", MessageBoxButtons.OK);
}
}
private void frmMain_FormClosing(object sender, FormClosingEventArgs e) {
Environment.Exit(0);
}
} // end main form
}
以及登录后的第二个表单:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Net;
using System.Net.Mail;
using System.Runtime.Serialization.Formatters.Binary;
using System.Windows.Forms;
namespace myApp{
public partial class Form2 : Form {
public Form2() {
InitializeComponent();
this.MaximumSize = new Size(1032, 691);
this.MinimumSize = new Size(1032, 691);
}
private void Form2_Load(object sender, EventArgs e){
Button btnLogout = new Button();
btnLogout.Text = "Logout";
btn.Location = new Point(10, 10);
this.Controls.Add(btn);
}
private void btnLogout_Click(object sender, EventArgs e) {
DialogResult result = MessageBox.Show("Do you want to logout?", "Logout", MessageBoxButtons.YesNo);
if (result == DialogResult.Yes) {
this.Visible = false;
frmMain f = new frmMain();
f.Show();
}
else {
return;
}
} // end logout
private void Form2_FormClosing(object sender, FormClosingEventArgs e) {
btnLogout_Click(sender, e);
}
这张图片显示了 运行 在没有从
使用 tasklist
命令检查 CMD 显示应用程序仍然 运行。 myApp.exe
在任务列表中。
FormClosing
方法将在结束时关闭表单,您应该取消表单关闭过程。
试试这个 Form2_FormClosing:
private void Form2_FormClosing(object sender, FormClosingEventArgs e) {
DialogResult result = MessageBox.Show("Do you want to logout?", "Logout", MessageBoxButtons.YesNo);
if (result == DialogResult.Yes) {
this.FormClosing -= new System.Windows.Forms.FormClosingEventHandler(this.Form2_FormClosing);
this.Visible = false;
frmMain f = new frmMain();
f.Show();
}
else {
e.Cancel = true;
}
}
和btnLogout点击:
private void btnLogout_Click(object sender, EventArgs e) {
this.Close();
}
在窗体关闭事件中,窗体关闭后,所有关联的资源将被释放,窗体将被销毁。为避免表单的 Cancel 属性 可以设置为 true 以停止关闭表单。
private void Form2_FormClosing(object sender, FormClosingEventArgs e){
button1_Click( sender, e);
e.Cancel = true;
}