How can i check on pictureBox1 mouse leave event if the user is in mouse down event?
<p>What i mean is that i want to activate the mouse leave event only when the user is moving the mouse pointer out of the pictureBox1 client area. This happens only if he presses down the left mouse button but not if the user doesn't press the left mouse button, i.e he can move the mouse around free and the event wont do anything.</p>

<p>I have a variable in the top of Form1 called mouseLeave type bool.</p>

<p>In constructor i made it to be false;</p>

<p>In the pictureBox1 mouse down event i did the mouseLeave variable to be true.</p>

<pre><code>private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                label1.Visible = true;
                label4.Visible = true;
                // find the index that is closest to the current mouse location
                float t = wireObject1.GetIndexByXY(e.X, e.Y, 5);

                if (t == -1)
                {
                    button3.Enabled = false;
                }
                else
                {
                    button3.Enabled = true;
                    {
                        selectedIndex = t;
                        mouseMove = true;
                        mouseLeave = true;
</code></pre>

<p>In the pictureBox1 mouse move event i check if mouseMove is true then move the point drag it around:</p>

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

                    wireObject1.MovePoint(selectedIndex, NewPoint, NewPoint); // when moving a point dragging the other point is vanished deleted. To check why !

                    label1.Text = "{X = " + NewPoint.X + "}" + " " + "{Y = " + NewPoint.Y + "}";
                    pictureBox1.Refresh();
                }

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

<p>So when the user click on the mouse left button without leave it and drag the point then the point will move aorund the pictureBox1 client area.</p>

<p>Now in the pictureBox1 mouse leave event i did:</p>

<pre><code>private void pictureBox1_MouseLeave(object sender, EventArgs e)
        {
            if (mouseLeave == true)
            {
                mouseMove = false;
            }

        }
</code></pre>

<p>But it dosent work. I add a point drag it move it around but this event activate do something only when i move the mouse pointer out of the pictureBox1 area without dragging the point only when im not clicking down the left mouse button.</p>

<p>What i want is that only when i click down the mouse left button and move it around like in the mouse move event only then this leave event will do something in this case will make mouseMove to be false.</p>

<p>So the user will not be able to drag the point out of the pictureBox1 area. What should i do in the mouse leave event then ?</p>

<p>EDITED**</p>

<p>This the button1 click where i add each time a new point to the pictureBox1 area.
And the paint event where i draw the points.</p>

<p>Maybe this will help to solve the problem where it stop but not in the right places when drag out of bounds of pictureBox.</p>

<pre><code>private void button1_Click(object sender, EventArgs e)
        {
            halfX = pictureBox1.ClientRectangle.Width / 2;
            halfY = pictureBox1.ClientRectangle.Height / 2;
            Random rnd = new Random();
            offsetX = rnd.Next(-10, 10);
            offsetY = rnd.Next(-10, 10);
            wireObject1.addPoint(halfX + offsetX, halfY + offsetY);
            if (wireObjectCoordinates1 == null)
                wireObjectCoordinates1 = new WireObjectCoordinates() { FrameNumber = currentFrameIndex };
            wireObjectCoordinates1.Point_X.Add(halfX + offsetX);
            wireObjectCoordinates1.Point_Y.Add(halfY + offsetY);
            wireObjectAnimation1._coordinatesList.Add(wireObjectCoordinates1);
            pictureBox1.Refresh();
            numberOfPoints++;
            label5.Text = "{X = " + (halfX + offsetX) + "}" + " " + "{Y = " + (halfY + offsetY) + "}";
            label5.Visible = true;
            label7.Visible = true;
            label16.Text = numberOfPoints.ToString();
            label16.Visible = true;
            label15.Visible = true;
            buttonLockMode = true;
            button8.Enabled = true;
            button4.Enabled = true;


        }



private void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
        {
           Point connectionPointStart;
           Point connectionPointEnd;
           Graphics g = e.Graphics;
           g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
           SolidBrush brush = new SolidBrush(Color.Red);
           Pen 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);

               }



        }
</code></pre>

<p>I think i solved it by changing the if in the mouse move to this:</p>

<pre><code>if (!((PictureBox)sender).ClientRectangle.Contains(NewPoint.X + 5,NewPoint.Y) || (!((PictureBox)sender).ClientRectangle.Contains(NewPoint.X - 5,NewPoint.Y) ||
                     !((PictureBox)sender).ClientRectangle.Contains(NewPoint.X, NewPoint.Y + 5)) || (!((PictureBox)sender).ClientRectangle.Contains(NewPoint.X, NewPoint.Y - 5)))
</code></pre>

<p>So the move event should look like this:</p>

<pre><code> private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (mouseMove == true)
            {
                mouseDrag = true;
                Point NewPoint = e.Location;
                if (!((PictureBox)sender).ClientRectangle.Contains(NewPoint.X + 5,NewPoint.Y) || (!((PictureBox)sender).ClientRectangle.Contains(NewPoint.X - 5,NewPoint.Y) ||
                     !((PictureBox)sender).ClientRectangle.Contains(NewPoint.X, NewPoint.Y + 5)) || (!((PictureBox)sender).ClientRectangle.Contains(NewPoint.X, NewPoint.Y - 5)))
                {
                    if (mouseDrag)
                    {
                        mouseMove = false;
                        return;
                    }
                }

                {
                    wireObject1.MovePoint(selectedIndex, NewPoint, NewPoint); // when moving a point dragging the other point is vanished deleted. To check why !

                    label1.Text = "{X = " + NewPoint.X + "}" + " " + "{Y = " + NewPoint.Y + "}";
                    pictureBox1.Refresh();
                }

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