Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Message when closing form

Posted on 2011-05-13
7
Medium Priority
?
387 Views
Last Modified: 2013-12-16
Hi,
When I click on the mainmenu-item Exit of my application a message appears:

        private void mnuExit_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you sure you want to Exit?", "Exit",
                MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                this.Close();
            }
        }

But what i want is that same message when a user clicks on the x on the form
to close the application.

Who knows the answer and is willing to help me?

Peter
0
Comment
Question by:peterkiers
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 35752471
There is a FormClosing event.  If you set e.Cancel = true in that then the form won't close.
0
 
LVL 1

Author Comment

by:peterkiers
ID: 35752529
I don't understand.

What I want is not only to show the message when the user clicks on menu-item Exit
but also show the message when the user clicks on the x-button of the titel bar.

P.
0
 
LVL 7

Expert Comment

by:Gene_Cyp
ID: 35752532
Take your existing code and insert it in a method:

Step 1:

private void ConfirmBeforeClosing(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you sure you want to Exit?", "Exit",
                MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                this.Close();
            }
        }

Step 2: Ensure you trap the form closing event, when you click on the Events tab in your Visual Studio (the one with the thudner icon) - make sure you have the form selected - there is a FormClosing event trap there.

Add the following:

OnFormClosing

This will generate an event for you in your code.

Step 3: Add your method inside both events.

private void mnuExit_Click(object sender, EventArgs e)
        {
            ConfirmBeforeClosing(sender, e);
        }
private void OnFormClosing(object sender, EventArgs e)
        {
            ConfirmBeforeClosing(sender, e);
        }
0
Restore individual SQL databases with ease

Veeam Explorer for Microsoft SQL Server delivers an easy-to-use, wizard-driven interface for restoring your databases from a backup. No expert SQL background required. Web interface provides a complete view of all available SQL databases to simplify the recovery of lost database

 
LVL 1

Author Comment

by:peterkiers
ID: 35752693
This is what I wanted but it doesn't work right.
With the menu-item Exit i get the message but when I press ok to close the app it won't close.
and when I press the x-button and i press the cancel button of the message the application closes anyway.

Peter
0
 
LVL 45

Accepted Solution

by:
AndyAinscow earned 2000 total points
ID: 35752789
As I said earlier - use the FormClosing event:

        private void FormClosing(object sender, FormClosingEventArgs e)
        {
            if (MessageBox.Show("Are you sure you want to Exit?", "Exit",
                MessageBoxButtons.OKCancel) != DialogResult.OK)    e.Cancel = true;
        }


        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
0
 
LVL 7

Expert Comment

by:Gene_Cyp
ID: 35752794
Adjust your method:


private void ConfirmBeforeClosing(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you sure you want to Exit?", "Exit",
                MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                this.Close();
            }
           else
               e.Cancel = true;
        }


If this.close() isn't working for you, cast the sender into a Form object and close the sender.

0
 
LVL 12

Expert Comment

by:Mohamed Abowarda
ID: 35752895

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace App
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
            this.FormClosing += new FormClosingEventHandler(Form1_FormClosing);
        }

        void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (MessageBox.Show("Are you sure you want to Exit?", "Exit",
                MessageBoxButtons.OKCancel) != DialogResult.OK)
            {
                e.Cancel = true;
            }
        }
    }
}

Open in new window

0

Featured Post

Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…
Suggested Courses
Course of the Month14 days, 21 hours left to enroll

577 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