0

I have a form where I have a Picturebox within it, I Utilizes "MouseUp" & "MouseDown" event to draw. Everytime when mouseup event is detected(after mousedown event), a new form will pop-up. The pop-up form is basically for user to select their prefered choice(this part isn't much of a concern so i won't explain in detail). Once the selection is done, a button is clicked. User will be prompt to "double click" on the form to select the position they want to place the "number" which is in numerical order(e.g. if it is the first time the user complete the "3 steps(mousedown, mouseup, doubleclick on the picturebox), it will place a "1" on the position where the user doubleclicked on.

However the MouseEvents don't seem to restrict within the Picturebox boundary.

I've not tried anything, because I do not find anything related to restricting "MouseEvent" within a specific space. Below is the code that I've done up to now. I Apologize for giving a vague background of my code.

  private void Side_pictureBox_MouseDown(object sender, MouseEventArgs e)
    {

        if (Edit_Variables.add_remark_now == false && Side_pictureBox.ClientRectangle.Contains(e.Location))
        {
            lastPoint = e.Location;

            isMouseDown = true;



        }
        else if (Edit_Variables.add_remark_now == true && Side_pictureBox.ClientRectangle.Contains(e.Location))
        {
            if (e.Clicks >= 2)
            {

                doubleclicked = true;
                Edit_Variables.add_remark_now = false;
                lastPoint = e.Location;

                NumberingPosition.Add(lastPoint);//To save the location of each numbering
                this.Invalidate();
            }

        }

    }
 private void Side_pictureBox_MouseMove(object sender, MouseEventArgs e)
    {


            if (isMouseDown == true && Edit_Variables.add_remark_now == false && Side_pictureBox.ClientRectangle.Contains(e.Location))
        {

            if (lastPoint != null)//if our last point is not null, which in this case we have assigned above

            {

                using (Graphics g = Graphics.FromImage(Side_pictureBox.Image))

                {




                    g.DrawLine(new Pen(Color.DarkRed, 2), lastPoint, e.Location);
                    g.SmoothingMode = SmoothingMode.AntiAlias;


                    PrevStore.Add(lastPoint);
                    NowStore.Add(e.Location);



                }

                Side_pictureBox.Invalidate();//refreshes the picturebox

                lastPoint = e.Location;//keep assigning the lastPoint to the current mouse position


            }

        }

    }
 private void Side_pictureBox_Paint(object sender, PaintEventArgs e)
    {
        if (doubleclicked == true && Side_pictureBox.Enabled == true )
        {

            Bitmap bm = new Bitmap(Side_pictureBox.Image);


            using (Graphics gr = Graphics.FromImage(bm))
            {
                gr.SmoothingMode = SmoothingMode.AntiAlias;

                String drawString = numbering_for_digram.ToString();
                Font drawFont = new Font("Calibri (Body)", 15);
                SolidBrush drawBrush = new SolidBrush(Color.Blue);

                gr.DrawString(drawString, drawFont, drawBrush, lastPoint);
                Numbering.Add(drawString);


                drawFont.Dispose();
                drawBrush.Dispose();
            }

            Side_pictureBox.Image = bm;
            doubleclicked = false;



            Tagged_Remarks_listBox.Items.Add(numbering_for_digram + "." + Edit_Variables.remarks_for_diagram);


            // Tagged_Remarks_richTextBox.Text = Tagged_Remarks_richTextBox.Text + numbering_for_digram + "." + Edit_Variables.remarks_for_diagram + Environment.NewLine;
            numbering_for_digram++;


            // To keep the drawings and remarks in place even after switching views //
            if (Side_pictureBox.ImageLocation == AppDomain.CurrentDomain.BaseDirectory + @"pictures for app\Bus_Nearside.png" || Side_pictureBox.ImageLocation == AppDomain.CurrentDomain.BaseDirectory + @"pictures for app\NearsideSaved.jpg")
            {
                MessageBox.Show("saving nearside image");
                //Side_pictureBox.Image.Save(@"C:\Users\user\Desktop\PDI_APP_EDIT_FOR_TO\pictures for app\Saved_Nearside.jpg");

                Side_pictureBox.Image.Save(AppDomain.CurrentDomain.BaseDirectory + @"\pictures for app\NearsideSaved.jpg");
                Edit_Variables.Bus_Nearside_Edited = true;

                // To save every single input to allow reverting //
                Edit_Variables.Bus_Nearside_Changes_Count++;
                Side_pictureBox.Image.Save(AppDomain.CurrentDomain.BaseDirectory + @"\Nearside drawing memory\Nearsidememory" + "_" + Edit_Variables.Bus_Nearside_Changes_Count + ".jpg");
            }

            else if (Side_pictureBox.ImageLocation == AppDomain.CurrentDomain.BaseDirectory + @"pictures for app\Bus_Offside.png" || Side_pictureBox.ImageLocation == AppDomain.CurrentDomain.BaseDirectory + @"pictures for app\OffsideSaved.jpg")
            {
                MessageBox.Show("saving offside image");
                Side_pictureBox.Image.Save(AppDomain.CurrentDomain.BaseDirectory + @"\pictures for app\OffsideSaved.jpg");
                Edit_Variables.Bus_Offside_Edited = true;

 private void Side_pictureBox_MouseUp(object sender, MouseEventArgs e)
    {
        if (Edit_Variables.add_remark_now == false && Side_pictureBox.ClientRectangle.Contains(e.Location))
        {

            isMouseDown = true;

            //lastPoint = Point.Empty;
            lastPoint = e.Location;


            Diagram_RemarkInput_Form myform = new Diagram_RemarkInput_Form();
            myform.ShowDialog(this);

            No_of_circle++;

            MessageBox.Show("Please doubleclick on the position where you want to place remark");

        }
        else if (Edit_Variables.add_remark_now == true && Side_pictureBox.ClientRectangle.Contains(e.Location))
        {
            isMouseDown = false; //do not touch//

            // To store the number of points for each draw event, also to differentiate between different drawing //
            NumberOfPoints.Add(PrevStore.Count - PointsCount); // To keep the number of points of each drawing

            StartDrawCount.Add(PointsCount);// define when to start drawing 

            PointsCount = PrevStore.Count;

            StopDrawCount.Add(PrevStore.Count);// define when to stop drawing

            counterAdd++;


        }

    }

How can I make it such that only "MouseEvent" only trigger within a specific area?

  • The mouse is 'captured'. This is by design so slipping off the control won't affect the code.. It should only concern mouseup and mousemove. The coordinates and the sender are still working as if you were inside. So you can easily detect the situation by testing e.g. like so: `if (!Side_pictureBox.ClientRectangle.Contains(e.Location)) return;` – TaW Jan 28 '19 at 07:46
  • @Curious Sorry, I'm doing Winform. Do Winform have html(Not being sarcastic)? – user10748500 Jan 28 '19 at 07:47
  • @TaW Side_pictureBox.ClientRectangle.Contains(e.Location) , does this means "if" within the Side_pictureBox's Dimensions? – user10748500 Jan 28 '19 at 07:54
  • Indeed it does. (Do look up the useful Rectangle (and Point) functions! on [MSDN](https://learn.microsoft.com/en-us/dotnet/api/system.drawing.rectangle?view=netframework-4.7.2)) – TaW Jan 28 '19 at 07:56
  • Please validate that `Side_pictureBox_MouseUp` listener is registered to your `pictureBox` and NOT to the form (or any other general component). – ymz Jan 28 '19 at 07:59
  • @TaW Can i say that if i were to write this "if (Side_pictureBox.ClientRectangle.Contains(e.Location))" followed by the " using (Graphics g = Graphics.FromImage(Side_pictureBox.Image))..." it will only allow MouseEvent only when its within the Picturebox? I've tried this, but it still doesnt work out – user10748500 Jan 28 '19 at 08:10
  • @ymz Sorry i do not get what you mean, can you give me a rough example how would a "registered to the picturebox" look like? – user10748500 Jan 28 '19 at 08:14
  • Not a problem! Follow this link https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/events/how-to-subscribe-to-and-unsubscribe-from-events. In short - the relevant code should be placed at `Designer.cs` file – ymz Jan 28 '19 at 08:33
  • The event(s) will always happen (unless you dig deeper into the sysem than you want or need to.) - But with the given line __at the top of the event(s)__ they will be cut short. - _it still doesnt work out_ Do show the new code in the question! – TaW Jan 28 '19 at 08:34
  • @ymz: I really see no reason to assume that the events are not hooked up correctly. The effect is called 'capture'. It can even be called [directly](https://learn.microsoft.com/en-us/dotnet/api/system.windows.input.mouse.capture?view=netframework-4.7.2) but here it happens 'naturally' when slipping off the control while the button is still down. – TaW Jan 28 '19 at 08:37
  • @TaW I've updated the code. Did I do it correctly? – user10748500 Jan 28 '19 at 08:57
  • Looks ok at 1st glance. But you will have to test if it works as planned. Btw: I prefer to test for the mousebutton like this `if (e.Button.HasFlag(MouseButton.Left))..` instead of a flag, at least in the mouse events.. – TaW Jan 28 '19 at 09:48
  • I've tested with my code already, sadly it doesnt work – user10748500 Jan 28 '19 at 12:40
  • Not seing the current code nor knowig in what way it 'doesn't work' or what exactly you want to achieve I guess we can't help you further.. – TaW Jan 28 '19 at 22:13
  • @TaW Sorry for not giving a more concise explaination, I've tried to describle more on my code. But what i want to achieve still remains the same, i do not want any drawing or any mouseevent linked codes to run when I am not clicking on the PictureBox's picture. – user10748500 Jan 29 '19 at 12:32
  • If the MouseClick/MouseDown are hooked up correctly it __will only__ happen when you click on the PBox. But MouseMove and MouseUp may happen if you leave the PBox while the button is still clicked.You can prevent any code running in the events but you can't prevent them from being triggered. – TaW Jan 29 '19 at 12:41
  • @TaW okay i roughly get what you meant. Now i'm focusing on the "Drawline". I've set a "if" condition, which is in my "paint event". This should only take place if my mousedown event is happening and it is occuring within the picturebox dimension(Side_pictureBox.ClientRectangle.Contains(e.Location)), then it will set a boolean variable(isMouseDown) as true. The "Drawline" within "paint event" will only take place if this boolean "isMouseDown" is true. – user10748500 Jan 29 '19 at 13:02
  • @TaW However, it turn out even outside of the picturebox, mousedown event still triggered, even if i set the "if" ...Side_pictureBox.ClientRectangle.Contains(e.Location)" condition . – user10748500 Jan 29 '19 at 13:02
  • If MouseDown id triggered from outside the pbox then it is not hooked up to the pbox but to the parent/form.. – TaW Jan 29 '19 at 21:23

0 Answers0