O'Reilly logo

Microsoft® Visual Basic® .NET Programmer's Cookbook by Matthew MacDonald

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Perform Hit Testing with Shapes

Problem

You want to detect if a user clicks inside a shape.

Solution

Test the point where the user clicked with the Rectangle.Contains or GraphicsPath.IsVisible method.

Discussion

If you are creating a program that has custom graphical elements the user can interact with, you need to be able to determine when the user’s mouse is inside or outside a given shape. The .NET Framework provides two methods that can help with this task. The first is the Rectangle.Contains method, which takes a point and returns True if the point is inside the rectangle.

For example, you might add the following code to the drawing program demonstrated in recipe Paint Dynamic Content to check if the point where the user right-clicked lies inside ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required