winforms - Dynamically call a label in c# -
i trying call label dynamically have no idea how it.
i want make label visible depending on integer.
so if int = 1
, label1
should turn visible , if i = 2
, label2
should turn visible, , on , forth.
how do this?
int = word.indexof("t"); //this need label dynamically called
i tried ("label" + i.tostring()).visible = true;"
in lazy attempt.
here's dynamic solution:
foreach (var label in controls.oftype<label>()) label.visible = (label.name == "label" + i);
note that:
this hide labels not named
"label" + i
. may need additional filtering logic if there other labels on form/containerthe above code works if labels direct descendants of form. if that's not case (for example, labels children of panel called
panel1
), you'll need replacecontrols
panel1.controls
Comments
Post a Comment