1

i want to find an index of a selected RadioButton in RadioGroup. I attached next single method to each RadioButton in the group:

private void radio_button_CheckedChanged(object sender, EventArgs e){
    if (sender.GetType() != typeof(RadioButton)) return;
    if (((RadioButton)sender).Checked){
        int ndx = my_radio_group.Controls.IndexOf((Control)sender);
        // change something based on the ndx
    }
}

It is important to me that lower radioButton must have lower index, starting from zero. And seems it is working, but i am not sure if this is a good solution. Maybe there is more betufilul way to do the same.

  • Something like this http://stackoverflow.com/questions/17082551/getting-the-index-of-the-selected-radiobutton-in-a-group – Barry O'Kane Apr 21 '16 at 14:02
  • What are you targetting: Winforms, WPF, ASP..? __Always__ tag your question correctly. – TaW Apr 21 '16 at 14:05
  • I've always preferred to use the radio button `value` property rather than its index in the group. This allows you to change the order, insert new items and doesn't require to change code after the fact (other than to handle the new options' logic). – Max Sorin Apr 21 '16 at 14:07
  • @TaW winforms, edited tags – Vladimir Tsykunov Apr 21 '16 at 14:09

3 Answers3

2

This will give you the Checked RadioButton:

private void radioButtons_CheckedChanged(object sender, EventArgs e)
{
    RadioButton rb = sender as RadioButton;
    if (rb.Checked)
    {
        Console.WriteLine(rb.Text);
    }
}

Any indices in the Controls collection of its Parent are highly volatile.

You could access it like this: rb.Parent.Controls.IndexOf(rb) If you want a relatively stable ID besides the Name and the Text you can put it in the Tag.

Obviously you need to hook up this event to all the RadionButtons in the group.

No type checks are really necessary (or imo recommended,) as only a RadioButton can (or rather: must ever) trigger this event.

TaW
  • 53,122
  • 8
  • 69
  • 111
1

To obtain index ideally you want to have controls arranged as collection. If you can add controls from code behind than that's as easy as

List<RadionButton> _buttons = new List<RadioButton>();

_buttons.Add(new RadioButton() { ... });    
_buttons.Add(new RadioButton() { ... });    
...

If you want to use form designed, then perhaps creating this list in form constructor is an alternative:

List<RadioButtons> _list = new List<RadioButton>();

public Form1()
{
    InitializeComponent();
    _list.Add(radioButton1);
    _list.Add(radioButton2);
    ...
}

Then the actual task to obtain index is as simple as:

void radioButton_CheckedChanged(object sender, EventArgs e)
{
    var index = _list.IndexOf(sender);
    ...
}
Sinatr
  • 20,892
  • 15
  • 90
  • 319
  • Did you see the first line of Barry's link? ; - ) – TaW Apr 21 '16 at 14:28
  • @Barry's comment? wpf one? – Sinatr Apr 21 '16 at 14:29
  • thanks, this is helpful, maybe i will use it a bit later – Vladimir Tsykunov Apr 21 '16 at 14:30
  • @TaW, the idea is to create (or to have initially) specialized collection (in addition to `Controls`). It can be a dictionary and then using `dictionary[sender]` might be preferable *design* compared to e.g. using `Controls.Find(o => o == sender).Tag` – Sinatr Apr 21 '16 at 14:47
  • Yes, but we can't really tell since we do not know what the index is supposed to __do__.. That's the question I meant by asking about the design. Putting info into the Tag is the cheap version of maintainig a Dictionary. – TaW Apr 21 '16 at 14:52
  • @TaW, since when `Controls` become dictionary? [It is not](https://msdn.microsoft.com/en-us/library/system.windows.forms.control.controlcollection(v=vs.110).aspx), so if you want dictionary you have to maintain it separately. Accessing `Tag` is linear search. – Sinatr Apr 22 '16 at 06:58
  • Well just about anything can go into a `Dictionary`, right? Maybe `Dictionary` would have been clearer. I thought that's what you meant.. Search speed can't matter when reacting to a user interacting with GUI controls.. – TaW Apr 22 '16 at 07:17
0
//----checked change----

private void radioButtons_CheckedChanged(object sender, EventArgs e)
{
  int ndx = 0;
            var buttons = RdoGroup.Controls.OfType<RadioButton>()
  .FirstOrDefault(n => n.Checked);

//-----in initialize set radioButton tags : this.radioButton1.Tag = "1";------

        if (buttons.Tag != null) ndx=Convert.ToInt32( buttons.Tag.ToString());
//--------do some thing by index----------

}
H.P
  • 11
  • 3
  • Welcome to Stack Overflow. Code is a lot more helpful when it is accompanied by an explanation. Stack Overflow is about learning, not providing snippets to blindly copy and paste. Please edit your question and explain how it answers the specific question being asked. – Vimal Patel Dec 12 '21 at 07:33