-1

How can I delete DialogResult object ? I am using it as a confirmation for clearing form (removing all controls and reinitializing of controls). The problem is that when I hit yes it recreates the second DialogResult, then third one, then fourth, etc.

SO when user hits yes, I would like to remove this DialogResult. Is there a way?

Code here:

private void GUI_DCP_FormClosing(object sender, FormClosingEventArgs e)
    {

        var confirmation_text = "If you click 'Yes', all information will be discarded and form reset. If you want to save the input click 'No' and then 'Save'";

        DialogResult dialogResult = MessageBox.Show(confirmation_text, "WARNING", MessageBoxButtons.YesNo);
        if (dialogResult == DialogResult.Yes)
        {
            this.Hide();
            e.Cancel = true; // this cancels the close event.
            this.Controls.Clear();
            this.InitializeComponent();
            this.Height = 278;
            this.Width = 341;
        }
        else
        {
            e.Cancel = true;
        }
    }
marhyno
  • 677
  • 1
  • 8
  • 20
  • 1
    Can you show the code that triggers this behavior?. Without it your question is unanswerable because there is no [mcve] – Steve Aug 27 '17 at 21:04
  • @Steve added code – marhyno Aug 27 '17 at 21:07
  • So when you click yes this code is recalled again and a second messagebox appears immediately? Just to check. What happen if you comment out the two lines with this.Controls.Clear and this.InitializeComponent? Does the infinite loop stops? – Steve Aug 27 '17 at 21:11
  • @Steve the first time I hit yes, everything is okay, the form hides and controls are reset (because I am also creating dynamically new controls so when I hit yes I resets like at the beginning of the program). But the second time I show this form and hit X to close this form now two Confirmation boxes appears. I cannot remove Controls.Clear and InitializeCompoments because this is what I need for reseting the form to the pure first state, but DialogResult doesnt belong to Controls so its not deleted and recretead. Just new one is created. – marhyno Aug 27 '17 at 21:15
  • DialogResult is a enum, it is not a control. It is declared on the stack of your event handler, so it has nothing to do with the behaviour you experiment. Something in your code is causing a second call to Form_Closing event handler – Steve Aug 27 '17 at 21:17

1 Answers1

2

When you recall the InitializeComponent you are not only adding your controls ex-novo, but you are also re-adding all the event handlers INCLUDING the event handlers linked to the form itself (the FormClosing event and others if present).

In this way, the first call seems to go well, but it register the FormClosing event handler a second time. So, when you trigger the action that enters the FormClosing event handler, it is called two times and, in the same call, it will be registered again, and the next time the call is made three times and so on.

The simplest thing to stop this behavior is removing the FormClosing event handler before calling InitializeComponent

if (dialogResult == DialogResult.Yes)
{
    this.Hide();
    e.Cancel = true; 

    // This removes the FormClosing event handler.
    // If other event handlers are present you should remove them also.
    this.FormClosing -= GUI_DCP_FormClosing;   

    this.Controls.Clear();
    this.InitializeComponent();
    this.Height = 278;
    this.Width = 341;

    // Do not forget to reshow your hidden form now.
    this.Show();
}

But I really don't think that it is a good idea to clear the controls collection and call again the InitializeComponent.
Apart from the fact that if you have many event handlers you should remove them all before calling InitializeComponent, this approach will hit your performances and memory footprint.

Instead, I would prepare a list of all dynamically added controls and remove them one by one. Second I would write a procedure to reset the fixed controls to their initial value without removing them from the controls collection and readding them again and again.

Steve
  • 213,761
  • 22
  • 232
  • 286
  • thanks a lot :), yes your note about creating list of controls is good. – marhyno Aug 27 '17 at 22:11
  • Well, that was an interesting situation to find out. I think that if you update your title and explain better the behavior of your initial code then this question will be searched and found by many future readers. (as I have said DialogResult had nothing to do with the problem) – Steve Aug 27 '17 at 22:27