我该如何在c#(压缩代码)中正确执行此操作

时间:2018-10-24 08:11:02

标签: c# winforms

我对编程非常无能,而且我从谷歌搜索中学到的所有知识。

我无法找出合适的词来搜索以找到答案,我知道我曾经看过它,如果有人可以告诉我我要完成的工作的名称,那么我将非常感激一个非常基本的示例

这是我的代码,

if (metroButton2.Text == "Check All")
{
    metroCheckBox1.CheckState = CheckState.Checked;
    metroCheckBox2.CheckState = CheckState.Checked;
    //Insert 58 Lines
    metroCheckBox61.CheckState = CheckState.Checked;
    metroCheckBox62.CheckState = CheckState.Checked;
    metroButton2.Text = "Uncheck All";
    metroTabPage1.Focus();
}
else if (metroButton2.Text == "Uncheck All")
{
    metroCheckBox1.CheckState = CheckState.Unchecked;
    metroCheckBox2.CheckState = CheckState.Unchecked;
    //Insert 58 Lines
    metroCheckBox61.CheckState = CheckState.Unchecked;
    metroCheckBox62.CheckState = CheckState.Unchecked;
    metroButton2.Text = "Check All";
    metroTabPage1.Focus();
}

请,谢谢您的时间。

我不知道如何找到控件名称,因为我知道所有控件名称。

我想做的就是浓缩所有这些

            if (metroButton2.Text == "Uncheck All")
            {
                metroCheckBox1.CheckState = CheckState.Unchecked;
                metroCheckBox2.CheckState = CheckState.Unchecked;
                metroCheckBox3.CheckState = CheckState.Unchecked;
                metroCheckBox4.CheckState = CheckState.Unchecked;
                metroCheckBox5.CheckState = CheckState.Unchecked;
                metroCheckBox6.CheckState = CheckState.Unchecked;
                metroCheckBox7.CheckState = CheckState.Unchecked;
                metroCheckBox8.CheckState = CheckState.Unchecked;
                metroCheckBox9.CheckState = CheckState.Unchecked;
                metroCheckBox10.CheckState = CheckState.Unchecked;
                metroCheckBox11.CheckState = CheckState.Unchecked;
                metroCheckBox12.CheckState = CheckState.Unchecked;
                metroCheckBox13.CheckState = CheckState.Unchecked;
                metroCheckBox14.CheckState = CheckState.Unchecked;
                metroCheckBox15.CheckState = CheckState.Unchecked;
                metroCheckBox16.CheckState = CheckState.Unchecked;
                metroCheckBox17.CheckState = CheckState.Unchecked;
                metroCheckBox18.CheckState = CheckState.Unchecked;
                metroCheckBox19.CheckState = CheckState.Unchecked;
                metroCheckBox20.CheckState = CheckState.Unchecked;
                metroCheckBox21.CheckState = CheckState.Unchecked;
                metroCheckBox22.CheckState = CheckState.Unchecked;
                metroCheckBox23.CheckState = CheckState.Unchecked;
                metroCheckBox24.CheckState = CheckState.Unchecked;
                metroCheckBox25.CheckState = CheckState.Unchecked;
                metroCheckBox26.CheckState = CheckState.Unchecked;
                metroCheckBox27.CheckState = CheckState.Unchecked;
                metroCheckBox28.CheckState = CheckState.Unchecked;
                metroCheckBox29.CheckState = CheckState.Unchecked;
                metroCheckBox30.CheckState = CheckState.Unchecked;
                metroCheckBox31.CheckState = CheckState.Unchecked;
                metroCheckBox32.CheckState = CheckState.Unchecked;
                metroCheckBox33.CheckState = CheckState.Unchecked;
                metroCheckBox34.CheckState = CheckState.Unchecked;
                metroCheckBox35.CheckState = CheckState.Unchecked;
                metroCheckBox36.CheckState = CheckState.Unchecked;
                metroCheckBox37.CheckState = CheckState.Unchecked;
                metroCheckBox38.CheckState = CheckState.Unchecked;
                metroCheckBox39.CheckState = CheckState.Unchecked;
                metroCheckBox40.CheckState = CheckState.Unchecked;
                metroCheckBox41.CheckState = CheckState.Unchecked;
                metroCheckBox42.CheckState = CheckState.Unchecked;
                metroCheckBox43.CheckState = CheckState.Unchecked;
                metroCheckBox44.CheckState = CheckState.Unchecked;
                metroCheckBox45.CheckState = CheckState.Unchecked;
                metroCheckBox46.CheckState = CheckState.Unchecked;
                metroCheckBox47.CheckState = CheckState.Unchecked;
                metroCheckBox48.CheckState = CheckState.Unchecked;
                metroCheckBox49.CheckState = CheckState.Unchecked;
                metroCheckBox50.CheckState = CheckState.Unchecked;
                metroCheckBox51.CheckState = CheckState.Unchecked;
                metroCheckBox52.CheckState = CheckState.Unchecked;
                metroCheckBox53.CheckState = CheckState.Unchecked;
                metroCheckBox54.CheckState = CheckState.Unchecked;
                metroCheckBox55.CheckState = CheckState.Unchecked;
                metroCheckBox56.CheckState = CheckState.Unchecked;
                metroCheckBox57.CheckState = CheckState.Unchecked;
                metroCheckBox58.CheckState = CheckState.Unchecked;
                metroCheckBox59.CheckState = CheckState.Unchecked;
                metroCheckBox60.CheckState = CheckState.Unchecked;
                metroCheckBox61.CheckState = CheckState.Unchecked;
                metroCheckBox62.CheckState = CheckState.Unchecked;
                metroButton2.Text = "Check All";
                metroTabPage1.Focus();
            }

我所拥有的一切都按原样完美运行,我只想知道如何压缩所有这些,以我要求的方式,我已经看过它用其他语言完成过,我是否必须先创建一个字符串并执行那个?我想执行一小段代码,这将取消选中每个框。如果我能得到一个简单的例子,如

if (1<2)
{
    for (int i = 1; i < 62; i++)
    {
        var myVariable = i;
        metroCheckBox["myVariable"].CheckState = CheckState.Unchecked;
    }
}

我真的很想找出如何完成这样的事情

请谢谢。

此外,我正在为此使用Windows窗体应用程序,如果有帮助,这似乎是我要问的基本问题,我太愚蠢而无法自己找到答案。

3 个答案:

答案 0 :(得分:0)

您可以选中和取消选中所有复选框复选框,如下所示

支票

from selenium import webdriver

driver = webdriver.Firefox(executable_path='/path/to/geckodriver')
print("Firefox Browser Invoked")
driver.get('http://google.com/')
driver.quit()

取消选中

foreach(Control control in this.Controls)
{
     if(control.GetType() == typeof(CheckBox) && control.Name.Contains("metroCheckBox"))
     {
          ((CheckBox)control).CheckState = CheckState.Checked;
     }
}

答案 1 :(得分:0)

在您的类中创建一个字段,以保存对您希望一起切换的所有控件的引用(请注意,如果您要具有“此组中的所有成员”样式功能以及“所有”,则可以多次执行此操作”):

private List<CheckBox> _allCheckboxes;

然后填充一次。通常在调用InitialiseComponents之后的一个好地方:

_allCheckBoxes = new List<CheckBox>{
  metroCheckBox1,
  metroCheckBox2,
  /* ... */
};

或者,如果确实是表单上的所有CheckBox控件,则应该执行以下操作:

_allCheckBoxes = this.Controls.OfType<CheckBox>().ToList();

(使用LINQ)应该可以解决问题。

现在,当您要切换所有控件时,可以使用foreach语句遍历每个控件并设置其CheckState属性(并且请注意,我们不再需要知道例如,如果我们使用foreach,则有62个项目

答案 2 :(得分:0)

为了显示Guido所说的内容,以便在子面板中访问它们时,这是一种非常简单的方法:

            if (c is CheckBox && c.Name.StartsWith("metroCheck"))
            {
                ((CheckBox)c).CheckState = CheckState.Checked;
            }
            else if (c.HasChildren)
            {
                foreach (Control cc in c.Controls)
                {
                    if (cc is CheckBox && cc.Name.StartsWith("metroCheck"))
                    {
                        ((CheckBox)cc).CheckState = CheckState.Checked;
                    }
                }
            }

感谢Guido