c# - Accessing panels with similar names -


i making game have lots of panels. names polje1, polje2, polje3 etc ... of them, need call invalidate() method refresh paint event , problem don't want use 50 ifs or switch function.

so, can ("polje" + number).invalidate();

here code if don't understand:

random rnd = new random(); private panel polje1; private panel polje2; private panel polje3; private panel polje4; private panel polje5; private panel polje6; private panel polje7; private panel polje8; private panel polje9; private panel polje10;  definition bla bla bla ...  private void button1_mouseclick(object sender, mouseeventargs e) {   int pp = rnd.next(1,7);   setcurrentfield(pp);   ("polje" + pp).invalidate(); 

}

neither invalidate() method not option, because form flickering ! make class , perform doublebuffered();

you can access control it's name property through container's control collection:

example showing containskey function make sure control's name exist in collection:

panel polje1 = new panel() { name = "polje1" }; this.controls.add(polje1); if (this.controls.containskey("polje1")) {   this.controls["polje1"].backcolor = color.red; } 

Comments

Popular posts from this blog

hibernate - How to load global settings frequently used in application in Java -

python 3.x - Mapping specific letters onto a list of words -

objective c - Ownership modifiers with manual reference counting -