Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
325 views
in Technique[技术] by (71.8m points)

winforms - Is it possible to incorporate variables into a C# switch{case} statement


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You may create your controls programmatically and make an array:

var combos = Enumerable.Range(0,100)
.Select((x, i) => new ComboBox {Name = $"TypeComboBox{i}", Visible = false, ...})
.ToArray();

or you may just get controls by name:

ComboBox GetCombo(int index)
{
    var name = "TypeComboBox" + index.ToString().PadLeft('0',2); 
    var combo = this.Controls[name];
    return combo;
}

it is also possible to assign your existing controls to an array:

var combos = parent.Controls.OfType<ComboBox>()
             .OrderBy(c => c.Name)
             .ToArray();  //parent can be your form or whatever 
                          //control that contains your comboboxes
                      

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...