ive got for example 25 panels that are formed like a grid (5 rows, 5 columns),
if i click at one of those i want to get e.g. the panel above too.
at first i named the panels like their positions e.g. PanelX1Y1, PanelX1Y2 …
then i took the coords out of the name and created the new coords…
and after i got the new name, i used a foreach loop to go through all the items and get that one with the right name.
ive already tried it with the winforms positions so i took the positions of the clicked one addet e.g. 25 pixels and lopped through all the items in form and checked them via their location.
But i dont want to looped through all the items …
how can i get the item if i know its name whitout to loop through all items and check for their names..
can i use this:
this.Controls[“name”];
okay my grid:
p11 p12 p13
p21 p22 p23
p31 p32 p33
if i click on p31 i want to change something at p31 and p21 so i need the object p21
To directly answer you question, you can use the
Find()method of theControlCollectionclass. e.g.:To suggest a better method, don’t use strings for something like this. It is hackish and sloppy.
Instead, initialize your panels in a 2D array and use the array indices to find the right panel.