The Cancel button in the Yes / No / Cancel messages window in the FormClosing method

I posted the Yes / No / Cancel Messagebox in the FormClosing Method of my form. and now this is the text in the message field: do you want to save the data?

I am not a professional and I don’t know how to handle if the user clicked Cancel ? Exactly, the result of clicking the "Cancel" button should be forms remains open.
How to prevent my form from closing in the 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 fill out my code!
thanks

+3
source share
4

FormClosing , , True , , IIRC.

EDIT: ,

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

. .

+11

- :

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

, "" "", , "".

+6

, , , , . , .

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); 
}

, , # . .:)

+6

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

.

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

Source: https://habr.com/ru/post/1714734/


All Articles