0

I am attempting to drag Music Notes vertically, up and down a Music Staff. However, rather than a constant drag, I would like the music notes to only be allowed to be dragged onto particular intervals (only specific y-coordinates). For example, in a vertical line, a music note can be dragged on to coordinates (0,0), (0,5) or (0,10).

Below is my relevant code:

private Point MouseDownLocation;

private void Note_MouseDown(object sender, MouseEventArgs e)
    {
            foreach (MusicNote mn in panel2.Controls.OfType<MusicNote>())
            {
                if (sender == mn)
                {
                if (e.Button == MouseButtons.Left)
                {
                    MouseDownLocation = e.Location;

                }
            }
            }
    }


 private void Note_MouseMove(object sender, MouseEventArgs e)
    {
        foreach(MusicNote mn in panel2.Controls.OfType<MusicNote>())
        {
            if (sender == mn)
            {
                if (e.Button == MouseButtons.Left)
                {
                    mn.Top = e.Y + mn.Top - MouseDownLocation.Y;
                }
            }
        }
    }

Any help is appreciated. Thank you!

Luke Xuereb
  • 73
  • 12
  • you can use this great [nuget package](https://www.nuget.org/packages/Control.Draggable) that will do the hard work for you – styx Jan 06 '19 at 13:49
  • @styx unfortunately this is for a university project so I will need to code everything. – Luke Xuereb Jan 06 '19 at 14:39

1 Answers1

0

Basically, you need to check if you drag up or drag down You should want to check the MouseDown.X and compare it to the MouseUp.X (or Y if you want to check vertical direction as well). It is important to note that (0, 0) is the upper left of your screen. So you need to compare the X position from mouse down event to the mouse up event.

here's an example with one label that moves up and down in steps of 10

private void label1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {

                if (label1.Location.Y > 0 && label1.Location.Y < panel1.Size.Height) // not the most accurate way, but you get the idea
                {
                    mPointDown = new Point(e.X, e.Y);
                }


            }
        }

        private void label1_MouseUp(object sender, MouseEventArgs e)
        {
            bool movedUp, movedDown;

            if (e.Y == mPointDown.Y)
            {
                movedUp = movedDown = false;
            }
            else
            {
                movedUp = e.Y < mPointDown.Y;
                movedDown = !movedUp;
            }
            if (movedDown)
            {
                label1.Location = new Point(label1.Location.X, label1.Location.Y + 10);
            }
            else if (movedUp)
            {
                label1.Location = new Point(label1.Location.X, label1.Location.Y - 10);
            }
        }

        private void label1_MouseMove(object sender, MouseEventArgs e)
        {
            mouseDownPoint = e.Location;
        }
styx
  • 1,852
  • 1
  • 11
  • 22