我使用了不同的方法来使这个GUI工作。我已经使用了用户控件,并通过迭代我的“艺术家”来加载每个PictureBox中需要单独的图像。问题是,我认为这是非常丑陋的代码,并且可以以更好的方式完成,但目前工作正常。所以,如果有人花时间告诉我这是否可以,或者我应该以某种方式重写它?
foreach(Artist a in artists)
{
int i = 0;
u.picArtistOne.ImageLocation = artists[i].artistPic;
i++;
u.picArtistTwo.ImageLocation = artists[i].artistPic;
i++;
u.picArtistThree.ImageLocation = artists[i].artistPic;
i++;
u.picArtistFour.ImageLocation = artists[i].artistPic;
i++;
u.picArtistFive.ImageLocation = artists[i].artistPic;
i++;
u.picArtistSix.ImageLocation = artists[i].artistPic;
i++;
}
答案 0 :(得分:1)
在foreach
上执行artists
在此处无用。您可能只想考虑执行以下操作:
var index = 0;
Dictionary<int, PictureBox> pictureBoxes = u.Controls
.OfType<PictureBox>
.ToDictionary(key => index++)
for(var i = 0; i < index; i++)
{
pictureBoxes[i].ImageLocation = artists[i].artistPic;
}
答案 1 :(得分:1)
如果你绝对坚持修复的命名属性(并且不想反映它们),至少删除所有的foreach和i东西:
u.picArtistOne.ImageLocation = artists[0].artistPic;
u.picArtistTwo.ImageLocation = artists[1].artistPic;
u.picArtistThree.ImageLocation = artists[2].artistPic;
u.picArtistFour.ImageLocation = artists[3].artistPic;
u.picArtistFive.ImageLocation = artists[4].artistPic;
u.picArtistSix.ImageLocation = artists[5].artistPic;
答案 2 :(得分:0)
您可以按照以下方式执行此操作 您可以创建一个flowLayoutPanel,而不是创建您的图片框,这些图片框应该是这样的,并且这样做
foreach(Artist artist in artists)
{
PictureBox pic = new PictureBox();
//Manually set the Size and SizeMode of your pictureBox here
pic.ImageLocation = artist.artistPic;
flowLayoutPanel.Controls.Add(pic);
}