---
title: "MessageBox when closing form"  
description: "MessageBox when closing form"  
author: "Johnny Depp"  
published: 2013-11-11  
updated: 2013-11-12  
canonical: https://www.mindstick.com/forum/1713/messagebox-when-closing-form  
category: "c#"  
tags: ["c#"]  
reading_time: 2 minutes  

---

# MessageBox when closing form

I have a [button](https://www.mindstick.com/articles/63/how-to-add-button-in-datagridview-in-csharp-dot-net) [named](https://answers.mindstick.com/qa/44918/what-is-a-no-day-yet-named-motion) "Exit" to exit the [application](https://www.mindstick.com/articles/12824/calculator-application-in-android) and clicking on it i have to show the [message](https://www.mindstick.com/forum/12802/show-confirmation-message-yes-or-no-in-asp-dot-net) that "Are you sure you want to exit?".\
I don't know the syntax for it. any help will be appreciated.Thanks.

## Replies

### Reply by Johnny Depp

hello

### Reply by Anonymous User

Hi Johnny,\
have you tried this :-\

```
private void frmMain_FormClosing(object sender, FormClosingEventArgs e)    {    if (e.CloseReason == CloseReason.UserClosing)        {        dynamic mboxResult = MessageBox.Show("Are you sure you want to exit the form?", "Tranxit CCTV Job Creation Tool", MessageBoxButtons.YesNo);        if (mboxResult == DialogResult.No)            {            /* Cancel the Closing event from closing the form. */            e.Cancel = true;            }         else if (mboxResult == DialogResult.Yes)            {            /* Closing the form. */            e.Cancel = false;            Application.Exit();            }        }    }
```

\
This will solve your problem.\

### Reply by Anonymous User

Hi Johnny,\
have you tried this :-\

```
private void yourMethodName(){	if(MessageBox.Show("Are you sure you want to exit?", MessageBoxButtons.YesNo) == DialogResult.Yes)	{		this.Close();	}	else	{	}}
```

\
This will solve your problem.\


---

Original Source: https://www.mindstick.com/forum/1713/messagebox-when-closing-form

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
