Why MouseEventArgs doesn't work when used in an equation

Windows Forms Application:

This puzzled me for several hours. What I ACCEPT to do is when I hold the label it moves the shape.

    private void label1_MouseUp(object sender, MouseEventArgs e)
    {
        KeepMoving = false;
    }

    private void label1_MouseDown(object sender, MouseEventArgs e)
    {
        KeepMoving = true;
    }

    private void label1_MouseMove(object sender, MouseEventArgs e)
    {
        if (KeepMoving == true)
            this.Location = new Point(MousePosition.X - (e.X + SystemInformation.FrameBorderSize.Width + label1.Left), MousePosition.Y - (e.Y + SystemInformation.CaptionHeight + label1.Top));

    }

      

is what I'm using (with the KeepMoving public bool, of course.)

Everything works fine if I remove eX and eY, but then it refers to the position of the top left edge of the label, however I want the position to be on the label itself.

When I use the message box to show me the eX and eY coordinates on the label, the numbers are correct, showing me where on the label I clicked. When I use a dot in the code above, no matter where on the label I click, the number doesn't change and when I try to move it, it goes up to the 30k + range.

Why is MouseEventArgs not working in my equation? Sorry if I described poorly, I tried everything I could.

+3


source to share


1 answer


Observe the inner offset at the top left corner of the mark and adjust the position of the form accordingly.



    public bool KeepMoving = false;
    private Point offset;

    private void label1_MouseDown(object sender, MouseEventArgs e)
    {
        KeepMoving = true;
        offset = new Point(e.X, e.Y);
    }

    private void label1_MouseUp(object sender, MouseEventArgs e)
    {
        KeepMoving = false;
    }

    private void label1_MouseMove(object sender, MouseEventArgs e)
    {
        if (KeepMoving)
        {
            Left += e.X - offset.X;
            Top += e.Y - offset.Y;
        }
    }

      

+2


source







All Articles