In a Windows Forms app, any drawing on controls should be done on the
Paint
event, yes. There is the option for drawing on an
Image
object, which can be done any time, and then displaying that
Image
in/on the control. The latter can be done by either assigning that
Image
to an appropriate property or by drawing it onto the control on its
Paint
event. You might choose that option if the drawing is complex and/or it doesn;t change much or at all.
The reason it's done this way is because, when something changes or may have changed in the UI, some or all of a control will be redrawn and repainted on the screen. When that happens, anything that you have drawn previously will be erased and must be redrawn in order to remain visible. The part of the control - note that forms are controls too - that has or may have changed is invalidated and only that part gets repainted, but your drawing code should draw everything required. That means that your drawing code will work no matter what part of the control is being repainted. The execution of the drawing code is generally quite fast and it is the actual painting of pixels to the screen that is slow, so that part should be kept to a minimum, i.e. if you need to invalidate part of a control to be repainted, you shopuld specify the smallest area possible.
You have two choices of where to put your drawing code to be executed on a
Paint
event. If it is a one-off situation then you can create a handler in the relevant form or user control for the Paint event of that control, just as you would do for the Click event of a Button, for example. If you want to do the same thing in various places, either in the same project or in different projects, then you should create a custom control and override the
OnPaint
method, which is where the
Paint
even is raised. Any time you want to customise the benahiour of a derived class on a particular event, you do it in pretty much the same way: you override the method that raises the event and then add your code either before or after the event is raised, e.g.
Protected Overrides Sub OnPaint(e As PaintEventArgs)
'Add code here to draw before any client drawing is perfromed.
MyBase.OnPaint(e)
'Add code here to draw after any client drawing is perfromed.
End Sub
Note that you can still draw in the Paint event handler as well, so it's important to realise that any drawing you do before the event is raised will appear under that and any you do after the event is raised will apear over that.