I want to thank the helpful posts. I'm now having a new problem. I have created 100 picture boxes at run time and I am giving each a dynamic name (pictureBox_1, pictureBox_2, etc). I want to be able to change the background image of the picture box of my choosing for example. Picturebox_30's background image will be changed? I tried to just use my same picBox instance and created a new point, location, and image but that didn't work? is there a way I can reference a particular pictureBox to change it's background image?

public partial class gameScreen : Form
    {
        PictureBox picBox;
        int xLoc,
            yLoc,
            increment;
        public gameScreen()
        {
            InitializeComponent();
            xLoc = 0;
            yLoc = 0;
            increment = 0;
            drawPictureBoxes();
            changeBGImage();
        }
        private void drawPictureBoxes()
        {
            for (int i = 0; i < 101; i++)
            {
                picBox = new PictureBox();
                picBox.Image = Image.FromFile("Border.jpg");
                picBox.Size = new Size(50, 50);
                picBox.Location = new Point(xLoc, yLoc);
                picBox.Name = "pictureBox_" + i;
                this.Controls.Add(picBox);
                xLoc += 50;
                increment++;
                if (increment == 10)
                {
                    xLoc = 0;
                    yLoc += 50;
                    increment = 0;
                }
            }
        }
        private void changeBGImage()
        {
           /*How do I set for example pictureBox_30's  
            bg Image to a new image? 
           */
          }
    }

Recommended Answers

All 3 Replies

Sorry guys. Literally just found it. Solution:

private void changeBGImage()
{
            PictureBox picBox = this.Controls["pictureBox_30"] as PictureBox;
            picBox.Image = Image.FromFile("Colored.jpg");
}

Good! Please mark as solved.

I often have the same experience. I have a question, but when formulating it, I find the answer!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.