点击“是”当退出winform C#时加倍? [英] Click "yes" double when exit winform C#?
本文介绍了点击“是”当退出winform C#时加倍?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好
我创建了一个退出winform C#的代码,但是我必须在消息框中点击是两次。它有什么问题?
这是我的代码:
Hello
I make a code to exit winform C#, it works but I must click "yes" in messagebox twice. What wrong with it?
Here is my code:
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
DialogResult result = MessageBox.Show("Sure?", "Exit", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
Application.Exit();
}
else
{
e.Cancel = true;
}
}
推荐答案
试试这样:
Try it like this :
Protected Overrides Sub OnFormClosing(e As System.Windows.Forms.FormClosingEventArgs)
Dim myResult As DialogResult = MessageBox.Show(Me, "FormClosing-Message", "Caption-Text", MessageBoxButtons.YesNo)
If myResult = Windows.Forms.DialogResult.No Then
e.Cancel = True
End If
MyBase.OnFormClosing(e)
End Sub
我不使用Event而是使用Event-calling-Method。
当DialogResult为是时你不需要做任何事 - 这是正常情况......
和额外的C#代码:
I don't use the Event but the Event-calling-Method.
You don't Need to do anything when DialogResult is "Yes" - it is the normal case ...
and additional as C#-Code :
protected override void OnFormClosing(System.Windows.Forms.FormClosingEventArgs e)
{
DialogResult myResult = MessageBox.Show(this, "FormClosing-Message", "Caption-Text", MessageBoxButtons.YesNo);
if (myResult == Windows.Forms.DialogResult.No) {
e.Cancel = true;
}
base.OnFormClosing(e);
}
Hi
为该表单创建对象,使用obj u可以关闭表单。而不是使用Application.Exit使用close。
Hi Create object for that form,using obj u can close form.Instead of using Application.Exit use close.
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
DialogResult result = MessageBox.Show("Sure?", "Exit", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
Form1 ff= new Form1() ;
ff.Close ();
}
else
{
e.Cancel = true;
}
}
这篇关于点击“是”当退出winform C#时加倍?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文