Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Unable to set location of picturebox programatically

Unable to set location of picturebox programatically

Scheduled Pinned Locked Moved C#
questioncsharphelp
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    KaKa
    wrote on last edited by
    #1

    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? :(

    L 1 Reply Last reply
    0
    • K KaKa

      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? :(

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      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.


      K 1 Reply Last reply
      0
      • L Luc Pattyn

        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.


        K Offline
        K Offline
        KaKa
        wrote on last edited by
        #3

        Yup, I guess you are right. When I added the control to the form, it can work now. Thanks~ :)

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups