user1763812
user1763812

Reputation: 437

C# radio button array, how to find selected

I have the following c# code that will create a radio button array. How could I check which button was selected without going through each button individually?

Thanks.

 public Form1()
        {
            InitializeComponent();
            string[] stringArray = new string[3];

            stringArray[0] = "Yes";
            stringArray[1] = "No";
            stringArray[2] = "Maybe";

            System.Windows.Forms.RadioButton[] radioButtons = new System.Windows.Forms.RadioButton[3];

            for (int i = 0; i < 3; ++i)
            {
                radioButtons[i] = new RadioButton();
                radioButtons[i].Text = stringArray[i];
                radioButtons[i].Location = new System.Drawing.Point(10, 10 + i * 20);
                this.Controls.Add(radioButtons[i]);
            }
        }

Upvotes: 0

Views: 6867

Answers (3)

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236188

RadioButton selected = radioButtons.FirstOrDefault(r => r.Checked);

Just after creation all your radiobuttons will be unchecked, that's why I use FirstOrDefault instead of First. So, you should check if selected is not null.

UPDATE: If you want to do something each time when selected radiobutton changes (e.g. update some label with radiobutton text) then looping all radiobuttons is not best solution. You can subscribe all radiobuttons to same CheckedChanged event handler. In your loop add

radioButtons[i].CheckedChanged += RadioButton_CheckedChanged;

And handler is

private void RadioButton_CheckedChanged(object sender, EventArgs e)
{
    var radiobutton = (RadioButton)sender;
    if (radiobutton.Checked)
    {
        // some logic here
        label.Text = radiobutton.Text;
    }
}

UPDATE: Linq query above is a shortcut for

RadioButton selected = null;

foreach(var r in radioButtons)
{
    if (r.Checked)
    {
        selected = r;
        break;
    }
}

Upvotes: 5

Shell
Shell

Reputation: 6849

You can store the index in the any property which you are not using. ie. Tag

radioButtons[i].Tag = i

and you can handle the CheckedChanged event of that element

AddHandler radioButtons[i].CheckedChange, AddressOf Item_CheckedChange

Now, you can write code to retrieve the selected index from Tag property.

Dim iSelectedIndex As Integer = -1
Private Sub Item_CheckedChange(...)
    Dim Item As RadioButton = DirectCast(sender, RadioButton)
    If Item.Checked Then
        'Here you will get the selected radio button from Item object
        iSelectedIndex = CInt(Item.Tag)
    End If
End Sub

Upvotes: 0

Shaharyar
Shaharyar

Reputation: 12439

You can use radio button checked event. First register event for all radio buttons:

//Modifying for loop
for (int i = 0; i < 3; ++i)
{
    radioButtons[i] = new RadioButton();
    radioButtons[i].Text = stringArray[i];
    radioButtons[i].Location = new System.Drawing.Point(10, 10 + i * 20);
    radioButtons[i].CheckedChanged += radioButton_CheckedChanged; //added
    this.Controls.Add(radioButtons[i]);
}

Create new event:

RadioButton checkedButton;
void radioButton_CheckedChanged(object sender, EventArgs e)
{
    RadioButton tempRdBtn = sender as RadioButton;
    if (tempRdBtn.Checked)
        checkedButton = tempRdBtn;
}

At the end you can check checkedButton for selected item. If its null, it means no item has been selected.

Upvotes: 0

Related Questions