1

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;
    }
9
  • 1
    What are you trying to achieve ? Commented Apr 15, 2016 at 16:10
  • @Aybe I was trying to display 13 pictureBox, but it only show me the last one, so I was wondering if I do it in a wrong way. btw, thanks for your reply Commented Apr 15, 2016 at 16:16
  • Yes but how are you trying to display, horizontally, vertically, etc ... explain exactly what you need. Commented Apr 15, 2016 at 16:22
  • Yes,, exactly.. I was trying to display the pictureBox horizontally. Like this [][][][][]... X 13 Commented Apr 15, 2016 at 16:25
  • So you want to display boxes horizontally and eventually have a scroll bar if there are too many to see at once on your screen ? Commented Apr 15, 2016 at 16:27

2 Answers 2

1

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]); 
Sign up to request clarification or add additional context in comments.

1 Comment

it is a mistake I made when I was debugging, thanks for pointing that out !
1

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.

1 Comment

omg... can't believe you solve my problems with such clean code!! Thanks man!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.