I’m having trouble figuring out whether or not I can do the following: I want to initialize all of the values in my PictureBox array using a for loop. I also want the values assigned to make use of the loop iterator. For instance:
PictureBox[] picUserCards = new PictureBox[6];
for (int i = 0; i < picUserCards.Length; i++)
{
// Below is similar to what I have done in VBA
picUserCards[i] = picCard + (i + 1);
}
As you can see, C# is going to throw me an error saying that picCard doesn’t exist in this context. However, in VB/VBA, it will concatenate the character for me, giving me picUserCards[0] = picCard1.
This might be a really simple answer, and I hope it hasn’t been answered dozens of times already, but my precursory search didn’t yield anything other than how to use the For loop to work with the array’s index.
If it matters, the PictureBoxes already exist (12 of them) on the form, so I wanted to figure out how to associate them with an array instead of redesigning my form.
EDIT: I want to have two arrays, a solution which takes all PictureBoxes on the form and dumps them in one array wouldn’t solve my particular issue. I want to be able to determine on my own how many objects get associated with a given array.
You can reference child controls by their string name:
This only works for immediate descendants. If your controls are nested you’ll instead have to use
Find, which will be a bit slower (should be negligible for non-performance-intensive applications).