I've got a class called 'MusicNote' which inherits from 'PictureBox'. I'm trying to make it drag vertically when the user clicks on it and moves the mouse up/down. I've pasted the code below, which works. The problem is that it only works the first time - i.e. the user drags it and it's placed in the new position as it's supposed to. However, when I click it again and drag - it disappears. Any ideas?
2 Answers
Problem is with the MouseUp event handler. In that event you will have to unsubscribe to MouseMove event handler.
public void MusicNote_MouseUp(object sender, MouseEventArgs e) { isDragging = false; this.MouseMove -= new MouseEventHandler(OnDrag); } Multiple subscription to same event is causing your controls top value to be negative.
Comments
It seems the currentX and currentY variables are not updated in OnDrag
public void MusicNote_MouseUp(object sender, MouseEventArgs e) { isDragging = false; currentX = e.X; currentY = e.Y; } 2 Comments
Dot NET
I tried changing the code as u suggested, the problem now is that when the PictureBox is moved, it constantly vibrates from side to side. What could the problem be?
Jamie Hutton
updated the answer, so the current values don't case vibrate effect while your moving the item but still updates the values once you have finished moving it.