Click here to Skip to main content
15,885,915 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
I am having a problem with this code. When I start the program the rectangle (Ruler) is in the center of the page. When I mousemove when MouseDown is true, the Rectangle (Ruler) is dragable as I want. However, this only works on the first drag. The next time I go to drag it the Ruler jumps back to it's original position, then when you mouse over it the distance from where it was to where it jumped back is calculated and it jumps off the screen as the mouseup event doesn't fire as the rectangle has moved. I basically want to be able to drag the object around the screen however many times I want, but the XStart and YStart need to take the new rendered values on each click.

I think the reason has to do with the e.GetPosition(this).X; as 'this' refers to the Grid that is the rulers parent.

From digging around I notice that When I click on it first XStart and YStart are equal to grid positions X and Y as expected, this is subtracted from the mouse move position to create an offset. The second time I click the behavior is exactly the same, however, it jumps back to the center. My guess is on the first click it makes a transformation but doesn't commit it to memory maybe, then on the second click rather than translating its current position I believe it might be translating the original values again.

Is it possible the margins or centering could be causing it?

Do I need to commit the RenderTransform to the program? or is there an error in my logic?

It would honestly make more sense if it didn't work at all, but to work perfectly once, then screw up makes no sense.

Here is the code:

C#
private void Rectangle_MouseDown(object sender, MouseButtonEventArgs e)
{
    XStart = e.GetPosition(this).X;
    YStart = e.GetPosition(this).Y;

    Console.WriteLine("X: " + XStart + ", Y: " + YStart);

    MouseDown = true;

}

private void Rectangle_MouseMove(object sender, MouseEventArgs e)
{

    if(MouseDown)
    {
        X = e.GetPosition(this).X - XStart;
        Y = e.GetPosition(this).Y - YStart;

        Ruler.RenderTransform = new TranslateTransform(X, Y);
    }
}

private void Ruler_MouseUp(object sender, MouseButtonEventArgs e)
{
    MouseDown = false;
}
Posted

1 solution

In your Rectangle_MouseMove method you calculate the TranslateTransform according to the mouse position in the MouseDown event. In the first time the MouseMove difference is equal to the wanted TranslateTransform but, after that, the MouseMove difference is according to the last transform...


When you create a new TranslateTransform with the MouseMove difference, it translates it according to the original position. Try to add the MouseMove difference to the exist transform instead of ignoring the old transform and creating a new one.

 
Share this answer
 
v3
Comments
Graham Coulby 30-Jul-15 3:16am    
OK so I set the property in the class to
TranslateTransform Translate = new TranslateTransform();

and changed ruler_mousedown to this

private void Ruler_MouseMove(object sender, MouseEventArgs e)
{

if(IsMouseDown)
{
X = e.GetPosition(Grid).X - XStart;
Y = e.GetPosition(Grid).Y - YStart;

Translate.X = X;
Translate.Y = Y;

Ruler.RenderTransform = Translate;
}
}


However, the problem persists, is it possible that it is because of the e.GetPosition(Grid)
Graham Coulby 30-Jul-15 3:23am    
OK, you are a legend!

The problem was to do with the new TranslateTransform in a sense. By storing the TranslateTransform as a property I can modify the XStart and YStart

I changed Ruler_MouseDown to this

private void Ruler_MouseDown(object sender, MouseButtonEventArgs e)
{

XStart = e.GetPosition(Grid).X - Translate.X;
YStart = e.GetPosition(Grid).Y - Translate.Y;

IsMouseDown = true;
}

And now it works. Thank you very much!!!!!

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