Choose Invalidate, Update and Repaint in VCL

Use Invalidate after changing state that affects a control's appearance. It marks the control for painting and allows normal message processing to combine pending paint work. Use Update when an already-invalid region must be painted synchronously. Use Repaint when you deliberately need to invalidate and paint immediately.

These operations do not refresh a database or recompute your application model. They affect drawing. Keep state changes separate from rendering so the control can redraw correctly after exposure, resizing or another window covering it.

Make painting depend on current state

In a Delphi 13 VCL project, add a unit named ValuePanel with this complete custom control. It needs no designer registration when created at runtime.

Pascal
unit ValuePanel;

interface

uses
  System.Classes,
  Vcl.Controls;

type
  TValuePanel = class(TCustomControl)
  private
    FValue: Integer;
    procedure SetValue(AValue: Integer);
  protected
    procedure Paint; override;
  public
    constructor Create(AOwner: TComponent); override;
    property Value: Integer read FValue write SetValue;
  end;

implementation

uses
  System.SysUtils,
  Vcl.Graphics;

constructor TValuePanel.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  Width := 200;
  Height := 60;
  DoubleBuffered := True;
end;

procedure TValuePanel.SetValue(AValue: Integer);
begin
  if FValue = AValue then
    Exit;
  FValue := AValue;
  Invalidate;
end;

procedure TValuePanel.Paint;
begin
  Canvas.Brush.Color := clWindow;
  Canvas.FillRect(ClientRect);
  Canvas.Font.Assign(Font);
  Canvas.Font.Color := clWindowText;
  Canvas.TextOut(ScaleValue(8), ScaleValue(8), IntToStr(FValue));
end;

end.

Add ValuePanel to the form unit and create the control in the form's OnCreate event. The form owns it, so do not also free it manually during normal form destruction.

Pascal
procedure TForm1.FormCreate(Sender: TObject);
var
  Panel: TValuePanel;
begin
  Panel := TValuePanel.Create(Self);
  Panel.Parent := Self;
  Panel.SetBounds(16, 16, 200, 60);
  Panel.Value := 1;
  Panel.Value := 2;
  Panel.Value := 3;
end;

The displayed final value should be 3. The code does not require a visible intermediate frame for 1 or 2. Do not assert an exact number of paint calls, because window exposure and message processing can cause additional paints.

Understand when drawing happens

For windowed controls, Update paints invalid regions without first marking the entire control invalid. Calling it when nothing is invalid may produce no drawing. Repaint requests immediate work, and base VCL Refresh delegates to repainting. Descendants can override behavior, so inspect a specialized control before assuming every similarly named method has identical semantics.

Drawing directly on a control from unrelated code is fragile. That drawing can disappear on the next paint because the control's state does not describe it. Put persistent visual information in state and reproduce it in Paint.

Keep the message loop responsive

Calling Repaint inside a long main-thread loop may update pixels, but it does not turn the operation into responsive background work. Repeated Application.ProcessMessages calls can introduce reentrancy and allow unrelated actions to run while state is partially updated.

Move suitable expensive work away from the UI thread and publish completed state changes back to the main thread through a controlled mechanism. Keep the control and callback target alive until that work has finished or been safely detached.

Test resizing, covering and uncovering the window, theme changes and repeated state updates. Rendering should always reconstruct the latest state. Compiler or arithmetic checks cannot verify flicker, message ordering or visual correctness, so exercise those behaviors in the actual VCL application.

References

TControl.Invalidate

TWinControl.Update

TControl.Repaint

Question provenance

This article addresses What's the difference between Refresh, Update and Repaint? , asked by Ivan Prodanov on Stack Overflow. The source question is licensed under CC BY-SA 3.0 . The explanation and examples here are newly authored for current Delphi. No answer text or code is reproduced.

Author: Herman Schoenfeld