Paint Dynamic Content

Problem

You want to draw a combination of elements on a form and track them so that they can be redrawn later.

Solution

Place all your drawing code in an event handler for the Form.Paint event. When you need to update the form, call the Form.Invalidate method.

Discussion

In many applications, drawing takes place in response to another action, such as a user clicking a button or clicking directly on the form surface. Consider the example shown in Figure 12-8, in which the user can draw a small square object anywhere on a form simply by clicking with the mouse.

Custom drawing.

Figure 12-8. Custom drawing.

In this case, you have two choices:

  • Use drawing ...

Get Microsoft® Visual Basic® .NET Programmer's Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.