how to repaint a form manually?

L

linuxadmin

hello!


i want to be able to repaint a class, derived from a form, by myself.
it works automatically, when i do:

protected override void OnPaint(PaintEventArgs e){
base.OnPaint(e);
Graphics g=e.Graphics;
// draw something
}

but how i can call OnPaint by myself?

i tried "OnPaint(new PaintEventArgs(...))", but the PaintEventArgs
ctor needs a 'Graphics' instance as argument, but i have none.
where it can be taken from?

another question:
i have enabled doublebuffering using:

protected override void OnShown(EventArgs e){
this.DoubleBuffered=true;
}

is that ok?


thanks in advice!
 
T

Tom Spink

hello!


i want to be able to repaint a class, derived from a form, by myself.
it works automatically, when i do:

protected override void OnPaint(PaintEventArgs e){
base.OnPaint(e);
Graphics g=e.Graphics;
// draw something
}

but how i can call OnPaint by myself?

i tried "OnPaint(new PaintEventArgs(...))", but the PaintEventArgs
ctor needs a 'Graphics' instance as argument, but i have none.
where it can be taken from?

another question:
i have enabled doublebuffering using:

protected override void OnShown(EventArgs e){
this.DoubleBuffered=true;
}

is that ok?


thanks in advice!

Hi,

You can call the Invalidate() method of the form to force a redraw. Also,
it's wise to enable double buffering as soon as possible - i.e. in the
constructor.
 
P

Peter Duniho

[...]
but how i can call OnPaint by myself?

i tried "OnPaint(new PaintEventArgs(...))", but the PaintEventArgs
ctor needs a 'Graphics' instance as argument, but i have none.
where it can be taken from?

Don't call OnPaint() yourself.

The best mechanism to cause your Control (Form or otherwise) to be
repainted is to call one of the Control.Invalidate() methods. Then you
will get OnPaint() called for you at an appropriate time (a paint event
will be added to your message queue, and will eventually be processed as
other normal events).

If you absolutely must have the form repainted immediately, you can call
Control.Refresh(). This will invalidate the entire Control instance's
client area and force the repaint to occur synchronously. Alternatively,
call an Invalidate() method and then call Update(). This will also force
a synchronous paint, but will only redraw those areas you've specified
through the Invalidate() method call(s).

Synchronous drawing is only required in very specific situations, and is
rarely necessary. You should simply use Control.Invalidate() unless you
have some very specific need that can't be addressed that way.
another question:
i have enabled doublebuffering using:

protected override void OnShown(EventArgs e){
this.DoubleBuffered=true;
}

is that ok?

Seems fine to me. I generally just set that in the constructor, but I
don't see anything wrong with doing it in the OnShown() method (other than
the fact that you usually wouldn't otherwise need to override OnShown()
otherwise, so now you've got an unnecessary override method versus just
setting it in the constructor).

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

Top