对图片框使用 foreach 循环
Using a foreach loop with pictureboxes
我正在努力使我的代码尽可能短,并且我打算以一定的间隔从 3 个不同图片框的 6 张图片中随机选择图像。我没有为每个图片框复制和粘贴 switch 和 case 三次,而是尝试在一个 foreach 循环中进行更改。我是 C# 和 windows 表单的新手,因此非常感谢您的帮助。
编辑:抱歉措辞不当,但问题是程序说元素不是有效的图片框,我想知道为什么以及如何尽可能修复它,但感谢您提供替代解决方案。
private void timer1_Tick(object sender, EventArgs e)
{
Random random = new Random();
int picture = random.Next(1, 7);
var pictures = new List<PictureBox> { pictureBox1, pictureBox2, pictureBox2 };
foreach (PictureBox element in pictures)
{
switch (picture)
{
case 1:
this.element.Image = Image.FromFile("C:\Users\seanb\OneDrive\Pictures\cherry.jpg");
break;
case 2:
this.element.Image = Image.FromFile("C:\Users\seanb\OneDrive\Pictures\bell.jpg");
break;
case 3:
this.element.Image = Image.FromFile("C:\Users\seanb\OneDrive\Pictures\lemon.jpg");
break;
case 4:
this.element.Image = Image.FromFile("C:\Users\seanb\OneDrive\Pictures\orange.jpg");
break;
case 5:
this.element.Image = Image.FromFile("C:\Users\seanb\OneDrive\Pictures\star.jpg");
break;
case 6:
this.element.Image = Image.FromFile("C:\Users\seanb\OneDrive\Pictures\skull.jpg");
break;
}
}
像这样使用 Dictionary 怎么样:
Dictionary<int, Image> dictionary = new Dictionary<int, Image>()
{
{1, Image.FromFile("C:\Users\seanb\OneDrive\Pictures\cherry.jpg")},
{2, Image.FromFile("C:\Users\seanb\OneDrive\Pictures\bell.jpg")},
};
dictionary.TryGetValue(picture, out value);
if (value != null)
{
this.element.Image = value;
}
所以你有照片:
static Image[] s_Images = new string[] {
"cherry.jpg",
"bell.jpg",
"lemon.jpg",
"orange.jpg",
"star.jpg",
"skull.jpg"}
.Select(file => Path.Combine(@"C:\Users\seanb\OneDrive\Pictures", file))
.Select(file => Image.FromFile(file))
.ToArray();
static Random random = new Random();
并且您想将这些图像随机分配给图片框:
private void timer1_Tick(object sender, EventArgs e) {
foreach (PictureBox box in new PictureBox[] { pictureBox1, pictureBox2, pictureBox2 }) {
box.Image = s_Images[random.Next(s_Images.Length)];
}
}
Random random = new Random();
int picture = random.Next(1, 7);
var pictureBoxs = new List<PictureBox> { pictureBox1, pictureBox2, pictureBox2 };
string basePath = "C:\Users\seanb\OneDrive\Pictures\";
string[] pictures = new string[]{ "cherry.jpg", "bell.jpg", "lemon.jpg", "orange.jpg", "star.jpg", "skull.jpg" };
foreach (PictureBox element in pictureBoxs)
{
this.element.Image = Image.FromFile(basePath+ pictures[picture]);
}
接近Dictionary
int picture = new Random().Next(1, 7);
Dictionary<int, string> dictionary = new Dictionary<int, string>()
{
{1, "cherry.jpg"},
{2, "bell.jpg"},
{3, "lemon.jpg"},
{4, "orange.jpg"},
{5, "star.jpg"},
{6, "skull.jpg"}
};
string res = "default.jpg";
var pictures = new List<PictureBox> { pictureBox1, pictureBox2, pictureBox2 };
string path = System.IO.Path.Combine("C:\Users\seanb\OneDrive\Pictures\" + dictionary.TryGetValue(picture, out res));
pictures.ForEach(x => x.Image = Image.FromFile(path));
private void timer1_Tick(object sender, EventArgs e)
{
Random random = new Random();
List<string> picPaths = new List<string>();
picPaths.Add("C:\Users\seanb\OneDrive\Pictures\cherry.jpg");
picPaths.Add("C:\Users\seanb\OneDrive\Pictures\bell.jpg");
picPaths.Add("C:\Users\seanb\OneDrive\Pictures\lemon.jpg");
picPaths.Add("C:\Users\seanb\OneDrive\Pictures\orange.jpg");
picPaths.Add("C:\Users\seanb\OneDrive\Pictures\star.jpg");
picPaths.Add("C:\Users\seanb\OneDrive\Pictures\skull.jpg");
pictureBox1.Image = Image.FromFile(picPaths[random.Next(picPaths.Count)]);
pictureBox2.Image = Image.FromFile(picPaths[random.Next(picPaths.Count)]);
pictureBox3.Image = Image.FromFile(picPaths[random.Next(picPaths.Count)]);
}
我正在努力使我的代码尽可能短,并且我打算以一定的间隔从 3 个不同图片框的 6 张图片中随机选择图像。我没有为每个图片框复制和粘贴 switch 和 case 三次,而是尝试在一个 foreach 循环中进行更改。我是 C# 和 windows 表单的新手,因此非常感谢您的帮助。 编辑:抱歉措辞不当,但问题是程序说元素不是有效的图片框,我想知道为什么以及如何尽可能修复它,但感谢您提供替代解决方案。
private void timer1_Tick(object sender, EventArgs e)
{
Random random = new Random();
int picture = random.Next(1, 7);
var pictures = new List<PictureBox> { pictureBox1, pictureBox2, pictureBox2 };
foreach (PictureBox element in pictures)
{
switch (picture)
{
case 1:
this.element.Image = Image.FromFile("C:\Users\seanb\OneDrive\Pictures\cherry.jpg");
break;
case 2:
this.element.Image = Image.FromFile("C:\Users\seanb\OneDrive\Pictures\bell.jpg");
break;
case 3:
this.element.Image = Image.FromFile("C:\Users\seanb\OneDrive\Pictures\lemon.jpg");
break;
case 4:
this.element.Image = Image.FromFile("C:\Users\seanb\OneDrive\Pictures\orange.jpg");
break;
case 5:
this.element.Image = Image.FromFile("C:\Users\seanb\OneDrive\Pictures\star.jpg");
break;
case 6:
this.element.Image = Image.FromFile("C:\Users\seanb\OneDrive\Pictures\skull.jpg");
break;
}
}
像这样使用 Dictionary 怎么样:
Dictionary<int, Image> dictionary = new Dictionary<int, Image>()
{
{1, Image.FromFile("C:\Users\seanb\OneDrive\Pictures\cherry.jpg")},
{2, Image.FromFile("C:\Users\seanb\OneDrive\Pictures\bell.jpg")},
};
dictionary.TryGetValue(picture, out value);
if (value != null)
{
this.element.Image = value;
}
所以你有照片:
static Image[] s_Images = new string[] {
"cherry.jpg",
"bell.jpg",
"lemon.jpg",
"orange.jpg",
"star.jpg",
"skull.jpg"}
.Select(file => Path.Combine(@"C:\Users\seanb\OneDrive\Pictures", file))
.Select(file => Image.FromFile(file))
.ToArray();
static Random random = new Random();
并且您想将这些图像随机分配给图片框:
private void timer1_Tick(object sender, EventArgs e) {
foreach (PictureBox box in new PictureBox[] { pictureBox1, pictureBox2, pictureBox2 }) {
box.Image = s_Images[random.Next(s_Images.Length)];
}
}
Random random = new Random();
int picture = random.Next(1, 7);
var pictureBoxs = new List<PictureBox> { pictureBox1, pictureBox2, pictureBox2 };
string basePath = "C:\Users\seanb\OneDrive\Pictures\";
string[] pictures = new string[]{ "cherry.jpg", "bell.jpg", "lemon.jpg", "orange.jpg", "star.jpg", "skull.jpg" };
foreach (PictureBox element in pictureBoxs)
{
this.element.Image = Image.FromFile(basePath+ pictures[picture]);
}
接近Dictionary
int picture = new Random().Next(1, 7);
Dictionary<int, string> dictionary = new Dictionary<int, string>()
{
{1, "cherry.jpg"},
{2, "bell.jpg"},
{3, "lemon.jpg"},
{4, "orange.jpg"},
{5, "star.jpg"},
{6, "skull.jpg"}
};
string res = "default.jpg";
var pictures = new List<PictureBox> { pictureBox1, pictureBox2, pictureBox2 };
string path = System.IO.Path.Combine("C:\Users\seanb\OneDrive\Pictures\" + dictionary.TryGetValue(picture, out res));
pictures.ForEach(x => x.Image = Image.FromFile(path));
private void timer1_Tick(object sender, EventArgs e)
{
Random random = new Random();
List<string> picPaths = new List<string>();
picPaths.Add("C:\Users\seanb\OneDrive\Pictures\cherry.jpg");
picPaths.Add("C:\Users\seanb\OneDrive\Pictures\bell.jpg");
picPaths.Add("C:\Users\seanb\OneDrive\Pictures\lemon.jpg");
picPaths.Add("C:\Users\seanb\OneDrive\Pictures\orange.jpg");
picPaths.Add("C:\Users\seanb\OneDrive\Pictures\star.jpg");
picPaths.Add("C:\Users\seanb\OneDrive\Pictures\skull.jpg");
pictureBox1.Image = Image.FromFile(picPaths[random.Next(picPaths.Count)]);
pictureBox2.Image = Image.FromFile(picPaths[random.Next(picPaths.Count)]);
pictureBox3.Image = Image.FromFile(picPaths[random.Next(picPaths.Count)]);
}