简体   繁体   English

提示用户在关闭表单之前提交更改

[英]Prompt the user to submit changes before closing form

I am working on a large .NET 4.0 C# project that spans a few groups. 我正在一个跨几个小组的大型.NET 4.0 C#项目。 I have been tasked with modifying a form to prompt the user to submit changes. 我的任务是修改表单以提示用户提交更改。 The form that I need to modify has a "Submit Changes", "Ok", "Cancel" buttons. 我需要修改的表单具有“提交更改”,“确定”,“取消”按钮。 Currently, there are no events triggered by the "Ok" and "Cancel" buttons. 当前,没有由“确定”和“取消”按钮触发的事件。

If the user has made any changes, without submitting them, pressing the "Ok" button should generate a MessageBox to guide the user into submitting his/her changes. 如果用户进行了任何更改而没有提交更改,则按下“确定”按钮应会生成一个MessageBox,以指导用户提交其更改。 This is where my problem occurs. 这是我的问题发生的地方。

I made the "Ok" button trigger an event buttonOk_Click that checks for changes. 我使“确定”按钮触发了一个事件buttonOk_Click,用于检查更改。 The issue here is that since the "Ok" button has been pressed already, I am not sure how to stop the form from closing in order to allow the user to save his/her changes. 这里的问题在于,由于已经按下了“确定”按钮,因此我不确定如何停止关闭表单以允许用户保存其更改。 I cannot modify the code that calls the form since it is part of another group. 我不能修改调用该表单的代码,因为它是另一个组的一部分。

How can I stop the form from closing to allow the user to save his changes? 如何停止关闭表单以允许用户保存其更改?

Set the Form DialogResult property to DialogResult.None 将窗体DialogResult属性设置为DialogResult.None

 form1.DialogResult = DialogResult.None;

The DialogResult enumeration coupled with the property of the same name on the form and on the buttons is used to control how a modal form is closed and what value is returned to the caller. DialogResult枚举与窗体和按钮上的同名属性一起用于控制如何关闭模式窗体以及将什么值返回给调用者。
For example, pressing a button with its DialogResult property set to OK will force the modal form to exit from the ShowDialog() method with a return value set to DialogResult.OK. 例如,按下其DialogResult属性设置为OK的按钮,将强制模式窗体从ShowDialog()方法退出,返回值设置为DialogResult.OK。

using(Form1 f = new Form1())
{
    if(f.ShowDialog() == DialogResult.OK)
    {
        // Perform post confirmation task on the form data
    }
}
// You may use FormClosing Event of Form

  private void yourForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (MessageBox.Show("Want to exit from Application ?",  MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                Environment.Exit(0);
            }
            else
            {
                // your Code for Changes or anything you want to allow user changes etc.
                e.Cancel = true;

            }

        }

Thanks Bhavin Chhatrola; 感谢Bhavin Chhatrola; a slight change if you allow it :) 如果您允许,请稍作更改:)

 private void yourForm_FormClosing(object sender, FormClosingEventArgs e)
 {
     if (MessageBox.Show("Want to exit from Application ?",  MessageBoxButtons.YesNo) == DialogResult.Yes)
     {
         //Environment.Exit(0);
         e.Cancel = false;
     }
     else
     {
         // your Code for Changes or anything you want to allow user changes etc.
         e.Cancel = true;    
     }    
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM