TermSpar
TermSpar

Reputation: 441

C# Dynamically Move Picturebox Class With Mouse

I'm currently trying to make a video game level editor and am trying to have it where the user can add objects to the screen and then manipulate their locations and sizes and what not. I'm starting off by just having a platform class, and this is the method I'm using to allow the user to edit the location of the platforms:

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 LevelEditor
{
    class Platform : PictureBox
    {

        private PictureBox platform = new PictureBox();

        public Platform(int width, int height, int x, int y)
        {
            platform.Width = width;
            platform.Height = height;
            platform.Location = new Point(x, y);
            platform.BackColor = Color.Red;
            platform.BorderStyle = BorderStyle.FixedSingle;
        }

        public void drawTo(Form form)
        {
            form.Controls.Add(platform);
        }

        public void setPosition(int x, int y)
        {
            platform.Location = new Point(x, y);
        }

        private Point MouseDownLocation;

        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);
            if (e.Button == MouseButtons.Left)
            {
                MouseDownLocation = e.Location;
            }
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);

            if (e.Button == MouseButtons.Left)
            {
                platform.Left = e.X + platform.Left - MouseDownLocation.X;
                platform.Top = e.Y + platform.Top - MouseDownLocation.Y;
            }
        }
    }
}

The problem is that it's not working. I know that the Platform is not even detecting the MouseDown event because I've debugged it. I'm not sure what the problem is and any help is appreciated!

Upvotes: 1

Views: 195

Answers (1)

TermSpar
TermSpar

Reputation: 441

The problem was, as pointed out in the comments, I was inheriting from Picturebox, but using an instance of a Picturebox variable as the object itself. The corrected code is this:

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 LevelEditor
{
    class Platform : PictureBox
    {

        public Platform(int width, int height, int x, int y)
        {
            this.Width = width;
            this.Height = height;
            this.Location = new Point(x, y);
            this.BackColor = Color.Red;
            this.BorderStyle = BorderStyle.FixedSingle;
        }

        public void drawTo(Form form)
        {
            form.Controls.Add(this);
        }

        public void setPosition(int x, int y)
        {
            this.Location = new Point(x, y);
        }

        private Point MouseDownLocation;

        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);
            if (e.Button == MouseButtons.Left)
            {
                MouseDownLocation = e.Location;
            }
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);

            if (e.Button == MouseButtons.Left)
            {
                this.Left = e.X + this.Left - MouseDownLocation.X;
                this.Top = e.Y + this.Top - MouseDownLocation.Y;
            }
        }
    }
}

Upvotes: 1

Related Questions