How we close the window in C# windows application?

Posted by Bageshkumarbagi under C# on 9/5/2013 | Points: 10 | Views : 1419 | Status : [Member] | Replies : 4
Hi,

How we close the window in C# windows application? my problem is when i closing the applicatin my program is still executing.

Thanks,
Bagesh Kumar Singh
Mark as answer if you have been satisfied



Responses

Posted by: Allemahesh on: 9/5/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
You can override OnFormClosing to do this.
Just be careful you don't do anything too unexpected, as clicking the 'X' to close is a well understood behavior.

protected override void OnFormClosing(FormClosingEventArgs e)

{
base.OnFormClosing(e);

if (e.CloseReason == CloseReason.WindowsShutDown) return;

// Confirm user wants to close
switch (MessageBox.Show(this, "Are you sure you want to close?", "Closing", MessageBoxButtons.YesNo))
{
case DialogResult.No:
e.Cancel = true;
break;
default:
break;
}
}


You can see the below link:- http://msdn.microsoft.com/en-us/library/system.windows.forms.form.onformclosing.aspx

Happy Coding.

Bageshkumarbagi, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bageshkumarbagi on: 9/5/2013 [Member] Starter | Points: 25

Up
0
Down
Thanks ..
its working...

Thanks,
Bagesh Kumar Singh
Mark as answer if you have been satisfied

Bageshkumarbagi, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Allemahesh on: 9/5/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
Dear Bagesh Kumar Singh,

Can you mark or click on as "Mark As Answer". So that it will help others.

Happy Coding.

Bageshkumarbagi, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Satyapriyanayak on: 9/5/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
 private void Form1_FormClosing(object sender, FormClosingEventArgs e)

{
DialogResult dr = MessageBox.Show("Are you sure you want to close this form?", "Confirmation of Form Close", MessageBoxButtons.YesNo);
if (dr == DialogResult.No) e.Cancel = true;
}


If this post helps you mark it as answer
Thanks

Bageshkumarbagi, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response