How can i prevent in mouse move event when dragging a point in pictureBox to leave the pictureBox borders?
<p>I have pictureBox1 mouse up , down and move events.
I can drag points around the pictureBox1 client area. But if i keep dragging the point to the right,down,left,up i can drag the point out of the pictureBox1 client area behind the Form.</p>

<p>I need that when i drag the point to one of the pictureBox borders the dragging will stop on the point edge with the pictureBox border.</p>

<p>This is the events:</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;
                        OriginalX = wireObject1._point_X[(int)selectedIndex];
                        OriginalY = wireObject1._point_Y[(int)selectedIndex];



                        if (cyclicSelectedIndex.Count() == 2)
                        {
                            cyclicSelectedIndex[currentCyclicIndex] = (int)selectedIndex;
                            currentCyclicIndex++;
                            cyclicSelectedIndex[currentCyclicIndex] = (int)selectedIndex;
                            if (currentCyclicIndex == 1)
                            {
                                currentCyclicIndex = 0;
                            }

                            if ((cyclicSelectedIndex[0] == cyclicSelectedIndex[1]) || (cyclicSelectedIndex[0] == -1) || (cyclicSelectedIndex[1] == -1))
                                // To check why when clicking on connected point with al ine its blinking enable true the button 2 for milisecond !!!!!
                                // Still connecting points dosent work good yet sometimes need to click twice in a row to delete a line and to check all possible ways and bugs.
                            {
                                button2.Enabled = false;
                            }
                            else
                            {
                                button2.Enabled = true;
                            }

                            for (int i = 0; i &lt; wireObject1._connectionstart.Count; i++)
                            {
                                if ((wireObject1._connectionstart[i] == cyclicSelectedIndex[0] &amp;&amp; wireObject1._connectionend[i] == cyclicSelectedIndex[1]) ||
                                   (wireObject1._connectionstart[i] == cyclicSelectedIndex[1] &amp;&amp; wireObject1._connectionend[i] == cyclicSelectedIndex[0]))
                                {
                                    button2.Enabled = false;
                                }
                            }

                            label13.Text = selectedIndex.ToString();
                            label13.Visible = true;
                            label14.Visible = true;

                            listView1.Items.Add(selectedIndex.ToString()).EnsureVisible();
                        }
                    }
                }
            }
        }

        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 + "}";
            }

        }

        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;

                WireObjectCoordinatesCloneFrame();

            }

        }
</code></pre>

<p>This is the button click where io add each time a new point to the 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;


        }   
</code></pre>

<p>And this is the paint event</p>

<pre><code>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 tried many times but it didnt work smooth. 
Cant figure out how to do it ?</p>

<p>EDIT **</p>

<p>This is what i tried before some time ago but it didnt work good enough i did it in the pictureBox mouse move event:</p>

<pre><code>private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (mouseMove == true)
            {
                Point NewPoint = e.Location;
                if(NewPoint.X &gt;= 0 &amp;&amp; NewPoint.X &lt; this.pictureBox1.ClientSize.Width &amp;&amp;
                        NewPoint.Y &gt;= 0 &amp;&amp;NewPoint.Y &lt; this.pictureBox1.ClientSize.Height)
                {
                    Point_X[(int)selectedIndex] = NewPoint.X;
                    Point_Y[(int)selectedIndex] = NewPoint.Y;
                    label1.Text = NewPoint.X.ToString();
                    label2.Text = NewPoint.Y.ToString();
                    pictureBox1.Refresh();
                }
            }

        }
</code></pre>

<p>But the problem i think is:</p>

<p>This is because of the size of the drawn point (the size of the ellipse) in combination with the offset where you clicked onto it. So, i will need to record the offset once when the mouse is down and on mousemove i need to implement this offset into your calculation.</p>

<p>I think this is the problem and i cant figure out how ot do it according to my code and all events.</p>
