c# - Dynamic combo box names -
c# - Dynamic combo box names -
i'm working c#. want utilize string variables combo box names. suppose, have 3 combo box named box1, box2 , box3. want alter properties of these combo boxes. can write:
box1.someproperty = somevalue1; box2.someproperty = somevalue2; box3.someproperty = somevalue3; but want within for or while loop. like:
string[] names = new string[3] {"box1","box2","box3"}; int[] values= new int[3] {4,5,6}; (int = 0; <= names.length; i++) { ?names[i]?.someproperty = values[i]; } ?name[i]? replaced strings names variable. first post. please forgive errors.
if have set of controls know you'll want update, create collection:
var comboboxes = new list<combobox> { box1, box2, box3 }; foreach (var cb in comboboxes) { cb.someproperty = somevalue1; } if wanted utilize names, search them in current form's controls collection (assuming winforms... didn't specify):
var comboboxes = new list<string> { "box1", "box2", "box3" }; foreach (var cb in comboboxes) { var box = (combobox)this.controls.find(cb, true).firstordefault(); if (box != null) box.someproperty = somevalue1; } if want modify all combobox controls on current form (again, assuming winforms), utilize linq's oftype<>() method in conjunction controls collection:
foreach (var cb in this.controls.oftype<combobox>()) { cb.someproperty = somevalue1; } c#
Comments
Post a Comment