如何使用循环加载多个图像到图片框没有重复的代码?



我想使用for循环的增量来访问资源文件夹中的不同图片框和图像。我可以像下面所示的那样全部输入,但我想要"编程"。它更多。(希望我的问题有意义)

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");
}

您可以使用一个图片框列表和计数器来重构并减少到在循环中移动的一行:

using System.IO;
using System.Collections.Generic;
var pictureBoxes = new List<PictureBox>();
string pathExe = Path.GetDirectoryName(Application.ExecutablePath);
string pathImagesPattern = Path.Combine(pathExe, "Resources", "Spine {0}.png");
for ( int index = 1; index <= 10; index++ )
{
var pictureBox = new PictureBox();
string pathImage = string.Format(pathImagesPattern, index);
pictureBox.Image = Image.FromFile(pathImage);
pictureBoxes.Add(pictureBox);
}

我改名字是为了更有发言权和更干净,以及根据我似乎已经理解你在做什么路径。

现在您可以使用列表来处理项目,例如将它们添加到面板或表单中:

MyPanel.Controls.AddRange(pictureBoxes.ToArray());

你可以这样访问它们:

pictureBoxes[0].Location = ...

但是您应该使用特定的计算变量初始化循环中的每个控件以进行呈现:

pictureBox.Location = new Point(posX, posY);

如果您计划在方法之外使用此列表,则可能需要将其提升为类数据成员:

private List<PictureBox> PictureBoxes = new List<PictureBox>();

对于更简洁的代码的另一个改进是在循环中使用常量或变量而不是字面量:

private int PictureBoxesCount = 10;
PictureBoxes.Clear();
for ( int index = 1; index <= PictureBoxesCount; index++ )

如果这个数字是固定的,你可以使用const和固定数组来代替List:

const private int PictureBoxCount = 10;
private PictureBox[] PictureBoxes = new PictureBox[PictureBoxCount];
for ( int index = 0; index < PictureBoxes.Length; index++ )
{
string pathImage = string.Format(pathImagesPattern, index);
PictureBoxes[index] = new PictureBox();
PictureBoxes[index].Image = Image.FromFile(pathImage);
}

如果图片框是使用Visual Studio表单设计器创建的,并且已经创建,您可以简单地使用这个预分配数组:

PictureBoxes = Controls.OfType<PictureBox>()
.Where(p => p.Name.StartsWith("pbSpine"))
.OrderBy(p => p.Name.Length)
.ThenBy(p => p.Name)
.ToArray();
for ( int index = 0; index < PictureBoxes.Length; index++ )
{
string pathImage = string.Format(pathImagesPattern, index);
PictureBoxes[index].Image = Image.FromFile(pathImage);
}

但是与完全动态的版本不同,这里我们可能会遇到控件索引和图像索引之间的匹配问题。

因此,我不建议对已经放置在表单上的控件使用此方法,除非我们知道我们在做什么,并且我们以一种健壮的方式控制过程。

最新更新