Close form when click X symbol

Satyapriyanayak
Posted by Satyapriyanayak under C# category on | Points: 40 | Views : 854
In this Code Snippet we will know how to Close windows form when clicked X symbol. When user will click the X symbol message box will appears as Do you want to Close the form,if user select yes form will close and vice-versa. 

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

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (MessageBox.Show("Do you want to Close the form?", "Form close", MessageBoxButtons.YesNo) == DialogResult.No)
{
e.Cancel = true;
}
}
}
}

Comments or Responses

Login to post response