I want to use the incrementing of the for loop to access different picture boxes and images in the resource folder. I could type it all out as I have shown below but I would like to "program" it more. (Hope my question makes sense)
private void btnUse19_Click(object sender, EventArgs e)
{
string applicationDirectory = Application.ExecutablePath;
//Using my student number as an index to get the file path of the application.
string editedApplicationDirectory = applicationDirectory.Substring(0, applicationDirectory.IndexOf("19001700"));
for (int i = 1; i <= 10; i++)
{
string spineNumber = i.ToString();
string pbSpineNum = "pbSpine" + spineNumber;
string picNameNum = "Spine " + spineNumber + ".png";
//this is what im trying to do but does not work
pbSpineNum.Image = Image.FromFile(editedApplicationDirectory + "19001700" + "\\Resources\\"+picNameNum);
}
//Dont want to have to do this
pbSpine1.Image = Image.FromFile(editedApplicationDirectory + "19001700" + "\\Resources\\Spine 1.png");
pbSpine2.Image = Image.FromFile(editedApplicationDirectory + "19001700" + "\\Resources\\Spine 2.png");
pbSpine3.Image = Image.FromFile(editedApplicationDirectory + "19001700" + "\\Resources\\Spine 3.png");
pbSpine4.Image = Image.FromFile(editedApplicationDirectory + "19001700" + "\\Resources\\Spine 4.png");
pbSpine5.Image = Image.FromFile(editedApplicationDirectory + "19001700" + "\\Resources\\Spine 5.png");
pbSpine6.Image = Image.FromFile(editedApplicationDirectory + "19001700" + "\\Resources\\Spine 6.png");
pbSpine7.Image = Image.FromFile(editedApplicationDirectory + "19001700" + "\\Resources\\Spine 7.png");
pbSpine8.Image = Image.FromFile(editedApplicationDirectory + "19001700" + "\\Resources\\Spine 8.png");
pbSpine9.Image = Image.FromFile(editedApplicationDirectory + "19001700" + "\\Resources\\Spine 9.png");
pbSpine10.Image = Image.FromFile(editedApplicationDirectory + "19001700" + "\\Resources\\Spine 10.png");
}
You can use a list of picture boxes and the counter to refactor and reduce all to one line moved in the loop:
I changed names to be more talking and clean, as well as the path according to what I seem to have understood that you were doing.
Now you can use the list to process items like adding them in a panel, or the form:
And you can access them like that:
But you should initialize each control in the loop using certain calculated variables for rendering:
You might need to promote this list as a class data member if you plan to use it outside of the method:
Another improvement for a cleaner code will be to use a constant or a variable instead of the literal in the loop itself:
If this number is fixed, you can use a const and also a fixed array instead of a
List:If the picture boxes are created using the Visual Studio Form Designer and are already created, you can simply use this preallocated array:
But unlike the fully dynamic version, here we may encounter matching problems between the indexes of controls and images.
I therefore do not recommend to use this with controls already placed on the form unless we know what we are doing and that we control the process in a robust way.