Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Close YesNo MessageBox C#

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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;
                }
            }
        }

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading