当我单击Form Closing按钮(clasic red X按钮)时,我想在系统托盘中隐藏我的应用程序。我提供了这段代码;
private void Ana_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
this.Hide();
}
所以我的程序stil在系统托盘中运行,我添加了一个ContextMenuStrip,当我右键单击我的鼠标contextmenustrip时出现“关闭”,但当我点击“关闭”我的程序仍在运行时我想退出我的应用程序时我点击“关闭”。这是我的代码:
private void kapatToolStripMenuItem_Click(object sender, EventArgs e) //Close
{
DialogResult ext;
ext = MessageBox.Show("Çıkmak İstediğinizden Emin misiniz?", "Onay", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (ext == DialogResult.Yes)
{
Application.Exit();
}
}
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
this.Show();
}
答案 0 :(得分:0)
调用Application.Exit()
实际上会尝试关闭所有表单,并且由于您的FormClosing
被硬编码为取消,因此无法完成任务。
当前结构的一个选项是创建AllowClose
标志。这可以是Form的属性,也可以是全局静态值。
例如:
//in your form
public bool AllowClose {get;set;}
private void Ana_FormClosing(object sender, FormClosingEventArgs e)
{
if(!AllowClose)
{
e.Cancel = true;
this.Hide();
}
}
//in your context menu event
private void kapatToolStripMenuItem_Click(object sender, EventArgs e) //Close
{
DialogResult ext;
ext = MessageBox.Show("Çıkmak İstediğinizden Emin misiniz?", "Onay", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (ext == DialogResult.Yes)
{
this.AllowClose = true;
Application.Exit();
}
}
答案 1 :(得分:0)
试试这个,
bool isClosing = false;
private void Ana_FormClosing(object sender, FormClosingEventArgs e)
{
if(!isClosing)
{
e.Cancel = true;
this.Hide();
}
}
private void kapatToolStripMenuItem_Click(object sender, EventArgs e) //Close
{
DialogResult ext;
isClosing = true;
ext = MessageBox.Show("Çıkmak İstediğinizden Emin misiniz?", "Onay", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (ext == DialogResult.Yes)
{
Application.Exit();
}
}