Question

I put a Yes/No/Cancel Messagebox in FormClosing Method of my form. and now this is Message box text: Do You Want to Save Data?

I am not a profesional and not know how to handle if user clicked Cancel Button? Exactly the result of clicking on Cancel Button must be The form remain open.
How to prevent Closing my form in FormClosing method?

I wrote So far: ;)

DialogResult dr = MessageBoxFarsi.Show("Do You Want to Save Data?","",MessageBoxButtons.YesNoCancel,MessageBoxIcon.Warning);

//...
else if (dr == DialogResult.Cancel)
{
    ???
}

Please Help me to complete my code!
Thanks

Was it helpful?

Solution

FormClosing has a Boolean parameter which, if set to True when the function returns, will cancel closing the form, IIRC.

EDIT: For example,

private void Form1_FormClosing(Object sender, FormClosingEventArgs e) {
    // Set e.Cancel to Boolean true to cancel closing the form
}

See here.

OTHER TIPS

Actually I think you are missing Event handler, oh you can not turn to that even without an even handler. You must add an event with an event handler like this.

private void myform_Closing(object sender, FormClosingEventArgs e) 
{
    DialogResult dr = MessageBoxFarsi.Show("Do You Want to Save Data?","",MessageBoxButtons.YesNoCancel,MessageBoxIcon.Warning)

    if (dr == DialogResult.Cancel) 
    {
        e.Cancel = true;
        return;
    }
    else if (dr == DialogResult.Yes)
    {
        //TODO: Save
    }
}

//now add a default constructor 
public myform()  // here use your form name.
{
    this.FormClosing += new FormClosingEventHandler(myform_Closing); 
}

Forgive me if there are some wrongs spellings in this code because i didn't write it in c# and copy paste here. I just wrote it in here. :)

You could have something like the following:

if(dr == DialogResult.Cancel)
{
    e.Cancel = true;
}
else if(dr == DialogResult.Yes)
{
    //Save the data
}

The above code should only close the form if you choose yes or no, and will save data when you choose yes.

you should try this function

public DialogResult msgClose(string msg)
{
     return MessageBox.Show(msg, "Close", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
}

and used like this.

private void frm_FormClosing(object sender, FormClosingEventArgs e)
{
     if (conn.msgClose("Application close?") == DialogResult.No)
         e.Cancel = true;
     else
     {
         this.Close();
     }
}

You can try this:

if (MessageBox.Show("Are you sure you want to quit?", "Attention!!", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning) == DialogResult.Yes)
{
   //this block will be executed only when Yes is selected
   MessageBox.Show("Data Deleted", "Done", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
else
{
  //this block will be executed when No/Cancel is selected
  //the effect of selecting No/Cancel is same in MessageBox (particularly in this event)
}

If needed same you can do for the No and Cancel Button click using DialogResult class

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top