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:

  1. this hide labels not named "label" + i. may need additional filtering logic if there other labels on form/container

  2. the 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 replace controls panel1.controls


Comments

Popular posts from this blog

curl - PHP fsockopen help required -

HTTP/1.0 407 Proxy Authentication Required PHP -

c# - Resource not found error -