49
votes

I don't know which of those three methods suits me most. They all work for me. Does anyone know the difference between Refresh, Update and Repaint?

3
To you and the two people who up-voted this so far: I know that opening the Delphi help from the IDE is a bit slow, but it should still be faster than asking on SO and waiting for answers. Answers that can hardly do anything but quoting from the help.mghie
Searching the online documentation, with Google is also another option. docs.codegear.comstukelly
Embarcadero help is notorious for its "high quality packed with informations". I understand ГошУ that he better asked here on SO.truthseeker
@truthseeker Please add an answer then. All the answers are docs lifts. This question has no value in my view.David Heffernan
@David Heffernan Disagree. Google has more intelligent search than most docs (including Embarcadero's). And docs, while obviously a good start, are often limited for lack of detail. SO has the benefit of user discussion. So no, just blandly saying RTFM doesn't necessarily help.Bizmarck

3 Answers

41
votes

According to the online documentation:

Refresh - Repaints the control on the screen.

Call Refresh method to repaint the control immediately. Refresh calls the Repaint method. Use the Refresh and Repaint methods interchangeably.

Repaint - Forces the control to repaint its image on the screen.

Call Repaint to force the control to repaint its image immediately. If the ControlStyle property includes csOpaque, the control paints itself directly. Otherwise, the Repaint method calls the Invalidate method and then the Update method so that any visible portions of controls beneath the control will be repainted as well.

Update - Processes any pending paint messages immediately.

Call Update to force the control to be repainted before any more, possibly time-consuming, processing takes place. Use Update to provide immediate feedback to the user that cannot wait for the Windows paint message to arrive.

Update does not invalidate the control, but simply forces a repaint of any regions that have already been invalidated. Call Repaint instead to invalidate the control as well.

38
votes

Your question is already answered, but if you need good performance and less flicker you should call Invalidate instead. It allows Windows to optimize the painting process.

Invalidate - Completely repaint control.

Use Invalidate when the entire control needs to be repainted. When more than one region within the control needs repainting, Invalidate will cause the entire window to be repainted in a single pass, avoiding flicker caused by redundant repaints. There is no performance penalty for calling Invalidate multiple times before the control is actually repainted.

4
votes

Per an older (CBuilder 6) VCL reference:

Invalidate "Invalidate informs a control that its entire surface needs to be repainted. Calling Invalidate can prevent flicker caused by a series of partial repaints. There is no performance penalty for calling Invalidate multiple times before the control is actually repainted".

The actual repaint does not occur until the control is updated. To force an immediate repaint, call Repaint instead.

Repaint "TWinControl::Repaint calls the Invalidate method and then the Update method to repaint the control" (slight paraphrase).

(Sadly, the older reference is better and more complete than the newer reference I have (CBuilder 2007).