3D-kreativ
3D-kreativ

Reputation: 9301

Location for a picture box and dynamic creation of user controls

When I'm trying to set a position for a user control that contains a PictureBox and an ImageList, only part of the image is visible!? What is wrong? For the location, I guess pixel is what I can use?

pictureBox1.Location = new Point(10, 20);
pictureBox1.Image = imageList1.Images[3];

Then I also wonder how I can create several user controls, like the one I describe above, during runtime, like some kind of dynamic creation!? I want to set different images and location for each user control. This creation should take place inside another user control and that contains a big panel. I'm not sure how i should do this and how I can communicate between the two user controls? Just like passing values to methods and constructors af these user controls?!

Preciate some help to solve this! Thanks!

EDIT: I'm trying this code right now, but I cant find the reason why it isn't working?

pictureBox1.Size = new System.Drawing.Size(79, 91);
pictureBox1.Location = new Point(10,10);
pictureBox1.Image = imageList1.Images[3];

I still get only parts of the image. And it's strange why the code below works and show 100% of the image?

pictureBox1.Image = imageList1.Images[2];

Upvotes: 1

Views: 1117

Answers (2)

Mahdi Tahsildari
Mahdi Tahsildari

Reputation: 13582

PictureBox[] pics = new PictureBox[10];

        int size = 20;
        for (int i = 0; i < 10; i++)
        {
            pics[i] = new PictureBox();
            pics[i].Size = new System.Drawing.Size(size, size);
            pics[i].Location = new Point(size * 2 * i + 10, size);
            //pics[i].Image = image
            pics[i].BackColor = Color.AliceBlue;
            pics[i].Parent = this;
        }

and for 2D rows of picture boxes you can use nested for loops:

PictureBox[,] pics = new PictureBox[10,10];

        int size = 20;
        for (int i = 0; i < 10; i++)
        {
            for (int j = 0; j < 10; j++)
            {
                pics[i, j] = new PictureBox();
                pics[i, j].Size = new System.Drawing.Size(size, size);
                pics[i, j].Location = new Point(size * 2 * i + 10, size * 2 * j + 10);
                //pics[i,j].Image = image
                pics[i, j].BackColor = Color.AliceBlue;
                pics[i, j].Parent = this;
            }
        }

Upvotes: 1

Justin Harvey
Justin Harvey

Reputation: 14672

Have a look at the SizeMode property on the PictureBox, you may need to set this to StretchImage to fit your image resolution into your PictureBox size.

As for dynamically creating controls, yes you can, just lokk at the code in your designer.cs to see how the code is generated for the controls when you set them up at design time.

Upvotes: 0

Related Questions