0

The pictureBox prints at the same location regardless of where I want it to print. The airplane prints here regardless of where I try to set the location of the pictureBox.

 private void Form1_MouseClick_1(object sender, MouseEventArgs e)
    {
        int locationX = Cursor.Position.X;
        int locationY = Cursor.Position.Y;
        this.Text = Convert.ToString(Cursor.Position.X + ":" + Cursor.Position.Y);
        MessageBox.Show(this.Text);
        drawPlane(ref locationX, ref locationY);


    }

    private void drawPlane(ref int argx, ref int argy)
    {

        //PictureBox pictureBox = new PictureBox();
        //pictureBox.Location = new Point(400, 400);
        //pictureBox.Image = Properties.Resources.whitePlane1;

        //this.Controls.Add(pictureBox);

        var picture = new PictureBox
        {
            Name = "pictureBox",
            Size = new Size(16, 16),
            Location = new Point(100, 800),//command not followed, prints at same location every time
            Image = Properties.Resources.whitePlane1,

        };
        this.Controls.Add(picture);

    }

How do I regain control of the location of my pictureBox?

Here is my full code.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace csci363x300
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            DrawEllipse();
        }


        private void DrawEllipse()
        {
            System.Drawing.Pen myPen = new System.Drawing.Pen(System.Drawing.Color.Green);
            System.Drawing.Graphics formGraphics;
            formGraphics = this.CreateGraphics();
            int scale = 5;
            int centerSpace = 100;
            formGraphics.DrawEllipse(myPen, new Rectangle(0 * scale + centerSpace, 0 * scale + centerSpace, 100 * scale, 100 * scale));
            formGraphics.DrawEllipse(myPen, new Rectangle(10 * scale + centerSpace, 10 * scale + centerSpace, 80 * scale, 80 * scale));
            formGraphics.DrawEllipse(myPen, new Rectangle(20 * scale + centerSpace, 20 * scale + centerSpace, 60 * scale, 60 * scale));
            formGraphics.DrawEllipse(myPen, new Rectangle(30 * scale + centerSpace, 30 * scale + centerSpace, 40 * scale, 40 * scale));
            formGraphics.DrawEllipse(myPen, new Rectangle(40 * scale + centerSpace, 40 * scale + centerSpace, 20 * scale, 20 * scale));
            int height = 100 * scale;
            int width = 100 * scale;

            Rectangle[] rects =
                     {
                 //new Rectangle( centerSpace, centerSpace, 100*scale, 100*scale),
                 //new Rectangle(centerSpace+10*scale, centerSpace, 250,  50),
                 //new Rectangle(300,   0,  50, 100)

                 new Rectangle(0*scale + centerSpace, centerSpace, 100*scale, height),
                 new Rectangle(10 * scale + centerSpace, centerSpace, 80 * scale, height),
                 new Rectangle(20 * scale + centerSpace, centerSpace, 60 * scale, height),
                 new Rectangle(30 * scale + centerSpace, centerSpace, 40 * scale, height),
                 new Rectangle(40 * scale + centerSpace, centerSpace, 20 * scale, height),
                 new Rectangle(centerSpace, centerSpace, width, 80*scale)
            };

            Pen redPen = new Pen(Color.Red, 1);
            formGraphics.DrawRectangles(redPen, rects);
            redPen.Dispose();
            myPen.Dispose();
            formGraphics.Dispose();

        }



        private void Form1_MouseClick_1(object sender, MouseEventArgs e)
        {
            int locationX = Cursor.Position.X;
            int locationY = Cursor.Position.Y;
            this.Text = Convert.ToString(Cursor.Position.X + ":" + Cursor.Position.Y);
            MessageBox.Show(this.Text);
            drawPlane(ref locationX, ref locationY);


        }

        private void drawPlane(ref int argx, ref int argy)
        {

            //PictureBox pictureBox = new PictureBox();
            //pictureBox.Location = new Point(400, 400);
            //pictureBox.Image = Properties.Resources.whitePlane1;

            //this.Controls.Add(pictureBox);

            var picture = new PictureBox
            {
                Name = "pictureBox",
                Size = new Size(8, 10),
                Location = new System.Drawing.Point(argx, argy),//command not followed, prints at same location every time, approx (200,200)
                Image = Properties.Resources.whitePlane1,

            };
            this.Controls.Add(picture);

        }
    }
}

Above is my full code. Perhaps a property of Form1 is causing this issue.

Nick Knapp
  • 23
  • 5
  • Note that the `[visual-studio]` tag should only be used for questions about the Visual Studio application. The `[location]` tag states that it refers to physical locations only. You can hover over any tag to see a short description of them in order to determine if your question is consistent with the tag's outlined usage. – ProgrammingLlama Dec 08 '19 at 01:23
  • `//command not followed, prints at same location every time` - you are setting the location to 100,800 every time. What do you expect to happen in this scenario? Perhaps you meant `new Point(argx, argy)`. FYI You are also creating a new plane every click. Oh, and I don't understand why `argx` and `argy` are passed as `ref` since you're not changing them in the `drawPlane` method. – ProgrammingLlama Dec 08 '19 at 01:24
  • You are telling it to always appear at location 100, 800. You are ignoring the argx and argy parameters (which shouldn't be marked as ref). – LarsTech Dec 08 '19 at 01:25
  • I was planning on using argx and argy but opted for setting location manually to test the code when those didn't work. The image prints to the same location regardless of what numbers I put in the Point () method. – Nick Knapp Dec 08 '19 at 01:34
  • The Size () method also does not do anything. – Nick Knapp Dec 08 '19 at 01:35
  • What do you expect to happen? I would expect a 16x16 plane to be added at x 100, y 800. And all subsequent clicks would add another plane of the exact same size in the exact same location. If 800 is off the bottom of the window, you won't see it. – ProgrammingLlama Dec 08 '19 at 01:40
  • The plane prints to approx. (200,200) regardless of what I set the location to be. – Nick Knapp Dec 08 '19 at 01:57
  • I ran your code and it adds my image at 100,800. Changing the coded value to 200,200 an re-running the application causes it to add it at 200,200. Please advise how to reproduce your scenario. Likewise, modifying your code to pass the e.X and e.Y values from the mouseclick event into your `drawPlane` method, and then using `argx` and `argy` instead of 100 and 800 results in a new plane being added wherever I click. – ProgrammingLlama Dec 08 '19 at 02:03
  • is there a Form property that could be causing this? – Nick Knapp Dec 08 '19 at 02:19
  • I don't believe so. Do you have a pre-existing plane picturebox that you could be looking at by mistake? – ProgrammingLlama Dec 08 '19 at 02:21
  • I commented out the "this.Text = convertToString..." and "MessageBox.Show(this.text);" lines, saved it and ran it and it ran exactly the same as if those lines were not commented out. – Nick Knapp Dec 08 '19 at 02:45
  • I started a new project, copied, pasted, and tested and it works as intended. I still do not know what was wrong with my previous project. – Nick Knapp Dec 08 '19 at 03:07

0 Answers0