0

I have a panel with multiple pictureboxes over it. I want to create a rectangle over panel and Picturebox when MouseLeft Button is down and Mouse is dragged. I understand Rectangle Mouse select area can be created with the combination of Mousedown on Panel and MouseMove events on Picturebox and Panel.

Now My Implementation is , When i receive Mousedown event on Panel I save Mousedown Location and look for MouseMove events. if mousemove events is on panel i draw a rectangle over panel and if Mouse Move event is on Picturebox i start drwaing Rectangle for that Picturebox.

Now the Problem is , when mouseleft button is down and mouse move is done over panel i am able to receive mousemove events over it and draw rectangle over panel. However i am not receiving any MouseMove Events for Picturebox to create rectangle select area for it.

Is my Implementation Correct and can someone kindly propose the solution for this ?

I am Posting My Code Snippet for reference.

Code:

//Panel Class

public partial class Childwindow : Form
    {
     //Mouse down event
  private void ClickEventOnchildForm(object sender, MouseEventArgs e)
    {
    if (e.Button == MouseButtons.Left)
        {
            RectStartPoint = e.Location;
            IsSelecting = true;
            Invalidate();
         }
     }

   //Mouse Move event
    private void flowLayoutPanel1_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.Button != MouseButtons.Left)
                return;

            Point tempEndPoint = e.Location;
            Rect.Location = new Point(
                Math.Min(RectStartPoint.X, tempEndPoint.X),
                Math.Min(RectStartPoint.Y, tempEndPoint.Y));
            Rect.Size = new Size(
                Math.Abs(RectStartPoint.X - tempEndPoint.X),
                Math.Abs(RectStartPoint.Y - tempEndPoint.Y));

            this.flowLayoutPanel1.Invalidate();
        }

     //Paint event
   private void flowLayoutPanel1_Paint_1(object sender, PaintEventArgs e)
        {
            if (Rect != null && Rect.Width > 0 && Rect.Height > 0)
            {
                e.Graphics.FillRectangle(selectionBrush, Rect);
            }
        }
}

Public class Picturebox: UserControl{

 private void picbox_MouseMove(object sender, MouseEventArgs e)
        {//Higlight particular Picturebox
        }
}
amithkrp07
  • 81
  • 1
  • 6
  • Only the top-most control will get the mouse events. In cases like this I listen to all relevant events on all of the controls, using the same callbacks for the different controls. – SimpleVar Feb 11 '15 at 07:05
  • Hello Yorye , thanks for your response , I am Listening to MouseMove event for Picturebox too , But i am not receiving them when I have Mousedown for Panel. Is this a Behavior ? – amithkrp07 Feb 11 '15 at 07:15
  • It's possible that the `MouseDown` on the panel is stealing any following `MouseMove` from the picture box. I suggest listening to **both** events on **both** controls. Control events, especially mouse related ones, have a lot of quirks - and it's hard to memorize them all. – SimpleVar Feb 11 '15 at 07:19

0 Answers0