Friday, May 17, 2013

Form closing event with Yes/No message in winform(C#)


 
 

If you want to display a message like 'Do you want to close this form(Yes/No) when clicking of the form close button

then use this code

 

write this event in form contructor

 

public Form1()

{

this.Closing += new CancelEventHandler(Form1_Closing);

}

 

then write closing event of Form1

 

protected void Form1_Closing(object sender, CancelEventArgs e)

{

DialogResult dr =MessageBox.Show("Do u want to close(Y/N)","Warning",MessageBoxButtons.YesNo,MessageBoxIcon.Warning);

 

if (dr == DialogResult.Yes)

{

//do some thing

}

else

{

e.Cancel = true;

}