2

I have a Winform's application where inside a form I have a UserControl with 2 Radio buttons inside it. Both of the Radio buttons are subscribed to a single Event method.

On the Form I also have a Close button.

Inside my Event method I set some properties etc.

The issue I am having is that when I click on one of the radio buttons and then click on Close on the Form, it fires the RaddioButton_CheckedChanged Event again?

I unsubscribe and dispose the control but still can't seem to figure out what could be causing the multiple firing?

private void SubscribeToEvents()
{
     radioButton1.Click += RaddioButton_CheckedChanged;
     radioButton2.Click += RaddioButton_CheckedChanged;
     UnSubscribeEvents;
}

private void RaddioButton_CheckedChanged(object sender, EventArgs e)
{
     if (!(sender is RadioButton rb)) return;
        
     if (rb.Checked)
     {
         switch (rb.Name)
         {
             case raddioButton1:
                  //Set some properties
                  break;
             case raddioButton2:
                  //Set some properties
                  break;
          }
     }
  }

private void UnSubscribeEvents(object sender, EventArgs e)
{
     radioButton1.Click -= RaddioButton_CheckedChanged;
     radioButton2.Click -= RaddioButton_CheckedChanged;
}

protected override void Dispose(bool disposing)
{
    if (disposing && (components != null))
    {
        components.Dispose();
    }

    Disposing?.Invoke(this, new EventArgs());

    base.Dispose(disposing);
}
KJSR
  • 1,679
  • 6
  • 28
  • 51
  • related: https://stackoverflow.com/a/11494020/1132334 what does the call stack look like in the event handler on/after closing? – Cee McSharpface Jun 29 '20 at 16:16
  • You need to create you own OnClose Event that does the UnSubscribe. Also should be in the Dispose method. Where are you calling the UnSubscribeEvents? When yo uhave the UnSubscribeEvents you need to register with += to be called. – jdweng Jun 29 '20 at 16:33
  • Don't invoke an event in the `Dispose` override. Override an existing method, as `OnFormClosing`. – Jimi Jun 29 '20 at 16:37
  • Does this compile? especially with `Disposing += UnSubscribeEvents;`? `Disposing` is a property not an event. Maybe you mean `Disposed`. Also, better to handle the `CheckedChanged` event to handle the changes made by the arrow keys. I suggest removing everything except the `RaddioButton_CheckedChanged` handler, and subscribe to it in the constructor `radioButton1.CheckedChanged += (s, e) => RaddioButton_CheckedChanged(s, e);` and `RaddioButton_CheckedChanged += (s, e) => RadioButtonCheckedChanged(s, e);`. –  Jun 29 '20 at 20:49
  • Also, make sure the `Click` event of the close `Button` is not subscribed to the same event by mistake. –  Jun 29 '20 at 20:54
  • 1
    JQSOFT I have updated my question w.r.t `Disposing`. – KJSR Jun 29 '20 at 21:06
  • 1
    Thank you. Maybe also from `Dispose`. :)... Correcting the last line in my last comment. Its: `radioButton2.CheckedChanged += (s, e) => RadioButton_CheckedChanged(s, e);` Sorry for the typo. –  Jun 29 '20 at 21:26
  • Ok found the issue, it's `.CheckedChanged` event which is causing the issue. If I change it to `.Click` it works fine? `.CheckedChanged` seems to remain in in memory...is this correct? Sounds like it is not getting disposed? – KJSR Jun 29 '20 at 23:25
  • `CheckedChanged` fires twice to report `radioButton1` is unchecked, and `radioButton2` is checked, and vise versa. –  Jun 30 '20 at 17:13

0 Answers0