My program draws text on its panel,but if I'd like to remove the text I have to repaint.
How do I call(raise) the paint event by hand?
My program draws text on its panel,but if I'd like to remove the text I have to repaint.
How do I call(raise) the paint event by hand?
I think you can also call
Refresh()
.I found the Invalidate() creating too much of flickering. Here's my situation. A custom control I am developing draws its whole contents via handling the Paint event.
This handler calls a custom routine that does the actual painting.
To simulate scrolling I want to repaint my control every time the cursor moves while the left mouse button is pressed. My first choice was using the Invalidate() like the following.
The control scrolls OK but flickers far beyond any comfortable level. So I decided, instead of repainting the control, to call my custom DrawFrame() method directly after handling the MouseMove event. That produced a smooth scrolling with no flickering.
This approach may not be applicable to all situations, but so far it suits me well.
Refresh would probably also make for much more readable code, depending on context.
The Invalidate() Method will cause a repaint.
MSDN Link
use
Control.InvokePaint
you can also use it for manual double bufferingIn a method of your Form or Control, you have 3 choices:
Normally, you would just call
Invalidate()
and let the system combine that with other Screen updates. If you're in a hurry you should callRefresh()
but then you run the risk that it will be repainted several times consecutively because of other controls (especially the Parent) Invalidating.The normal way Windows (Win32 and WinForms.Net) handles this is to wait for the MessageQueue to run empty and then process all invalidated screen areas. That is efficient because when something changes that usually cascades into other things (controls) changing as well.
The most common scenario for Update() is when you change a property (say, label1.Text, which will invalidate the Label) in a for-loop and that loop is temporarily blocking the Message-Loop. Whenever you use it, you should ask yourself if you shouldn't be using a Thread instead. But the answer is't always Yes.