Unable to set location of picturebox 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:
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? :( -
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:
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? :(Hi, some comments: 1. your names are quite confusing, you should change them: PlaceFlower() does not place a flower, it creates a PictureBox that holds a flower, but it does not make it visible and does not set its location; GetFlowerBox() is a better name. SetCoordinates() does not set coordinates, it creates and returns a Point, but does not do anything with the Point. GetRandomPoint() would be a more appropriate name. Choosing proper names will help you in understanding your own code, today and in future. 2. your code is not complete, and the problem is likely to reside it what is not shown. My best guess is you have two PictureBoxes: - if you already see a flower, it is not the one from flowerPicture; I suspect you have some other PictureBox that shows the flower you see, and is not affected by your attempts to move it; - the flowerPicture PictureBox, as far as the code shows, never becomes visible, since it is not added to the Controls property of any Container or Form. :)
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.
-
Hi, some comments: 1. your names are quite confusing, you should change them: PlaceFlower() does not place a flower, it creates a PictureBox that holds a flower, but it does not make it visible and does not set its location; GetFlowerBox() is a better name. SetCoordinates() does not set coordinates, it creates and returns a Point, but does not do anything with the Point. GetRandomPoint() would be a more appropriate name. Choosing proper names will help you in understanding your own code, today and in future. 2. your code is not complete, and the problem is likely to reside it what is not shown. My best guess is you have two PictureBoxes: - if you already see a flower, it is not the one from flowerPicture; I suspect you have some other PictureBox that shows the flower you see, and is not affected by your attempts to move it; - the flowerPicture PictureBox, as far as the code shows, never becomes visible, since it is not added to the Controls property of any Container or Form. :)
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.