1

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];
4

2 回答 2

1
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;
        }

对于图片框的 2D 行,您可以使用嵌套的 for 循环:

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;
            }
        }
于 2012-08-15T08:59:39.330 回答
0

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.

于 2012-08-15T08:52:07.260 回答