What is the best way to get the RadioButton that was chosen by the user?

1

My program has 5 RadioButton on one of the Panel that I have not renamed and thus got Panel1 , so only one can be chosen.

I created a list with all RadioButtons , after clicking a button I tried to use the code below to iterate the Tag tag property:

// this.ListaRadios é minha lista de RadioButtons

var variacao = from item in this.ListaRadios //Esta parte dá erro:

// Could not find an implementation of the query pattern for source type
// 'System.Collections.Generic.List<System.Windows.Forms.RadioButton>'.
// 'Where' not found.  Are you missing a reference or a using directive
// for 'System.Linq'?

               where item.Checked = true
               select (string)item.Tag;

In addition to the error you are experiencing, is there a more efficient way to do this?

How would a Linq to iterate only the RadioButtons of a Panel and then the only one marked?

    
asked by anonymous 13.07.2015 / 23:21

1 answer

1
    var resultado = "";

    foreach (Control control in this.Panel1.Controls)
    {
        if (control is RadioButton)
        {
            RadioButton radio = control as RadioButton;

            if (radio.Checked)
            {
                resultado = radio.Text; //Text ou qualquer outra propriedade
            }
        }
    }
    
13.07.2015 / 23:44