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);
}