>//Example Form1.cs >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 bool UserClosing { get; set; } > > public Form1() > { > InitializeComponent(); > UserClosing = false; > this.buttonExit.Click += new EventHandler(buttonExit_Click); > this.FormClosing += new FormClosingEventHandler(Form1_FormClosing); > } > > void buttonExit_Click(object sender, EventArgs e) > { > UserClosing = true; > this.Close(); > } > > void Form1_FormClosing(object sender, FormClosingEventArgs e) > { > switch (e.CloseReason) > { > case CloseReason.ApplicationExitCall: > break; > case CloseReason.FormOwnerClosing: > break; > case CloseReason.MdiFormClosing: > break; > case CloseReason.None: > break; > case CloseReason.TaskManagerClosing: > break; > case CloseReason.UserClosing: > if (UserClosing) > { > MessageBox.Show("The user clicked the Close Button"); > } > else > { > MessageBox.Show("The user clicked the X"); > } > break; > case CloseReason.WindowsShutDown: > break; > default: > break; > } > } > } >} >>