Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#-使用字符串命名单选按钮并访问其属性_C#_String_Properties_Radio Button - Fatal编程技术网

C#-使用字符串命名单选按钮并访问其属性

C#-使用字符串命名单选按钮并访问其属性,c#,string,properties,radio-button,C#,String,Properties,Radio Button,我有很多重复的代码,我正试图摆脱,但我有一些问题 这里是主要的一点: 我有几个计时器,都用数字标识,都有一个按钮来触发它们,但它会让相同的代码无缘无故地重复,例如: private void buttonTimer1_Start_Click(object sender, EventArgs e) { if (radioButtonTimer1_CountDown.Checked) { 等等 我已经能够为按钮创建一个事件,并通过以下方式获取按钮的编号: B

我有很多重复的代码,我正试图摆脱,但我有一些问题

这里是主要的一点:

我有几个计时器,都用数字标识,都有一个按钮来触发它们,但它会让相同的代码无缘无故地重复,例如:

private void buttonTimer1_Start_Click(object sender, EventArgs e)
    {
        if (radioButtonTimer1_CountDown.Checked)
        {
等等

我已经能够为按钮创建一个事件,并通过以下方式获取按钮的编号:

Button button = sender as Button;
var buttonName = button.Name;
var resultString = Regex.Match(buttonName, @"\d+").Value;
var buttonID = Int32.Parse(resultString);
因此,如果可能的话,我想做的是使用以下内容:

if ("radioButtonTimer"+buttonID+"_CountDown".Checked)
但无法从字符串访问属性“.Checked”

处理这个问题的最佳方式是什么?我有很多文本字段,单选按钮和其他我不需要的东西,这样我就可以使“动态”了

非常感谢您的时间和帮助。

假设WinForms:

        Button button = sender as Button;
        var resultString = Regex.Match(button.Name, @"\d+").Value;
        Control[] matches = this.Controls.Find("radioButtonTimer"+resultString+"_CountDown", true);
        if (matches.Length > 0 && matches[0] is RadioButton)
        {
            RadioButton rb = matches[0] as RadioButton;
            if (rb.Checked)
            {
                // ... do something in here ...
            }
        }
使用Controls.Find()方法

这可能是这样的:

Control[] ArrControls = this.Controls.Find("radioButtonTimer"+buttonID+"_CountDown");
if(ArrControls.Where(c => (c as RadioButton).Checked).ToList().Count > 0)
{
  // Checked Radio Buttons
}
else
{

}