0

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?

Marek Buchtela
  • 973
  • 3
  • 19
  • 42
  • How to make the panel not move left or right when it was moved vertically. – Marek Buchtela Jan 03 '13 at 15:17
  • @HansPassant Tried replacing 0 with 1, as soon as I click the panel, it starts moving to the right. It stops when I release the mouse button, but I cant get it back anyway. I just need something like "if location.y changed -> keep the location.x the same (so dont apply the strip_click) – Marek Buchtela Jan 03 '13 at 15:29

1 Answers1

2

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;
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Strange, now I cant move the panel at all. Maybe its because of this: as there are more panels with this thing applied, I am saving the "dragged" as invisible label in each of them, set to string "0", changing to "1" when in the "if" in MouseMove, and back to "0" in MouseDown. When i click the panel, it still moves left or right just fine, but I cant move it vertically now. – Marek Buchtela Jan 03 '13 at 15:59
  • Just figured out that what I did is a total non-sense. I'm sorry. Now applied your code with the bool variable, but I get the same result - after I hold the mouse button and move the mouse, the strip doesn't move. **BUT**, when I move the mouse at least it doesnt move horizontally :) – Marek Buchtela Jan 03 '13 at 16:16
  • 1
    These are just inserted statements, they don't prevent the assignment to the Location property in the MouseMove event. Which is what makes the panel move. I can't guess how you got it wrong. – Hans Passant Jan 03 '13 at 16:20