简体   繁体   中英

Verify which dynamically created Radiobutton is checked in C#

I am developing an application with multiple Panels (eight, to be precise) on one Form. In each Panel there are one TableLayoutPanel with three Radiobuttons (among of few other unimportant components). The Radiobuttons set the priority of the specific Panel's input (Priority 1 , 2 and 3 ).

I have no intention of placing three Radiobuttons on each of the eight Panels as I believe there are more effective ways of doing this. Below is the code used to place the Radiobuttons:

private void AddPriorityRadBtn(TableLayoutPanel lLayoutTable, int lTableLayoutColumn, int lTableLayoutRow)
    {
        int lPriority = -1;
        try
        {
            for (int i = lTableLayoutRow; i < (lTableLayoutRow+ 3); i++)
            {
                RadioButton lRadBtn = new RadioButton();
                lPriority = i - lLayTblStRow + 1;

                lRadBtn.Name = "radP_" + lPriority.ToString();
                lRadBtn.Text = "Priority Level " + lPriority.ToString();
                lRadBtn.Anchor = AnchorStyles.Left;
                lLayoutTable.Controls.Add(lRadBtn);


                if (lPriority < 3)
                {
                    lRadBtn.Checked = false;

                }
                else if(lPriority == 3)
                {
                    lRadBtn.Checked = true;
                }

                lTableLayout.Controls.Add(lRadBtn, lTableLayoutColumn, i);
            }
        }
        catch
        {
            Console.WriteLine(ex);
        }
    }

The lTableLayoutColumn and lTableLayoutRow are used to set the column in which the Radiobuttons are placed. lPriority are used to calculate the Priority of the button and always has a value from 1 to 3 .

I can add the Radiobuttons with the above code. I suspect this is where the problem is.

The next piece of code is where the problem becomes more evident:

private int GetSelectedRadioBtn()
    {
        RadioButton lRadBtnPriority = new RadioButton();
        try
        {
            for (int i = 1; i < 4; i++)
            {
                lRadBtnPriority.Name = "radP_" + i.ToString();
                if (lRadBtnPriority.Checked == true)
                {
                    return i;
                }
            }
            return -1;
        }
        catch (Exception ex)
        {
            return -1;
        }
    }

This function always returns -1 from the try block. Thus my application can't see which one of the Radiobuttons is selected.

What would be the reason for this? Any kind of help would be much appreciated.

Your GetSelectedRadioBtn() creates its own radio button how is it supposed to be linked to the previous buttons?

lRadBtnPriority.Name = "radP_" + i.ToString();

doing this doesnt magicly link your new button to the previusly created ones.

One way of doing what you want to do is subscribing a checkedChanged event to the radio buttons and changing a global variable called selectedRadio.

for (int i = lTableLayoutRow; i < (lTableLayoutRow+ 3); i++)
{
    RadioButton lRadBtn = new RadioButton();
    lPriority = i - lLayTblStRow + 1;

    lRadBtn.Name = "radP_" + lPriority.ToString();
    lRadBtn.Text = "Priority Level " + lPriority.ToString();
    lRadBtn.Anchor = AnchorStyles.Left;
    lRadBtn.Tag = i;
    lRadBtn.CheckedChanged +=(ss,ee)=>{

        selectedRadio = (int)((RadioButton)ss).Tag;
    };
    lLayoutTable.Controls.Add(lRadBtn);


    if (lPriority < 3)
    {
        lRadBtn.Checked = false;

    }
    else if(lPriority == 3)
    {
        lRadBtn.Checked = true;
    }

    lTableLayout.Controls.Add(lRadBtn, lTableLayoutColumn, i);
}

then in GetSelectedRadioBtn you do this

private int GetSelectedRadioBtn()
{
    return selectedRadio;
}

The easiest way to do it is to store a reference to the radio buttons created in the AddPriorityRadBtn . You can keep them for example, in a dictionary which uses the Panel as key, and the three radio buttons in an array.

Then, you just have to look at the dictionary to recover the radio buttons and check its state.

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