Home > Net >  Close YesNo MessageBox C#
Close YesNo MessageBox C#

Time:09-21

It (MessageBox) checks if a file is saved or not

I want to close the Form when clicking Yes

and to return to the app when clicking No

I searched a lot in the documents and questions but didn't find an answer

I mean, there is "MessageBox.Show()", Isn't there "MessageBox.Close()" ??

That's what I have:

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            var message = "The File Is Not Saved\nDo You Want To Close?";
            var title = "File Not Saved";
            var buttons = MessageBoxButtons.YesNo;

            if (FSaved == false)
            {
                var res = MessageBox.Show(message, title, buttons);
                if (res == DialogResult.Yes)
                {
                    this.Close();
                }
                else if (res == DialogResult.No)
                {
                    return;
                }
            }
        }

CodePudding user response:

You have you cancel the close with the event args - e.

e.Cancel = true;

You also shouldn't call Close() from FormClosing as it is already on the way out.

  • Related