Using a 'for' loop in C# to fill picturebox number 'i' with an image

10,798

Solution 1

Put references to the controls in an array:

PictureBox[] boxes = {
  PictureBox1, PictureBox2, PictureBox3, PictureBox4, PictureBox5, PictureBox6
};

Then you can loop through them:

for (int i = 0; i < boxes.Length; i++) {
  // use boxes[i] to access each picture box
}

Solution 2

You can use Tag property of any control to provide additional info (like image name or index). E.g. you can provide index for your pictureBoxes. Also use ImageList to store list of images:

foreach(var pictureBox in Controls.OfType<PictureBox>())
{
    if (pictureBox.Tag == null) // you can skip other pictureBoxes
        continue;

    int imageIndex = (int)pictureBox.Tag;
    pictureBox.Image = imageList.Images[imageIndex];
}

Also you can search picture box by tag value:

var pictureBox = Controls.OfType<PictureBox>()
                         .FirstOrDefault(pb => (int)pb.Tag == index);

Another option - if all your pictureBoxes have names like pictureBox{index}. In this case you can go without usage of Tag:

var pictureBox = Controls
  .OfType<PictureBox>()                     
  .FirstOrDefault(pb => Int32.Parse(pb.Name.Replace("pictureBox", "")) == index);

Solution 3

I implement this with Array of PictureBoxes :

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace ImageChanger
{
    public partial class Form1 : Form
    {
        PictureBox[] pictureBoxs=new PictureBox[4];

        public Form1()
        {
            InitializeComponent();


            pictureBoxs[0] = pictureBox1;
            pictureBoxs[1] = pictureBox2;
            pictureBoxs[2] = pictureBox3;
            pictureBoxs[3] = pictureBox4;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < 4; i++)
            {
                // Load Image from Resources
                pictureBoxs[i].Image = Properties.Resources.img100;

                Application.DoEvents();
                Thread.Sleep(1000);
            }
        }


    }
}
Share:
10,798

Related videos on Youtube

Holy Goat
Author by

Holy Goat

Updated on September 28, 2022

Comments

  • Holy Goat
    Holy Goat over 1 year

    I have six Pictureboxes and a for loop. I'd like to refer to PictureBox with the same number as 'i' variable that's being incremented inside a for loop.

    For example, if the 'i' variable from my for loop is 2 I'd like to assign an image to picturebox2.

    How do I do this the easiest way? It is a unique way that also works for other controls (that is, labels, textboxes) would be nice. :)

  • Holy Goat
    Holy Goat over 11 years
    I've tried this before but I get Error 2 The name 'PictureBox2' does not exist in the current context
  • Holy Goat
    Holy Goat over 11 years
    I understand imagelist idea, I don't understand how to use tag property to i.e. fill picturebox3 with image if for loop iterator == 3 though.
  • Holy Goat
    Holy Goat over 11 years
    I've tried this before but I get Error 2 The name 'PictureBox2' does not exist in the current context
  • Sergey Berezovskiy
    Sergey Berezovskiy over 11 years
    There is two options - either provide Tag from code pictureBox1.Tag = 1, or from designer - select pictureBox and set it's Tag property. But in second case Tag will contain string! Be careful.
  • Enigmativity
    Enigmativity over 11 years
    @HolyGoat - This answer would work - but you may need to define your array using pictureBox2 rather than PictureBox2. Note the difference between camel case and pascal case.
  • Guffa
    Guffa over 11 years
    @HolyGoat: Check what the id of the control really is. If PictureBox1 is accessible but not PictureBox2, then it suggests that the scope is right, but there isn't actually any picture box with the id PictureBox2.
  • Alaa M.
    Alaa M. over 10 years
    @lazyberezovsky kinda irrelevant: can you please provide the code using the Tag for visual c++? thank you
  • Sergey Berezovskiy
    Sergey Berezovskiy over 10 years
    @AlaaM. unfortunately last time I used C++ was about 10 years ago. So I think you will handle this task better than me :)