vendredi 17 avril 2015

C# Easier way to create array of pictureboxes (48x boxes)

Project enviroment: Visual Studio 2013, using Windows Forms template.


This is what I currently have, I assume there is an easier way to create an array like this. I have tried many ways, but I do not know C# well enough yet.



PictureBox[] boxes = {
runeBox1, runeBox2, runeBox3, runeBox4, runeBox5, runeBox6, runeBox7, runeBox8,
runeBox9, runeBox10, runeBox11, runeBox12, runeBox13, runeBox14, runeBox15, runeBox16,
runeBox17, runeBox18, runeBox19, runeBox20, runeBox21, runeBox22, runeBox23, runeBox24,
runeBox25, runeBox26, runeBox27, runeBox28, runeBox29, runeBox30, runeBox31, runeBox32,
runeBox33, runeBox34, runeBox35, runeBox36, runeBox37, runeBox38, runeBox39, runeBox40,
runeBox41, runeBox42, runeBox43, runeBox44, runeBox45, runeBox46, runeBox47, runeBox48};


What I have tried so far is this:



string[] strArr = new string[48];
for (int i = 1; i != 48; i++)
{
strArr[i] = "runebox" + i.ToString();
}
int count = 0;
foreach (object p in this.Controls)
{
if (p.GetType() == typeof(PictureBox))
{
if (((PictureBox)p).Name == strArr[count])
{
boxes[count] = ((PictureBox)p);
count++;
}
}
}


This compiles, but it gives a nullpointer exception in the array boxes.


I have also attempted a typeconverter, but this gave me no results as well.


Hopefully some of you clever ones can tell me the road to Rome.


Aucun commentaire:

Enregistrer un commentaire