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?