将项目添加到组合框,同时通过c#Windows窗体中的字符串引用组合框名称

Jam*_*son 3 .net c# winforms

一直在寻找高低,以获得如何做到这一点的答案!我基本上有26个组合框名为comboBox1 - comboBox26,并且想要使用for循环向每个框添加项目,所以我需要将comboBox称为字符串.解释不好,这是我到目前为止的代码;

for (int n = 1; n <= 26; n++)
{
      this.["comboBox"].Text.AddRange(new string[] 
             {"First Item", "second item", "third", "fourth", "fifth"}); 
}
Run Code Online (Sandbox Code Playgroud)

所以在循环之后,所有26个组合框都应填充该字符串数组.这和我尝试的其他所有东西都会抛出一个错误而且似乎无法找到答案,任何帮助都会很棒!

谢谢

Sha*_*ger 5

用途controls.Find:

for (int n = 1; n <= 26; n++)
{
    ComboBox c = Controls.Find("comboBox_"+n.ToString(),true)[0] as ComboBox;
    c.Items.AddRange(new string[] {"First Item", "second item", "third", "fourth", "fifth"});
}
Run Code Online (Sandbox Code Playgroud)

这是假设你命名你的组合框comboBox_0通过comboBox_25