Reputation: 13
I am making some game where I have lots of panels. Their names are polje1, polje2, polje3 etc ... For some of them, I need to call Invalidate() method to refresh the paint event and problem is that I don't want to use 50 ifs or switch function.
So, can I do something like ("polje" + number).Invalidate();
Here is some code if you 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 only is not an option, because form is flickering ! I already make my class and perform DoubleBuffered();
Upvotes: 1
Views: 40
Reputation: 81645
You can access a control by it's name property through the container's control collection:
Example showing the ContainsKey function to make sure the control's name does exist in the collection:
Panel polje1 = new Panel() { Name = "polje1" };
this.Controls.Add(polje1);
if (this.Controls.ContainsKey("polje1")) {
this.Controls["polje1"].BackColor = Color.Red;
}
Upvotes: 1