简体   繁体   中英

Troubles with onFormClosing in c#

I'm trying to implement some code that asks if the user wants to exit the application I've made.

It's in c# and is a windows form application.

I've had very little sleep this week and can't seem to get my head around the onFormClosing event. Could some please give me the exact code I should use to have code executed when the user clicks on the close button (the 'x' in the top right).

Please find it in your heart to help a sleep deprived moron.

Well, the event is called FormClosing and is cancellable. Subscribe to it, do your stuff and let the user close their form. This event is fired if the "x" button is used or if you close the form yourself.

You can subscribe to it in the designer by highlighting the form and looking in the events tab of the properties window, as SLaks says, then double-click it. You don't need to do anything special to cope with the "x" button.

Double-click the form's FormClosed event in the events tab of the Properties window in the designer.

The FormClos event allows you to prevent the form from closing by setting e.Cancel = true . 事件使您可以通过设置e.Cancel = true来防止表单关闭。

The easiest way is to activate the form in the designer and find the event FormClosing in the properties windows and then just double click the event.

Then just do the following:

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        if (e.CloseReason == CloseReason.UserClosing)
        {
            var result = MessageBox.Show("Are you sure you want to exit?", "Exit", MessageBoxButtons.YesNo);
            if (result != System.Windows.Forms.DialogResult.Yes)
            {
                e.Cancel = true;
            }
        }
    }

If you do not specify that the reason has to be UserClosing, it will stop windows from shutting down if you do not exit the program first which is not a good practice.

Make derive your form fromf the System.Windows.Forms.Form and put this override:

protected override void OnFormClosing(CancelEventArgs e)
{
    if (bWrongClose)
    {
        bWrongClose = false;
        e.Cancel = true; // this blocks the `Form` from closing
    }
    base.OnFormClosing(e);
}
public Form1()
    {
        InitializeComponent();
        this.FormClosing += new FormClosingEventHandler(Form1_FormClosing);
    }

    void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        if (MessageBox.Show("Are you sure that you wan't to close this app", "Question", MessageBoxButtons.OKCancel) == DialogResult.Cancel)
            e.Cancel = true;
    }

I hope this helps

You can add event handler manually. Example to add event handler in constructor:

public frmMain()
{
   InitializeComponent();
   FormClosing += frmMain_FormClosing;
}

private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
{
    //your code
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM