1

I am developing a WinForms (.NET 4.7.2) application with Telerik WinForm (2019.2.618.40) controls.

On click of Close Box [x] icon on top tight of the window, I would like to ask for user confirmation and if YES only then close the app.

Following code works well, it closes the window, but it is not closing process (I can still see the app in Task Manager).

I believe I need to call Application.Exit(); to kill the app process. But When I call that Closing Event is fired twice and I get confirmation window twice and gets following error

System.InvalidOperationException: 'Collection was modified; enumeration operation may not execute.'

How do I correct my closing event so that I can ask for user confirmation and close the windows as well as exit application cleanly from Task manager as well?

My Base Form

public class BaseForm : Telerik.WinControls.UI.RadForm
    {
        public BaseForm()
        {
           this.FormClosing += new FormClosingEventHandler(Form_Closing);
        }
        public void Form_Closing(object sender, System.ComponentModel.CancelEventArgs e)
    {
        RadMessageBox.SetThemeName("Material");
        DialogResult dialogResult = RadMessageBox.Show(
                 this,
                 "Are you sure, you want to exit out of the application? Any unsaved data will be lost!",
                 "Application Name",
                 MessageBoxButtons.YesNo,
                 RadMessageIcon.Question,
                 MessageBoxDefaultButton.Button2,
                 RightToLeft.No);
        if (dialogResult == DialogResult.Yes)
        {
            e.Cancel = false;
            //Application.Exit();
        }
        else if (dialogResult == DialogResult.No)
        {
            e.Cancel = true;
        }
        base.OnClosing(e);
    }


     //....
     //....
     //....
    }

Update


instead of FormClosign Event if I use FormClosed event, Application.Exit() works fine, but on confirmation message if I click NO it is still closing app.

this.FormClosed += new FormClosedEventHandler(App_kill);

App Kill Method (Cannot handle DialogResult.No response)

private void App_kill(object sender, FormClosedEventArgs e)
        {

            if (e.CloseReason == CloseReason.UserClosing)
            {
                RadMessageBox.SetThemeName("Material");
                DialogResult dialogResult = RadMessageBox.Show(
                         this,
                         "Are you sure, you want to exit out of the application? Any unsaved data will be lost!",
                         "Close Application",
                         MessageBoxButtons.YesNo,
                         RadMessageIcon.Question,
                         MessageBoxDefaultButton.Button2,
                         RightToLeft.No);
                if (dialogResult == DialogResult.Yes)
                {
                    Application.Exit();
                }

                if (dialogResult == DialogResult.No)
                {
                    //e.Cancel = true;
                }
            }
        }
HaBo
  • 13,999
  • 36
  • 114
  • 206
  • What happens if you comment out the `RadMessageBox.Show`, `dialogResult` logic and _then_ try to close the app. Does it kill the process? (just in case Telerik is doing something odd) –  Sep 05 '19 at 09:30
  • I see your class is named `BaseForm`, are you deriving a child class from it that represents your application window? –  Sep 05 '19 at 09:35
  • @MickyD, yes if I remove RadMessageBox.Show it will kill process fine. Yes all my Forms are derived from BaseForm – HaBo Sep 05 '19 at 09:48
  • Perhaps a bug with Telerik if used during the potential closure of a Window. Perhaps it attaches to it and does some flunky modal processing? You could use plain ol' WinForms `MessageBox.Show`, it may not look as pretty but it has equivalent funcationality for YesNo, Question etc. I would probably lodge a bug report against Telerik –  Sep 05 '19 at 09:52
  • @MickyD same problem with MessageBox.Show, Problem here is this event is wired to FormClosing event. And when we call Application.Exit() it again calling FormClosing event, I think on Application.Exit() I should prevent triggering of FormClosing – HaBo Sep 05 '19 at 10:59
  • Regarding your edit, you **cannot** prevent closure if you do your processing in `FormClosed` –  Sep 05 '19 at 11:35
  • _"same problem with MessageBox.Show"_ - unable to reproduce. I suspect you have subscribed multiply to the `FormClosing` event. This works **perfectly**: `private void Form1_FormClosing(object s, FormClosingEventArgs e) { if (MessageBox.Show("Really close?", Application.ProductName, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.No) { e.Cancel = true; } }`. Its not necessary to call either `Application.Exit` nor `Environment.Exit` as that is a hack –  Sep 05 '19 at 11:43
  • @MickyD can you please elaborate? I suspect you have subscribed multiply – HaBo Sep 05 '19 at 11:48
  • In your constructor you subscribe to FormClosing, make sure you didn’t do it from the Designer too –  Sep 05 '19 at 13:24
  • Not sure who marked it for close. If they are confident, I can share my code and challenge them to share solution. – HaBo Sep 05 '19 at 13:35

1 Answers1

0

Thanks to nihique posted here https://stackoverflow.com/a/13459878/942855 and also pointed by MickyD in this question comments.

Apparently there is no issue with my closing event. When I redirect from Form-A to Form-B, I had to do the following wiring and then it worked like a charm

 var newform = new myNewForm();
  newform.Closed += (s, args) => this.Close();
  this.Hide();
  newform.Show();
HaBo
  • 13,999
  • 36
  • 114
  • 206
  • Probably would have been useful to mention you had two forms. Glad you got it going nonetheless –  Sep 05 '19 at 14:20