Edwardhk
Edwardhk

Reputation: 97

Displaying pictureBox array

I would like to display 13 pictureBox, however, it ends up with only the last one visible. So I was wondering if I did it in a wrong way.

The following code get image from resources folder.

var testP = new PictureBox();
for (int i = 0; i < 13; i++)
{
    testP.Width = 65;                                   
    testP.Height = 80;
    testP.BorderStyle = BorderStyle.None;
    testP.SizeMode = PictureBoxSizeMode.StretchImage;
    test[i] = getImage(testP, testPTemp[i]);              
}

The following code is trying to display 13 pictureBox with shifting location.

These two codes segments should be able to perform the action.

test = new PictureBox[13];    
for (var i = 0; i < 13; i++)
{
    test[i].Image = (Image)Properties.Resources.ResourceManager.GetObject("_" + testTemp[i]);    
    test[i].Left = 330;      
    test[i].Top = 500;      
    test[i].Location = new Point(test[i].Location.X + 0 * displayShift, test[i].Location.Y);  
    this.Controls.Add(test[i]);
}

Here is the getImage()

private PictureBox getImage(PictureBox pB, string i)               // Get image based on the for loop number (i)
    {
        pB.Image = (Image)Properties.Resources.ResourceManager.GetObject("_" + i);           // Get the embedded image
        pB.SizeMode = PictureBoxSizeMode.StretchImage;
        return pB;
    }

Upvotes: 1

Views: 1078

Answers (2)

amura.cxg
amura.cxg

Reputation: 2427

It's hard to tell the exact problem based off the code you've provided. One possible issue could be that when you are creating the PictureBoxes you only create a single instance before the for loop and then fill the array with references to that instance. Another possibility is that when you're calculating the X position of the controls, you're multiplying by 0 which will always result in 0 (meaning all the controls are at location 330).

Below is code that will achieve basically what you're trying but without all your code I can't give you a more specific example.

In Your Class

const int PICTURE_WIDTH = 65;
const int PICTURE_HEIGHT = 85;

Inside You Function

//Loop through each image
for(int i = 0; i < testTemp[i].length; i++)
{
    //Create a picture box
    PictureBox pictureBox = new PictureBox();

    pictureBox.BorderStyle = BorderStyle.None;
    pictureBox.SizeMode = PictureBoxSizeMode.StretchImage;

    //Load the image date
    pictureBox.Image = (Image)Properties.Resources.ResourceManager.GetObject("_" + testTemp[i]);

    //Set it's size
    pictureBox.Size = new Size(PICTURE_WIDTH, PICTURE_HEIGHT);

    //Position the picture at (330,500) with a left offset of how many images we've gone through so far
    pictureBox.Location = new Point(330 + (i * PICTURE_WIDTH), 500);

    //Add the picture box to the list of controls
    this.Controls.Add(pictureBox);
}

If you need to keep a list of the picture boxes, just create a new list before the loop and add each pictureBox to the list inside the loop. If the control/window you're adding these PictureBoxes to needs to scroll left or right to see all the images set the AutoScroll property to true.

Upvotes: 1

Timo
Timo

Reputation: 9835

I'm pretty sure there are all PictureBox Controls but they have all the same location so they are lying above each other. That's why only the last one is visible to you.

I think you should replace the 0 with the i variable.

test[i].Location = new Point(test[i].Location.X + i * displayShift, test[i].Location.Y); this.Controls.Add(test[i]); 

Upvotes: 1

Related Questions