[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

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

Posted on 2010-04-06
11
Medium Priority
?
298 Views
Last Modified: 2013-11-27
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
0
Comment
Question by:AndersBranderud
  • 5
  • 4
  • 2
11 Comments
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 29912409
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
 

Author Comment

by:AndersBranderud
ID: 29915933
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
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 total points
ID: 29916759
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 2000 total points
ID: 29916955
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
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 29919664
>>>> 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
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 29919932
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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 29920060
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
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 29920888
>>>> 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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 29921425
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
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 29923680
>>>> 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
 

Author Closing Comment

by:AndersBranderud
ID: 31711335
Thanks for an excellent reply!
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
No other job is as rewarding and demanding as building an iPhone app is. It is not really in the hands of the developer for the success of an iPhone app. Many factors operate jointly for every iOS application's success in the market.
Six Sigma Control Plans
Introduction to Processes
Suggested Courses

611 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question