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.