Using .Invalidate to repaint the screen

J

jerry chapman

Based on a mouse click I change something on the screen. I then want to
repaint the screen to show the change. When I tried to use .Invalidate in my
mouse routine, I got a compilation error.

Here is my OnPaint code:
protected override void OnPaint(
PaintEventArgs paintEvent )
{
Graphics g = paintEvent.Graphics;

Where can I put the .Invalidate code, and what should the part before the
"." be?
 
M

Morten Wennevik

Hi Jerry,

Call Invalidate when you need to repaint a Control or a Form, in your case probably in relation to the mouse click event. For a Form, you can simply call

Invalidate(); //or this.Invalidate();

If you need a Panel to repain itself you would typically do

panel1.Invalidate();
 
T

Tim Wilson

I assume that you're looking to invalidate your form, or custom control,
when the user performs a click within the client area? If so then you can do
whatever needs to be done to indicate what should be painted from within the
OnClick override and then call "this.Invalidate()".
 
G

Guest

Invalidate() can only be called on classes that support it, such as most UI
elements. If you wish to repaint the entire window and the Invalidate() is
being called from within the same class, you can simply do a
this.Invalidate(), otherwise 'this' would be replaced with the reference name
of the object you want to have redrawn.

Give that a try, if that doesn’t work, what exactly is the compiler error
you are getting?

Brendan
 

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

Similar Threads


Top