Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
1.33/5 (2 votes)
See more:
in Notepad example click to closeing button asked to do you want to save or not diasplay save,dontsave,cancel options.how to write code for displaying this three options
Posted

As Eduard has said this is usually done from the Form.Closing EventHandler. As an alternative to MessageBoxButtons.YesNo as he suggests, there is MessageBoxButtons.YesNoCancel, and all you then have to do is word the message appropriately.

If you want more ideas, then search the articles here on The Code Project for either notepad or text editor. There are lots of examples, have a look at how they handle this event. In particular look for variables named 'dirty' or 'isDirty' (or something like that) which is how they keep track of whether the text has changed.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 14-Mar-11 16:38pm    
This is a correct improvement. YesNo is certainly not enough. I "down-voted" Eduard by my 4, and 5 for you.
--SA
Create an event handler for the FormClosing event

The event argument contains a Cancel Property

C#
private void Main_FormClosing(object sender, FormClosingEventArgs e)
{
    if (MessageBox.Show("Are you sure you want to close the application?",
        "Close?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
    {
        e.Cancel = true;
    }
}


Now you only have to develop a new form with three buttons, that returns a DialogResult.Yes, DialogResult.No and a DialogResult.Cancel.

Replace the MessageBox.Show in the above code with code that calls your custom window.
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 14-Mar-11 16:37pm    
Stop using auto-generated method names like "Main_FormClosing"! This is a violation of Microsoft naming conventions. Also, it's the best not to auto-generate any events in Designer. Use anonymous delegates. My 4.
--SA
Toli Cuturicu 14-Mar-11 18:29pm    
Then, why does Microsoft violate its own naming conventions?
Sergey Alexandrovich Kryukov 14-Mar-11 19:14pm    
This is the point. Microsoft certainly violates it, also many other rules as reported by FxCop.
I can understand it. Auto-generated code. Who wants to bother while generating code, also, semantic is unknown. It's the developer who is supposed to rename or avoid auto-generation and who knows the semantic. Come to thing about, this violation is handy: find all underscore and than rename semantically. Convenient! From this point, you can immediately tell auto-generated unattended code from the code you paid enough attention to and finalized. Suggest you use this as a tip :-)
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900