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

Asked

Viewed 225 times

1

My program has 5 RadioButton in one of Panel that I didn’t rename and so it was Panel1, then only one can be chosen.

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

// 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;

Besides the mistake you’re making, there’s a more efficient way to do it?

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

  • 1

    Regarding the error check if you have the reference to the System.Core Assembly and whether the Namespace with using System.Linq;

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
            }
        }
    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.