The timer for limit the paint event to be called dosent work good what could be the reason?
<p>In the pictureBox1 mouse move event i had a pictureBox1.refresh();
But i wanted to limit it so it will not go to the paint event each time i move the mouse but only every 30 miliseconds. </p>

<p>So in logic if i move the mouse 5 seconds the movements the times the paint event was call should be 150 ?</p>

<p>But in fact im getting another number each time.</p>

<p>I wrote the values of the movements to a text file in the first 5 seconds i did 398 moves.
I have two timers one for the moves limition and timer2 for the 5 seconds validation when writing the info to the text file.</p>

<p>This is the mouse move event:</p>

<pre><code>private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (mouseMove == true)
            {
                mouseDrag = true;
                Point NewPoint = e.Location;




                    wireObject1.MovePoint(selectedIndex, NewPoint, NewPoint); 

                    label1.Text = "{X = " + NewPoint.X + "}" + " " + "{Y = " + NewPoint.Y + "}";
                    refreshFlag = true;
                    timer2.Start();
                    if (moveFlag == true)
                    {

                            moveCounter++;
                            textBox1.Text = moveCounter.ToString();
                            if (moveCounter &gt; 10)
                            {
                                w.WriteLine("Number Of Moves ===&gt;   " + moveCounter); 
                            }

                    }


            }
            else
            {
                label19.Text = "{X = " + e.X + "}" + " " + "{Y = " + e.Y + "}";
            }
        }
</code></pre>

<p>In the mouse move event i have two flags.
The refreshFlag in the constructor is false so in the move event its true.
I also start timer2 when i make a move to count 5 seconds.</p>

<p>TIMER1 is enabled true in the designer so when i run the application it start to count 30 milisecond already. Timer2 is enabled false and i activate it in the move event and stop it in the timer2 tick event.</p>

<p>Then in the timer1 and timer2 tick events i did:</p>

<pre><code>private void timer1_Tick(object sender, EventArgs e)
        {
            if (refreshFlag == true)
            {
                pictureBox1.Refresh();
                refreshFlag = false;
            }
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            timer2Counter++;
            textBox2.Text = timer2Counter.ToString();
            if (timer2Counter == 5)
            {
                if (moveCounter &gt; 10)
                {
                    w.WriteLine("Passed 5 Seconds ===&gt;   " + timer2Counter);
                }
                timer2.Stop();
                label6.Text = moveCounter.ToString();
                timer2Counter = 0;
            }
        }
</code></pre>

<p>The second flag in the move event if moveFlag.
The moveFlag is false in the constructor and i set it to true in the paint event in the bottom loop.
The bottom loop is working entering the loop only when i move/drag a point wich is connected with a line to another point.</p>

<pre><code>private void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
        {
            SolidBrush brush;
            Pen p=null;
           Point connectionPointStart;
           Point connectionPointEnd;
           Graphics g = e.Graphics;
           g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

               brush = new SolidBrush(Color.Red);
               p = new Pen(brush);
               for (int idx = 0; idx &lt; wireObject1._point_X.Count; ++idx)
               {
                   Point dPoint = new Point((int)wireObject1._point_X[idx], (int)wireObject1._point_Y[idx]);
                   dPoint.X = dPoint.X - 5; // was - 2
                   dPoint.Y = dPoint.Y - 5; // was - 2
                   Rectangle rect = new Rectangle(dPoint, new Size(10, 10));
                   g.FillEllipse(brush, rect);

                   // g.FillEllipse(brush, rect);
               }

               for (int i = 0; i &lt; wireObject1._connectionstart.Count; i++)
               {

                   int startIndex = wireObject1._connectionstart[i];
                   int endIndex = wireObject1._connectionend[i];



                   connectionPointStart = new Point((int)wireObject1._point_X[startIndex], (int)wireObject1._point_Y[startIndex]);
                   connectionPointEnd = new Point((int)wireObject1._point_X[endIndex], (int)wireObject1._point_Y[endIndex]);
                   p.Width = 4;
                   g.DrawLine(p, connectionPointStart, connectionPointEnd);
                   moveFlag = true;
               }

        }
</code></pre>

<p>Im using the flag in the paint in the bottom inside the second loop since i dont want to limit and count movements just when i move the mouse but only when the point i move/drag is connected with al ine to another point.</p>

<p>In the mouse up event i close the StreamWriter so the file is saved and closed to hard disk:</p>

<pre><code>private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {

            if (mouseMove == true)
            {
                Point NewPoint = e.Location;
                wireObject1._point_X[(int)selectedIndex] = NewPoint.X;
                wireObject1._point_Y[(int)selectedIndex] = NewPoint.Y;
                mouseMove = false;
                if (moveCounter &gt; 10)
                {
                    w.Close();
                }
                WireObjectCoordinatesCloneFrame();

            }

        }
</code></pre>

<p>My problem is why im getting for example 398 movements after 5 seconds ? If i limit the refresh() of the pictureBox1 to 30 miliseconds each time so after 5 seconds it should show me 150 not 398. So whats wrong here ? I want that it will wait 30 miliseconds make the refresh wait again 30 miliseconds and make another refresh. </p>

<p>But it seems that the flag in the move event is raised 398 times in 5 seconds.</p>

<p>So where is the bug in my code ?      </p>
