我正在开发一个Windows窗体应用程序。当用户点击主窗口中的“X”按钮时,我想向用户显示一条消息,说明接近原因。“X”按钮表示窗口中“最小化”,“最大化”和“关闭”托盘中的“关闭”按钮。 / p>
我已经写了这段代码。
private void frmIMS_FormClosing(object sender, FormClosingEventArgs e)
{
if (MessageBox.Show("This application is closing down because of " + e.CloseReason.ToString() + ". Do you really want to close it ?", "", MessageBoxButtons.YesNo) == DialogResult.No)
{
e.Cancel = true;
}
else
{
Application.Exit();
}
}
现在发生的情况是,如果用户单击no消息框,则事件被丢弃,当用户单击yes时,form_closing()再次触发并显示其他messagebox.So messagebox显示两次。我想显示一次。请帮助并告诉它为什么要两次射击。
答案 0 :(得分:2)
您可以跳过应用程序的其他部分。如果您的表单是主要的申请形式,它仍然会退出。
Application.Exit()会导致所有窗口关闭。您的“第一次”关闭仍处于暂挂状态,因此表单尚未关闭,Application.Exit()会尝试第二次关闭您的表单。
你可以试试这个:
bool closingPending = false;
private void frmIMS_FormClosing(object sender, FormClosingEventArgs e)
{
if (closingPending) return;
if (MessageBox.Show("This application is closing down because of " + e.CloseReason.ToString() + ". Do you really want to close it ?", "", MessageBoxButtons.YesNo) == DialogResult.No)
{
e.Cancel = true;
}
else
{
closingPending = true;
Application.Exit();
}
}
答案 1 :(得分:2)
我知道这个帖子有点旧,但为什么不呢:
$Application.OpenForms["NameOfMainForm"].Close;
答案 2 :(得分:1)
我知道这是旧的,我偶然发现了这个问题,因为我遇到了同样的行为。我正在使用.Net 4.5,以下是我如何解决这个问题:
private void frmIMS_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing)
{
if (MessageBox.Show("This application is closing down because of " + e.CloseReason.ToString() + ". Do you really want to close it ?", "", MessageBoxButtons.YesNo) == DialogResult.No)
{
e.Cancel = true;
}
else
{
Application.Exit();
}
}
}
您可以在此处找到有关CloseReason
枚举的更多信息:http://msdn.microsoft.com/en-us/library/system.windows.forms.closereason.aspx
答案 3 :(得分:0)
为什么Application.Exit在else分支中?这是自动完成的,可以解决您的问题。
答案 4 :(得分:0)
Private Sub ClaimEditor_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
If MsgBox("Edit mode active. Exit without saving?", MsgBoxStyle.YesNo Or MsgBoxStyle.Question, "Edit mode") <> MsgBoxResult.Yes Then
e.Cancel = True
Exit Sub
'Else
' e.Cancel = False
End If
WriteIntoRregistry()
RemoveHandler Me.FormClosing, AddressOf ClaimEditor_FormClosing
Application.Exit()
End Sub
答案 5 :(得分:0)
我遇到了同样的问题,并确定Application.Exit()正在调用FormClosing事件。 只将Application.Exit()放在FormClosed事件中,而不是FormClosing事件。
使用此代码:
private void frmIMS_FormClosing(object sender, FormClosingEventArgs e)
{
if (closingPending) return;
if (MessageBox.Show("This application is closing down because of " +
e.CloseReason.ToString () + ". Do you really want to close it ?", ""
, MessageBoxButtons.YesNo) == DialogResult.No)
{
e.Cancel = true;
}
else
{
closingPending = true;
// Application.Exit(); <-- Remove this
}
private void frmIMS_FormClosing(object sender, FormClosingEventArgs e)
{
Application.Exit(); <-- Put it here.
}
答案 6 :(得分:0)
我认为@ Tornado726已经为所有人提供了最好的答案。但是我猜的只有一个复制/粘贴错误(键入form_closing两次)
Private Sub FORM1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
Select Case (MessageBox.Show("Do you really want to close?", "Quit", MessageBoxButtons.YesNo))
Case MsgBoxResult.No
e.Cancel = True
End Select
End Sub
Private Sub FORM1_FormClosed(sender As Object, e As FormClosedEventArgs) Handles MyBase.FormClosed
Application.Exit()
End Sub
答案 7 :(得分:0)
试试这个:这将是一个很好的帮助
private void Master_FormClosing(object sender, FormClosingEventArgs e)
{
DialogResult result = MessageBox.Show("Do you really want to exit the program?", "Data Patch", MessageBoxButtons.YesNo);
if (result != DialogResult.Yes)
{
e.Cancel = true;
}
}
答案 8 :(得分:0)
Application.Exit()
来电frmX_FormClosing()
。
这就是FormClosing()
被调用两次的原因。
答案 9 :(得分:-1)
我刚刚遇到同样的问题。这就是我做的事情:
private void frmIMS_FormClosing(object sender, FormClosingEventArgs e)
{
if (MessageBox.Show("This application is closing down because of " + e.CloseReason.ToString() + ". Do you really want to close it ?", "", MessageBoxButtons.YesNo) == DialogResult.No)
{
e.Cancel = true;
}
else
{
try
{
Environment.Exit(0); // It will try to close your program "the hard way"
}
catch (Exception)
{
Application.Exit(); // If a Win32 exception occurs, then it will be able to close your program "the normal way"
}
}
}
我确定它会为你工作,如果你还没有找到解决方案,甚至放弃了代码,因为它已经有一段时间了。< / p>