How do I show a confirm box when the user has pressed the close button in a Windows form, Virtual C++ 2008 Express

How do I show a confirm box when the user has pressed the close button in a Windows form in Virtual C++ 2008 Express Edition?

I know how to create a message box with "Yes" and "No"-alternatives. What I want to know is how to catch and process the event generated when the close-button is pressed on a Windows Form.

Thanks in advance!

Anders Branderud
http://bloganders.blogspot.com
AndersBranderudAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

itsmeandnobodyelseCommented:
I assume you were working with managed C++ and Windows Forms?

If so, I can't tell you the details but only what you could try:

Go to the resource editor (where you can design your form) and right-click on the close button. You should get a menu where you can choose to add an event handler. Further you should be able to specifiy the click event (OnClickedButton or similar) and let the wizard generate the function template in your code.

>>>> How do I show a confirm box when the user has pressed the close button

If you can create a messagebox with Yes/No, you can create same way a messagebox with ok only. Call it in the OnClickedButtonClose event handler.
0
AndersBranderudAuthor Commented:
Thanks for your reply!

"I assume you were working with managed C++ and Windows Forms?"
Yes!

The problem is that it is not possible to click on the close-button.
It is possible to click on the Window form. When I click on the window, the method "System:: Void form1_load (System::Object^, System::EventArgs^e)" appears.

"If you can create a messagebox with Yes/No, you can create same way a messagebox with ok only. Call it in the OnClickedButtonClose event handler."
I want a yes/no-button, so it is not a problem.

Anders Branderud
http://bloganders.blogspot.com
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
Use the FormClosing() event:
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.formclosing.aspx

Based on the result of your MessageBox, you can cancel the closure of a form by setting the Cancel property of the FormClosingEventArgs to true.

*I'm not a C++ programmer, but it should look something like:
private: System::Void Form1_FormClosing(System::Object^ sender, System::Windows::Forms::FormClosingEventArgs^ e) {
    String* message = S"Are you sure to want to quit??";
    String* caption = S"Quitting...";
    MessageBoxButtons buttons = MessageBoxButtons::YesNo;
    System::Windows::Forms::DialogResult result;

    result = MessageBox::Show(this, message, caption, buttons);
    if (result == DialogResult::No) {
        e->Cancel = true;
    }
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

Mike TomlinsonMiddle School Assistant TeacherCommented:
You click on the Form, then in the Properties Pane, click on the "Lightning Bolt" Icon to get a list of events.  Scroll down to FormClosing() and double click it to get a method stub.  Paste your messagebox code in there and set "e-->Cancel" appropriately.
0
itsmeandnobodyelseCommented:
>>>> The problem is that it is not possible to click on the close-button.

If you right-click on the button you should be able to get the properties (that is where you can enter the text 'Close' to the button. If you got this choose the little flash button at the top of the properties window. There you can choose the event to be handled. Choose 'Click' to get an OnClick handler added to your code.
0
itsmeandnobodyelseCommented:
Idle_Mind, sorry for repeating some of your advice. I never used Windows Forms myself and needed a test project to find out how it works. Unfortunately, I didn't refresh the question here before posting ;-)
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
Pretty sure by "the close button in a Windows form" he's talking about the "X" in the top right of the form...  =)
(this would also explain why he can't click on it)

See my comments on how to handle the FormClosing() event of the form.
0
itsmeandnobodyelseCommented:
>>>> Pretty sure by "the close button in a Windows form" he's talking about the "X" in the top right of the form...  =)

I was used on MFC forms always having an own ok and cancel button... But, you surely are right ;-)
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
Thank goodness I never had to learn MFC!

I'm much happier in the managed world...you can keep your low level pointers and manual memory management to yourself... ;)
0
itsmeandnobodyelseCommented:
>>>> you can keep your low level pointers and manual memory management to yourself... ;)

MFC is C++ class library, so no need for pointers (mostly) ;-)

And you can use STL containers, so there isn't manual memory management at all - though we dinosaurs of course were capable to do it manually if we want ;-)

0
AndersBranderudAuthor Commented:
Thanks for an excellent reply!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.