Microsoft Communities

Welcome to WindowsClient.net | Sign in | Join

Here are some frequently asked questions about Windows Forms and their answers.

Windows Forms FAQs

How do I display a confirmation dialog when the user closes the form?

You can listen to the Form's Closing event, where you can display a MessageBox as show below:

using System.ComponentModel;

private void Form1_Closing( object sender, CancelEventArgs e ) 
{ 
  string text = "Do you want to close the application?";
  string caption = "Close Application";
  if ( MessageBox.Show( text, caption, MessageBoxButtons.YesNo )
      == DialogResult.No ) 
    e.Cancel = true; 
}

Contributed from George Shepherd's Windows Forms FAQ



Page view counter