I need to write a program which can draw rectangle by mouse. User can draw a rectangle by click left mouse button and drag mouse, when user release left mouse button, rectangle will be drawn.
Now I can draw rectangle, but I want to check when I move mouse, program can show me the mouse is inside a rectangle or not.
I use an RectangleArray which inherits from CollectionBase to store all drawn rectangle. In mose move method, I loop through this array and use Contains method of Rectangle class to check the mouse inside or not. But I don't understand while I only can check the position of mouse if it is inside the first rectangle I drawn (label4 does not change value). But if I show a message box, label4 can change its value.
private void picImage_MouseMove(object sender, MouseEventArgs e)
{
Point mousePoint = new Point(e.X, e.Y);
foreach (Rectangle rect in rects)
{
if (rect .Contains(mousePoint))
{
label4.Text = "In rect";
//MessageBox.Show(mouseIsInShelf.ToString());
break;
}
else
{
label4.Text = "Not in rect";
}
}
}
Can anybody help me? Thanks a lot!