pikausp
pikausp

Reputation: 1192

C# Winform radiobutton state switching

I need to link radio buttons, as they are not in the same container, so I wanted to add them to a list and handle their CheckedChanged events

private List<RadioButton> Buttons = new List<RadioButton>();

public void Add( RadioButton button ) {
    button.CheckedChanged += button_CheckedChanged;
    Buttons.Add( button );
}

private void button_CheckedChanged( object sender, EventArgs e ) {
   var button = sender as RadioButton;
   if( button == null || button.Checked == false )
       return;
   foreach( var btn in Buttons ) {
       if( btn != button )
           button.Checked = false;
   }
}

When I click a radio button it stays unchecked, any idea why?

Upvotes: 0

Views: 1370

Answers (2)

Burdell
Burdell

Reputation: 60

Possibly a silly question, but you have the AutoCheck property set to true, correct? If it is set to false then the radiobutton will not be checked when you click on it.

button.AutoCheck = true;

Or you can change it in the property window in the designer.

Upvotes: 0

Steve Coleman
Steve Coleman

Reputation: 2027

shouldn't this be

  foreach( var btn in Buttons ) {
       if( btn != button )
           btn.Checked = false;
   }

Upvotes: 4

Related Questions