0

I have this Window Form and what i need is that when i close the form by pressing [x] in the window, i need to show a message.

This is not working: Any idea why?

private void ControlCom_Closing(object sender, FormClosingEventArgs e)
{ 
        MessageBox.Show("test");    
}

I just need to display the message on button x click. But this doesn't display the message box.

Shuld i add anything else there or something? I read there is a closereason but mybe it's not the case.

O. R. Mapper
  • 20,083
  • 9
  • 69
  • 114
MAL
  • 29
  • 1
  • 9

3 Answers3

3

By the way The Closing event is obsolete, it dates from .NET 1.x. and it was replaced in .NET 2.0 with the FormClosing event.

So better try :-

private void ControlCom_FormClosing(object sender, FormClosingEventArgs e)
{
    MessageBox.Show("test");    
}

For more details :-

http://msdn.microsoft.com/en-us/library/system.windows.forms.form.formclosing(v=vs.110).aspx

Neel
  • 11,625
  • 3
  • 43
  • 61
  • hmm thanks, i tried it, but still, no popup message, maybe there's something overwriting it – MAL Sep 09 '14 at 09:46
2
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    if (MessageBox.Show("Are you sure about closing the form?", "", MessageBoxButtons.YesNo) == DialogResult.No)
        e.Cancel = true;
}
TaW
  • 53,122
  • 8
  • 69
  • 111
Meysam Tolouee
  • 569
  • 3
  • 17
1

Typical scheme could be

private void Form1_FormClosing(object sender, FormClosingEventArgs e) {
  // If user (not system!) wants to close the form
  // but (s)he answered "no", do not close the form
  if (e.CloseReason == CloseReason.UserClosing)
    e.Cancel = MessageBox.Show(@"Do you really want to close the form?", 
                               Application.ProductName, 
                               MessageBoxButtons.YesNo) == DialogResult.No;
}
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215