Click here to Skip to main content
15,911,030 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have this xxx_MouseMove method. In it I manage to make the functionality for dragging a button and with it a custom control for a keyboard with Russian characters.
Right NOW when I click on that button, my mouse pointer move to 0,0 location of that button, which is the top left corner.
I don't want this behavior. I want that wherever i click inside that button, when I move the mouse, its pointer remain where I click the first time, and not reset to the top left corner.
Any ideas?
Here is my code:
And as always, many thanks for your great help you give to us (the little ones).
 bool dragging = false;
  private void ButtonMoveKeyboard_MouseMove(object sender, MouseEventArgs e)
        {
            if (dragging)
            {
                Point MPosition = new Point();
                MPosition = this.PointToClient(MousePosition);

//Point buttonCentre = new Point(ButtonMoveKeyboard.Width, ButtonMoveKeyboard.Height);
//Point p = ButtonMoveKeyboard.PointToScreen(buttonCentre);
//Cursor.Position = p;.Position = p;

                ButtonMoveKeyboard.Location = new Point(MPosition.X, MPosition.Y - 10);
                ButtonMoveKeyboard.Refresh();
                russianKeyboard1.Location = new Point(ButtonMoveKeyboard.Location.X, MPosition.Y);
            }
        }

private void MoveBtnForRusKeyboard_MouseDown(object sender, MouseEventArgs e)
        {
            dragging = true;
            ButtonMoveKeyboard.BackColor = Color.LimeGreen;
        }
 private void MoveBtnForRusKeyboard_MouseUp(object sender, MouseEventArgs e)
        {
            if (dragging) dragging = false;
            ButtonMoveKeyboard.BackColor = Color.SandyBrown;
        }
Posted
Updated 24-Nov-11 7:03am
v2
Comments
LanFanNinja 24-Nov-11 12:31pm    
Check my solution
_Q12_ 24-Nov-11 12:33pm    
Im just looking on it...but is a custom class...and I hardly see what I need there... maybe some other link perhaps? -thx.
LanFanNinja 24-Nov-11 12:48pm    
Just thought it might give you an idea of something you could do in your code.

I have had this problem before (a long time ago) right now I cannot remember how I fixed it so maybe this will help you figure it out
Move controls on a form at runtime[^]
 
Share this answer
 
Your code is not working because you are confusing Form co-ordinate space with Button co-ordinate space.

You need to record the Point where the Mouse goes down on the Button: that Point will be in Button co-ordinate space: i.e., relative to the upper-left of the Button as 0,0. And, that Point is available in the MouseEventArgs instance passed into the Button MouseDown EventHandler, accessible through the variable name 'e.

When you move the Mouse, you need to calculate an offset between the Point where the Mouse is right now ... in the MouseMove EventHandler ... (once again available in the 'e variable), and the Point the Mouse first went down.

That offset, then, can be applied to re-locate the Button, and your current Mouse-Cursor position will be maintained.

Then that same offset can be used to relocate your RussianKeyboard control.

Try this:
C#
private bool dragging = false;

    // where the Mouse first goes down
    private int mX;
    private int mY;

    // for calculating the offset in the MouseMove EventHandler
    private int dX;
    private int dY;

    private void ButtonMoveKeyBoard_MouseDown(object sender, MouseEventArgs e)
    {
        dragging = true;

        mX = e.X;
        mY = e.Y;

        ButtonMoveKeyboard.BackColor = Color.LimeGreen;
    }

    private void ButtonMoveKeyBoard_MouseUp(object sender, MouseEventArgs e)
    {
        dragging = false;

        ButtonMoveKeyboard.BackColor = Color.SandyBrown;
    }

    private void ButtonMoveKeyBoard_MouseMove(object sender, MouseEventArgs e)
    {
        if (dragging)
        {
            dX = e.X - mX;
            dY = e.Y - mY;

            ButtonMoveKeyboard.Left += dX;
            ButtonMoveKeyboard.Top += dY;

            russianKeyboard1.Left += dX;
            russianKeyboard1.Top += dY;
        }
    }
}
In this example, I'm using variables dX, dY, to store the calculated offsets because: they are used twice.
 
Share this answer
 
Comments
_Q12_ 24-Nov-11 12:41pm    
Thanks BillWoodruff, you saved the day for me. :)
Your code is wonderful. +5
LanFanNinja 24-Nov-11 12:46pm    
+5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900