This is a migrated thread and some comments may be shown as answers.

cancel form close with FormClosing

4 Answers 396 Views
Form
This is a migrated thread and some comments may be shown as answers.
Tino
Top achievements
Rank 1
Tino asked on 08 May 2018, 01:39 AM

I think I read how to do this but searching 'FormClosing' in this forum didn't find anything much.

I simply need to cancel the closing of a form (after user dialog confirmation). Is this the correct approach?

private void OnFormClosing(object sender, FormClosingEventArgs e)
{
    var dlg = new DlgConfirmUnsavedForm();
    var result = dlg.ShowDialog();
    if (result == DialogResult.Cancel)
    {
        e.Cancel = true;
        OnClosing(e);
        return; //??
    }
    else if (result == DialogResult.Yes)
    {
        SaveForm();
    }
     
    ...
}

 

By the way, post editing is terrible!

4 Answers, 1 is accepted

Sort by
0
Tino
Top achievements
Rank 1
answered on 08 May 2018, 01:43 AM
I needed to add that my forms are MDI child forms.
0
Dimitar
Telerik team
answered on 08 May 2018, 07:36 AM
Hello Tino,

This is the correct approach, consider always calling the base logic:
protected override void OnClosing(CancelEventArgs e)
{
    if (RadMessageBox.Show("Confirm Close", "Close" , MessageBoxButtons.OKCancel) != DialogResult.OK)
    {
        e.Cancel = true;
    }
    base.OnClosing(e);
 
}

Let me know if I can assist you further.

Regards,
Dimitar
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
Tino
Top achievements
Rank 1
answered on 08 May 2018, 10:30 PM

Thanks for the response, I've added a call to OnClosing() before the function returns.

I guess I forgot to mention the important thing, that it isn't working for me... the form still closes even with e.Cancel set to true.

I also tested using just your code, nothing else.

 

0
Dimitar
Telerik team
answered on 09 May 2018, 09:24 AM
Hi Tino,

This works on my side. I have attached my test project. 

Let me know if I can assist you further.

Regards,
Dimitar
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
Form
Asked by
Tino
Top achievements
Rank 1
Answers by
Tino
Top achievements
Rank 1
Dimitar
Telerik team
Share this question
or