-1

I am really bad a wording questions but hopefully showing what my problem is will work better. Also if someone has a better way to word the question just tell me and I will change it.

for(int i = 0; i < 208; ++i)
{
        string p = "panel" + i.ToString();
        
        //This thing
        p.Location = new Point(1, 0);
}

Yes I know thats not how it is done but how could I achieve this properly. I have been searching for a while and have no clue of to approach this.

iawindowss
  • 13
  • 2
  • 1
    If you want to lookup an object with some key, you probably want a `Dictionary`. – Evan Trimboli Nov 02 '20 at 05:28
  • 1
    The biggest issue here is that you have 208 panels. To create those manually would be a nightmare, so if you created them by code then you should have kept an array of the panels, and in that case you'd be able to just index an array to get the panel you need. – Enigmativity Nov 02 '20 at 05:39

1 Answers1

1

Assuming WinForms...

Use the Controls.Find() function:

for(int i = 0; i < 208; ++i)
{
    string pnlName = "panel" + i.ToString();
    Panel pnl = this.Controls.Find(pnlName, true).FirstOrDefault() As Panel;
    if (pnl != null) 
    {
        pnl.Location = new Point(1, 0);
    }
}
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40