c# - Access a form object using variable parameters -
is there way access label variable parameters? example, have list of labels (lbl00, lbl01, lbl02, lbl10, lbl11, lbl12) , need able access them programmatically change background color. in example below, strlabel = "lbl01", correspond correct object in form, cannot passed string. there way make work?
thanks!
private void btntest_click(object sender, eventargs e) { testhilight("0", "1"); } public void testhilight(string x, string y) { string strlabel = "lbl" + x + y; strlabel.backcolor = system.drawing.color.green; }
it better if keep track of labels in memory, if want find label
or control based on name can use control.find
method:
var control = this.controls.find(strlabel, true); //pass "lbl" + x + y; if(control != null && control.oftype<label>().any()) { //label found label label = control.oftype<label>().first() label; label.backcolor = system.drawing.color.green; }
you can shorten code like:
public void testhilight(string x, string y) { var matchedlabel = controls.find("lbl" + x + y, true).oftype<label>().firstordefault(); if (matchedlabel != null) { //label found matchedlabel.backcolor = system.drawing.color.green; } }
Comments
Post a Comment