简体   繁体   English

验证在C#中选中了哪个动态创建的Radiobutton

[英]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). 在每个面板中,都有一个带有三个单选按钮的TableLayoutPanel(在其他几个不重要的组件中)。 The Radiobuttons set the priority of the specific Panel's input (Priority 1 , 2 and 3 ). 的单选按钮设置的特定小组的输入的优先权(优先级1,23)。

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. lTableLayoutColumnlTableLayoutRow用于设置放置单选 按钮的列。 lPriority are used to calculate the Priority of the button and always has a value from 1 to 3 . lPriority用于计算按钮的Priority,并且始终具有13的值。

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. 此函数始终从try块返回-1 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? 您的GetSelectedRadioBtn()创建自己的单选按钮,应该如何链接到先前的按钮?

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. 做您想做的事情的一种方法是将一个checkedChanged事件订阅到单选按钮,然后更改一个名为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 然后在GetSelectedRadioBtn中执行此操作

private int GetSelectedRadioBtn()
{
    return selectedRadio;
}

The easiest way to do it is to store a reference to the radio buttons created in the AddPriorityRadBtn . 最简单的方法是存储对AddPriorityRadBtn创建的单选按钮的AddPriorityRadBtn You can keep them for example, in a dictionary which uses the Panel as key, and the three radio buttons in an array. 例如,您可以将它们保留在使用Panel作为键的字典中,并在数组中保留三个单选按钮。

Then, you just have to look at the dictionary to recover the radio buttons and check its state. 然后,您只需要查看字典即可恢复单选按钮并检查其状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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