Recall the PaintEvent method

  • Thread starter Thread starter sravan_reddy001
  • Start date Start date
S

sravan_reddy001

in the following code i am handling the Paint event;
this event is associated with the Form1_Paint() method;

I want to recall this method. i.e the Contents of the form should be
updated every 5 seconds.(Designing the Analog Clock. How to Update
the Time)

this.Paint += new PaintEventHandler(Form1_Paint);

-- Sravan Reddy
 
in the following code i am handling the Paint event;
this event is associated with the Form1_Paint() method;

I want to recall this method. i.e the Contents of the form should be
updated every 5 seconds.(Designing the Analog Clock. How to Update
the Time)

Basically, you have to call Invalidate() on the form instance.

More generally, the usual mechanism is that you have some data with which
you will draw the output in the Paint event. Whenever that data changes,
you call Invalidate(), optionally passing information such as a rectangle
or region indicating just what areas of the form were affected by the
change in the data.

In your case, I suppose that you are not actually storing any data, and
instead are just using the DateTime.Now property, so it would make sense
to just call Invalidate() each time your timer is signaled for a repaint.
Again, optionally restricting the invalidated area to just the part of the
form that actually has changed since the last time you handled the Paint
event.

Pete
 
Can i hav the systax for that. I mean from where to call the
Invalidate() and what are the arguments to it and it will do..

I called the
-------- this.refresh() from the timer1_click event.(i had to
include a timer control to do this). Is there any other metod.
please explain how to use the Invalidate() method.
 
Can i hav the systax for that. I mean from where to call the
Invalidate() and what are the arguments to it and it will do..

I called the
-------- this.refresh() from the timer1_click event.(i had to
include a timer control to do this).

That would work too, but IMHO it is better to use Invalidate(). Calling
Refresh() forces the update to happen immediately, while calling
Invalidate() allows it to be queued and coalesced along with the normal
redrawing. It is (very slightly in most cases) more efficient to use
Invalidate(). So it is preferable for you to get into the habit of using
that instead of Refresh().

That said, I have to admit, since you say you are already using Refresh()
and since that should do basically what you want (the ultimate effect of
both Invalidate() and Refresh() is to cause the form to be invalidated,
and then a Paint event to be raised for the form), I don't really
understand what the purpose of your original question was in the first
place. Maybe I'm not answering the right question.
Is there any other metod.
please explain how to use the Invalidate() method.

You would use Invalidate() just the same way you use Refresh():

this.Invalidate();

Note that technically you don't need the "this." part of the statement
(with Invalidate() or with Refresh()). That's already implied by the
context of the code.

As for other arguments, that just depends on what you want to do. Let's
say your clock is just part of the whole form. Then instead of calling
Invalidate(), you might use the version that takes a Rectangle, where the
Rectangle is set to the portion of the form containing the clock itself.

There are other versions of Invalidate(), such as the one that takes a
Region instance. The general idea is that calling Invalidate() without
any parameters will force the entire Control (your form in this case) to
be redrawn, while passing parameters narrows the area being redrawn to
whatever you specify (which usually will make drawing faster and, if
you're not using double-buffering, less noticeable).

Pete
 

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

Back
Top