Click here to Skip to main content
15,917,329 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
This is my code

C#
private void You_MouseDown(object sender, MouseEventArgs e)
        {
            You.Location = Cursor.Position;
        }




Mt problem is that its not following it, the picturebox(You) is just going to some random location and not following the cursor, can anyone help?
Posted

Here is the code that works:
C#
private void You_MouseDown(object sender, MouseEventArgs e)
{
  You.Location = e.Location;
}


or this solution that converts the Cursor position to client coordinates:
C#
private void You_MouseDown(object sender, MouseEventArgs e)
{
  You.Location = PointToClient(Cursor.Position);
}
 
Share this answer
 
v2
Comments
[no name] 15-Feb-11 1:33am    
Its doing better then it was but, not the effect i want, for some reason its when i click it, it goes to the location near the cursor, i want it to follow the cursor while my mouse is holding down on it
JF2015 15-Feb-11 1:35am    
The code you posted changes the mouse position when you move click the mouse button - you need to use a different event when you want to change the position every time the mouse moves.
Sergey Alexandrovich Kryukov 15-Feb-11 17:09pm    
You did not finish the implementation. You should also process mouse up. Also consider capturing mouse. Bad naming of the methods (Microsoft violates its own naming rules in generated code, but this is not a valid reason to follow bad practice), anonymous methods is most adequate.
--SA
JF2015 16-Feb-11 0:18am    
Since the OP did not exactly tell us what he wanted and only told us to use the MouseDown event, this is the correct solution. I added the other answer after the OP told us exactly what he wanted to achieve. I also did follow the naming convention of the OP so that it might be easier for him to include the snippets in his own code (but yes, you are totally right that the naming is bad). Sorry to have disappointed you but I think -40 is a bit harsh...
Alternative answer that moves a button when the mouse moves:
C#
bool m_bMouseDown = false;

private void Form1_MouseDown(object sender, MouseEventArgs e)
{
  button1.Location = PointToClient(Cursor.Position);
  m_bMouseDown = true;
}

private void Form1_MouseMove(object sender, MouseEventArgs e)
{
  if (m_bMouseDown)
    button1.Location = PointToClient(Cursor.Position);
}

private void Form1_MouseUp(object sender, MouseEventArgs e)
{
  m_bMouseDown = false;
}
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 15-Feb-11 17:09pm    
Much better.
--SA
Sergey Alexandrovich Kryukov 15-Feb-11 17:11pm    
Could be better is you discriminate which button is pressed, even it was not a requirement.
Also, this is not alternative, this is a fix to your incomplete code.
--SA
You can easily use this code
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                PictureBox b = ((PictureBox) sender);
                b.Location = new Point(b.Left + e.X, b.Top + e.Y);

            }
        }
 
Share this answer
 
Comments
[no name] 15-Feb-11 1:50am    
This is the best code so far, but its just not moving with the cursor
Xgener 15-Feb-11 2:04am    
May be I not uderstand... You should press and hold left button of the mouse, and move it. Then your picturebox would move with cursor
[no name] 16-Feb-11 0:00am    
I hold down the left mouse and the object follows the mouse with the left mouse down

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