I am implementing a single event handler to handle CheckedChanged event for all radioButtons on the Panel. ( Event handler for groupBox with radioButtons in C#, Which Radio button in the group is checked? )
I was waiting that when I switch a radioButton there are 2 events: one called from unchecked radioButton, and one called from a checked one.
public Form1()
{
Panel createToolPanel()
{
radioButton1.CheckedChanged += toolPanel_CheckedChanged;
radioButton2.CheckedChanged += toolPanel_CheckedChanged;
radioButton3.CheckedChanged += toolPanel_CheckedChanged;
// ...
}
void toolPanel_CheckedChanged (object sender, EventArgs e)
{
var rb = (RadioButton)sender;
if (rb.Checked == false)
return;
switch(rb.Name)
{
// Do stuff
}
}
But instead every time I switch any of radioButtons the sender is always the first radioButton.
sender: radioButton1
sender: radioButton1
What is more important, when I switch from 2nd to 3rd radioButton (or from 3rd to 2nd), no event occurs at all. I don't understand why is it happening..
I also tried to change adding event handler code like this:
radioButton1.CheckedChanged += new EventHandler(toolPanel_CheckedChanged);
radioButton2.CheckedChanged += new EventHandler(toolPanel_CheckedChanged);
radioButton3.CheckedChanged += new EventHandler(toolPanel_CheckedChanged);
But it does not helped.