Domanda

I have a panel, that can be moved left or right (chosen automatically according to its current position, distance is static) by click. Also, the user can drag the panel vertically however he wants by clicking the panel, holding the button and moving his mouse. The problem is, that the panel does the left/right move also, when it is dropped after being moved vertically, so the user has to click it again afterwards, to get to correct side (left/right). Here are the methods I am using: Adding eventhandlers to panel(called Strip here)

Strip.MouseDown += new MouseEventHandler(button_MouseDown);
Strip.MouseMove += new MouseEventHandler(button_MouseMove);
Strip.MouseUp += new MouseEventHandler(button_MouseUp);
Strip.Click += new EventHandler(strip_Click);

And here all the methods mentioned above:

void button_MouseDown(object sender, MouseEventArgs e)
    {
        activeControl = sender as Control;
        previousLocation = e.Location;
        Cursor = Cursors.Hand;
    }

void button_MouseMove(object sender, MouseEventArgs e)
    {
        if (activeControl == null || activeControl != sender)
            return;

        var location = activeControl.Location;
        location.Offset(0, e.Location.Y - previousLocation.Y);
        activeControl.Location = location;
    }

void button_MouseUp(object sender, MouseEventArgs e)
    {
        activeControl = null;
        Cursor = Cursors.Default;
    }

void strip_Click(object sender, EventArgs e) // The one moving strip to left or right
    {
        activeControl = sender as Control;
            if (activeControl.Left != 30)
                activeControl.Left = 30;
            else
                activeControl.Left = 5;
    }

How to make the panel not move left or right when it was moved vertically?

È stato utile?

Soluzione

You'll need to distinguish between a click and a drag. So add a private field named "dragged".

    private bool dragged;

In the MouseDown event handler add:

    dragged = false;

In the MouseMove event handler add:

    if (Math.Abs(location.Y - previousLocation.Y) > 
        SystemInformation.DoubleClickSize.Height) dragged = true;

In the Click event handler add:

    if (dragged) return;
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top