What does the book mean here(OnPaint)

T

Tony Johansson

Hi!

The book says the following."Windows notifies an application that some
repainting needs to be done by raising a Paint event.
Interestingly, the Form class has already implemented a handler for this
event, so you don't need to add one yourself.
The Form1 handler for the Paint event will at some point in its processing
call up a virtual method, OnPaint(), passing to it a single PaintEventArgs
parameter. This means that all you need to do is override Onpaint() to
perform painting.

What I don't understand here is why they say "Paint event will at some point
in its processing call up a virtual method, OnPaint(), passing to it a
single PaintEventArgs parameter" that doesn't match to what I have read.?

I have the following understanding about this. In method OnPaint in the
control class an event Paint is raised which cause that every subscriber
will be informed by calling their event handler method for this Paint event.
If you instead override the controls OnPaint method in the Form class this
method will be called instead.When overriding the OnPaint it's important to
call base.OnPaint() so all subscriber will be notified.


//Tony
 
V

vanderghast

The name of the subroutine is irrelevant, somehow, if you subscribe to the
Paint event:


this.Paint += new
System.Windows.Forms.PaintEventHandler(this.OnPaintOrNotOnPaintThereIsNoMagic);



and for this form, the procedure that will be called is
OnPaintOrNotOnPaintThereIsNoMagic. It would be in that subroutine that you
will paint, dynamically, what you have to. And there is not need to call
other base event, whatever, with this way of doing things.


Sure, you can also go like Charles Petzold does, in some of his examples,
using overriding, but in general, at least, from what I see commonly
everyday, developers seems to prefer to subscribe to the Paint event (as in
the code here up).

To subscribe to the Paint event, you can simply start at the event property
sheet (the Property windows have a lightning icon, which display available
events) and double click the value for the Paint event (of the form). The
+= statement will be generated for you, in the partial class file
formName.Designer.cs, and you will be prompted to a subroutine having
already the right signature for its arguments.



Vanderghast, Access MVP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top