C# 在 FormClosing 方法中处理是/否/取消消息框中的取消按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1252613/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 14:15:39  来源:igfitidea点击:

Handling Cancel Button in Yes/No/Cancel Messagebox in FormClosing Method

c#.netmessagebox

提问by Mehdi

I put a Yes/No/Cancel Messageboxin FormClosingMethod of my form. and now this is Message box text: Do You Want to Save Data?

我在表单的 FormClosing方法中放置了一个是/否/取消消息框。现在这是消息框文本:您要保存数据吗?

I am not a profesional and not know how to handle if user clicked CancelButton? Exactly the result of clicking on Cancel Button must be The form remain open.
How to prevent Closing my form in FormClosingmethod?

我不是专业人士,如果用户点击取消按钮,我不知道如何处理?单击取消按钮的确切结果必须是 表单保持打开状态。
如何防止在FormClosing方法中关闭我的表单?

I wrote So far: ;)

我写到目前为止:;)

DialogResult dr = MessageBoxFarsi.Show("Do You Want to Save Data?","",MessageBoxButtons.YesNoCancel,MessageBoxIcon.Warning);

//...
else if (dr == DialogResult.Cancel)
{
    ???
}

Please Help me to complete my code!
Thanks

请帮我完成我的代码!
谢谢

采纳答案by Matthew Iselin

FormClosing has a Boolean parameter which, if set to True when the function returns, will cancel closing the form, IIRC.

FormClosing 有一个布尔参数,如果在函数返回时设置为 True,将取消关闭窗体 IIRC。

EDIT: For example,

编辑:例如,

private void Form1_FormClosing(Object sender, FormClosingEventArgs e) {
    // Set e.Cancel to Boolean true to cancel closing the form
}

See here.

见这里

回答by Dan McClain

You could have something like the following:

你可以有如下内容:

if(dr == DialogResult.Cancel)
{
    e.Cancel = true;
}
else if(dr == DialogResult.Yes)
{
    //Save the data
}

The above code should only close the form if you choose yes or no, and will save data when you choose yes.

上面的代码应该只在选择yes或no时关闭表单,选择yes时会保存数据。

回答by Kasun Nimith

Actually I think you are missing Event handler, oh you can not turn to that even without an even handler. You must add an event with an event handler like this.

实际上,我认为您缺少事件处理程序,哦,即使没有偶数处理程序,您也无法转向。您必须使用这样的事件处理程序添加事件。

private void myform_Closing(object sender, FormClosingEventArgs e) 
{
    DialogResult dr = MessageBoxFarsi.Show("Do You Want to Save Data?","",MessageBoxButtons.YesNoCancel,MessageBoxIcon.Warning)

    if (dr == DialogResult.Cancel) 
    {
        e.Cancel = true;
        return;
    }
    else if (dr == DialogResult.Yes)
    {
        //TODO: Save
    }
}

//now add a default constructor 
public myform()  // here use your form name.
{
    this.FormClosing += new FormClosingEventHandler(myform_Closing); 
}

Forgive me if there are some wrongs spellings in this code because i didn't write it in c# and copy paste here. I just wrote it in here. :)

如果这段代码中有一些拼写错误,请原谅我,因为我没有用 c# 编写它并复制粘贴到这里。我刚写在这里。:)

回答by Ramgy Borja

you should try this function

你应该试试这个功能

public DialogResult msgClose(string msg)
{
     return MessageBox.Show(msg, "Close", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
}

and used like this.

并像这样使用。

private void frm_FormClosing(object sender, FormClosingEventArgs e)
{
     if (conn.msgClose("Application close?") == DialogResult.No)
         e.Cancel = true;
     else
     {
         this.Close();
     }
}

回答by Parag555

You can try this:

你可以试试这个:

if (MessageBox.Show("Are you sure you want to quit?", "Attention!!", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning) == DialogResult.Yes)
{
   //this block will be executed only when Yes is selected
   MessageBox.Show("Data Deleted", "Done", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
else
{
  //this block will be executed when No/Cancel is selected
  //the effect of selecting No/Cancel is same in MessageBox (particularly in this event)
}

If needed same you can do for the Noand CancelButton click using DialogResultclass

如果需要同样可以为做NoCancel按钮单击使用DialogResult