简体   繁体   中英

How to know the index of radiobutton selected

How to know the selcted index of a radio button to know which index option is selcted by user.

Currently my code displays the items read. But when i select the items appeared then it shows in a textblock the selected item of radiobutton (of the given 3 options, because my radiobutton currently has 3 items).

My try fo this is :

foreach (String item in param.Parameter[lop].Component.Attributes.Items)
{
    RadioButton radio = new RadioButton()
    {
        Content = item,
        GroupName = "MyRadioButtonGroup"
    };
    radio.Checked += (o, e) =>
    {
        txtblkShowStatus.Text = item;       
    };
    data= param.Parameter[lop].Label;
    sp.Children.Add(radio);                           
}

Now i want something like insted of "item" based selection now i want index based seelction. I want some thing like . If user select 2nd button(of radiobutton) then i will display some UI element. And the items are obtained on deserializing an xml. Which only deals with the index of the radio buttons. So i want something like if(selectedIndexofRadioButton ==IndexObtainedFromXmlInIntegr){do something}

Is it possible to do? If yes then how should i change my code to achieve this ? EDIT: My code change after DonBoitnott's answer edit is :

foreach (String item in param.Parameter[lop].Component.Attributes.Items)
                        {
                            RadioButton radio = new RadioButton()
                            {
                                Content = item,
                                GroupName = "MyRadioButtonGroup",
                                Tag=tg
                            };
                            radio.Checked += (o, e) =>
                            {
                                txtblkShowStatus.Text = item;
                                if (((Int32)((RadioButton)o).Tag).Equals(2))
                                {
                                    MessageBox.Show("hurrey");
                                }
                            };
                            radio.Tag=1;
                            data= param.Parameter[lop].Label;
                            sp.Children.Add(radio);
                            index++; tg++;
                        }

You should leverage the Tag property of the RadioButton control object:

foreach (String item in param.Parameter[lop].Component.Attributes.Items)
{
    RadioButton radio = new RadioButton()
    {
        Content = item,
        GroupName = "MyRadioButtonGroup",
        Tag = //integer value from XML
    };
    radio.Checked += (o, e) =>
    {
        txtblkShowStatus.Text = item;       
    };
    data = param.Parameter[lop].Label;
    sp.Children.Add(radio);                           
}

The Tag property is an Object , so you can assign anything to it. You just have to cast it on the way out when you use it:

if (((Int32)radioButton.Tag).Equals(IndexObtainedFromXmlInInteger)
{
    //do something
}

In response to your comment, OP:

radio.Checked += (o, e) => 
{
    txtblkShowStatus.Text = item; 
    if (((Int32)((RadioButton)o).Tag).Equals(2)) 
    {
        MessageBox.Show("hurrey");
    }
};

You are mistakenly trying to use radio , which is declared above. Instead, use what the anonymous method actually receives , which is o , or the Sender .

Create an event for when the index is changed on your radio button group. After this create a separate function to deserialize your XML file. Pass the given integer obtained from your XML file to the index changed event and then display the UI element accordingly. Be sure to compare the integer on the .selectedIndex property of your radio button.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM