“e.Cancel ” in formclosing Event

后端 未结 4 1631
谎友^
谎友^ 2021-02-04 11:21

When using the FormClosing event, why does the code e.Cancel = true; work, but new CancelEventArgs().Cancel = true; does not work?

4条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-04 12:08

    This code surely work just check it

    protected override void OnFormClosing(FormClosingEventArgs e)
            {            
                base.OnFormClosing(e);
                if (PreClosingConfirmation() == System.Windows.Forms.DialogResult.Yes)
                {
                    Dispose(true);
                    Application.Exit();
                }
                else
                {
                    e.Cancel = true;
                }
            }
    
            private DialogResult PreClosingConfirmation()
            {
                DialogResult res = System.Windows.Forms.MessageBox.Show(" Do you want to quit?          ", "Quit...", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                return res;
            }
    

    Happy Coding

提交回复
热议问题