[RESOLVED] Unable to set picturebox location programatically
I have a picturebox which was created programatically and its location should be determined when the form loads.
I set the location property to a point but no matter what point is set, the picturebox appears on a fixed location when the form loads.
Below is the code:
Code:
In the picture class:
public PictureBox PlaceFlower()
{
PictureBox flower = new PictureBox();
flower.Height = 80;
flower.Width = 80;
flower.Image = Image.FromFile("C:\\grass.bmp");
return flower;
}
public Point SetCoordinates()
{
int x = randomClass.Next(10,50);
int y = randomClass.Netxt(10,50);
Point p = new Point(x, y);
return p;
}
In the form class:
private PictureBox flowerPicture;
private void Form1_Load(object sender, EventArgs e)
{
flowerPicture = picture.PlaceFlower();
flowerPicture.Location = picture.SetCoordinates();
}
I've checked the values of the point coordinates and they vary each time the form loads but somehow the picturebox always remain fixed at the same point on the form. I have made sure that the locked property of the control is set to false.
What is the problem with the picturebox location? :(
Re: Unable to set picturebox location programatically
You're changing the reference of the PictureBox on the main form with the one that your picture class provides. This may be the reason. Try:
Code:
PictureBox pb = picture.PlaceFlower();
pb.Location = picture.SetCoordinates();
this.Controls.Add(pb);
I'm not sure how the runtime reacts to control references being changes programatically, but I imagine that's your problem. If it's not then someone smarter than me may be able to help. :)
Re: Unable to set picturebox location programatically
Yep, I guess the problem is that the control is not actually added to the form. Thanks!~ :)