By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,087 Members | 1,508 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,087 IT Pros & Developers. It's quick & easy.

how to repaint a form manually?

P: n/a
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!

Jul 8 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
li********@yandex.ru wrote:
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.

--
Tom Spink
University of Edinburgh
Jul 8 '07 #2

P: n/a
thank you, tom, now it works!

Jul 8 '07 #3

P: n/a
On Sun, 08 Jul 2007 09:23:21 -0700, <li********@yandex.ruwrote:
[...]
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
Jul 8 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.