2

I have to group RadioButtons present in different Panels as a single group. Sa,y for example, I have a RadioButton named radBtn1 on panel1 and radBtn2 and radBtn2 on panel2. Now I want to group all these radio buttons as a single group so that only one RadioButton is checked at a time.

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • I would argue that this could be indicative of poor UI\UX design. However, there is no _automated_ mechanism for getting the radio buttons to interact with each other in this way. – Martin Sep 20 '19 at 09:54
  • You will likely have to write some code to manually uncheck the other radio buttons when the `CheckedChanged` event fires on each radio button – Martin Sep 20 '19 at 09:54

1 Answers1

1

We can try uncheck RadioButtons manually. Let's assign RadioButtons_CheckedChanged event handler for all RadioButtons of interest:

private void RadioButtons_CheckedChanged(object sender, EventArgs e) {
  // If we have a RadioButton Checked 
  if (sender is RadioButton current && current.Checked) {
    // We should remove Check from the rest RadioButtons:
    var rest = new Control[] { panel1, panel2} 
      .SelectMany(ctrl => ctrl             // all radiobuttons on panel1, panel2
         .Controls
         .OfType<RadioButton>())
      .Where(button => button != current); // don't touch current it should stay checked

    foreach (var button in rest)
      button.Checked = false;
  }
}
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215