NelsonRios
NelsonRios

Reputation: 85

Obtaining the position of the mouse on the form for a button object

Although I posted something related to this (which cost me many down-votes), I decided to continue with my original attempt to do it by myself (with some good results), but I bumped into a problem, and I cannot see how to solve it.

My code creates a set of n clickable buttons, which should be moved when pressed the button. I already managed to do it, but when i click it and move them, they "jump" in a weird manner, not repositioning how I want them to, and then they can be moved freely.

This is the code of the button class:

namespace moverButtons 
{
    class buttonCito:Button
    {

        Point posActual;
        bool mousePressed;

       // public MouseEventHandler MouseMove;
        public buttonCito(int altUra, int anchUra, Point position)
        {
            this.Height = altUra;
            this.Width = anchUra;
            this.Location = position;

        }

        public buttonCito()
        {
            // TO DO: Complete member initialization
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {

            mousePressed = (e.Button == MouseButtons.Left) ? true : false;

            if (e.Location != null && mousePressed)
            {
                    moverButton(e.X, e.Y);

            }        

            //Añadir rutina para mover con el mouse 
            //Add routine to move with the mouse
        }
        public void moverButton(int x,int y)
        {

            this.Location = new Point(x + posActual.X, y + posActual.Y);
            posActual = this.Location;
        }

    }
}

And this is the code of the form:

namespace moverbuttons

{   
    public partial class Form1 : Form
    {
        Point positionMouseForm;

        public Form1()
        {
            InitializeComponent();

            this.MouseMove += new MouseEventHandler(map_MouseMove);

        }

        private void map_MouseMove(object sender, MouseEventArgs e)//obtains the position of the mouse on the form
        {   //I want to give this position to the button class, is there  a way?
            positionMouseForm = e.Location;
        }

        private void Form1_Load(object sender, EventArgs e)
        {   List<buttonCito> buttons = new List<buttonCito>();
            Random rnd = new Random();
            int x,y;

            for (int i = 0; i < 5; i++)
            {
                x = rnd.Next(1, 300);
                y = rnd.Next(1, 300);
                buttonCito newButton = new buttonCito(50,50,new Point(x,y));


                buttons.Add(newButton);
                this.Controls.Add(newButton);
            }
        }
    }
}

If could, somehow give the position of the mouse on the form to the buttons, I could easily fix it.

Upvotes: 2

Views: 96

Answers (1)

BradleyDotNET
BradleyDotNET

Reputation: 61339

One way to approach this problem would be to use a delta (the difference between two objects).

Point lastMousePosition;

private void MoveButton(int currentX, int currentY)
{
     int deltaX = currentX - lastMousePosition.X;
     int deltaY = currentY - lastMousePosition.Y;

     this.Location = new Point(Location.X + deltaX, Location.Y + deltaY);
     lastMousePosition = new Point(currentX, currentY);
}

You would set lastMousePosition initially to the mouse position when the button is pressed (and not again until it is released).

Note that repeatedly calling constructors like this is not very performant. You may want to consider reusing a Point object you instantiate only once. Of course you should only worry about that once you have eveything working :)

Upvotes: 2

Related Questions