Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
360 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 OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...