0

I am trying to make my code as short as possible and I intend on randomising images from a choice of 6 pictures for 3 different picture boxes at certain intervals. Instead of copy and pasting the switch and case three times for each picturebox, I am trying to instead change in one foreach loop. I am very new to C# and windows forms so any help is appreciated. EDIT:Sorry for poor wording, but the problem was that the program says element is not a valid picturebox and I am wondering why and how to fix it if possible, but thanks for the alternate solutions.

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

5 Answers5

3

What about using a Dictionary like this:

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;
}
Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
0

So you have pictures:

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();

And you want to assign these images to the picture boxes randomly:

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)];
  }
}
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
0
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]);
}
Mihir Dave
  • 3,954
  • 1
  • 12
  • 28
0

Approach with 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));
Toshi
  • 2,532
  • 4
  • 17
  • 45
0
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)]);
        }