I am initializing and array of objects, i need something like this:

Greyhound[1].StartingPosition = pictureBox1.Location;
Greyhound[2].StartingPosition = pictureBox2.Location;

and so on.. but I need to make it by a loop

for ( ......... ) { Greyhound.StartingPosition = ????????? // what should go here }

I would recommend using a list of greyhound, however you can do the following (assuming you are on a form)

Regex regex = new Regex(@"pictureBox(?<num>\d+)");

                        foreach( PictureBox pic in this.Controls.OfType<PictureBox>() )
                        {
                                int pictureNumber = 0;
                                int.TryParse( regex.Match( pic.Name ).Groups[ "num" ].Value , out pictureNumber );

                                Greyhound[ pictureNumber ] = pic.Location;
                        }

This would work, however we recommended solution would be to have a list of greyhound, you then use a foreach picturebox and add an item to your greyhound list with the needed information (location and id or picturebox name perhaps)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.