Click here to Skip to main content
15,910,277 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a function that doesn't always work. When the mouse left button is pressed and held and you drag over a graphic it will remove the points stored in a 2D list and redraw the other graphics. It works fine when setting it to the MouseUp Event m, but when invoked in the MouseMove event it will work if I ferociously hover over a graphic it will eventually remove it. Could it be an issue with the loop not fast enough??



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

        if (iamPainting)
        {
            Pen p = new Pen(Color.Black, float.Parse("2"));
            g.DrawLine(p, new Point(initX ?? e.X, initY ?? e.Y), new Point(e.X, e.Y));
            Lines2[theSize].Add(new Point(initX ?? e.X, initY ?? e.Y));
            Lines2[theSize].Add(new Point(e.X, e.Y));
        }
        else if (isErase)
        {
            Console.WriteLine("Erase: " + e.X.ToString() + " " + e.Y.ToString());
            int indexToRemove = 0;
            Boolean timetoBreak = false;
            for (int i = 0; i < Lines2.Count; i++)
            {
                for (int i2 = Lines2[i].Count - 1; i2 > 0; i2--)
                {
                    if (e.X == Lines2[i][i2].X && e.Y == Lines2[i][i2].Y)
                    //if (e.X >= Lines2[i][i2].X - 2 && (e.X + 2) <= Lines2[i][i2].X + 2 && e.Y >= Lines2[i][i2].Y - 2 && (e.Y + 2) <= Lines2[i][i2].Y + 2)
                    {
                        indexToRemove = i;
                        timetoBreak = true;
                        break;
                    }
                    if (timetoBreak)
                        break;
                }
            }
            if (timetoBreak)
            {
                Lines2.RemoveAt(indexToRemove);
                theSize -= 1;
                Canvas_Panel.Refresh();
            }
        }
        initX = e.X;
        initY = e.Y;
    }
}


What I have tried:

I've tried various loops, using different events but nothing really works.
Posted
Updated 4-Jul-17 22:17pm

1 solution

I would say yes, too much is going on in MouseMove. How many points have you stored in your Lines array? 10? 100? >1000?
If you have many points maybe you need some smarter solution which avoids iterating every point (i.e. clustering into a grid).
And maybe you don't have to sample every point in MouseMove. Sample only those points which exceed a given distance to the previous point.
Then you can play with the distance to see how many samples your code can handle.

Hope that helps,
Sven
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900