user3140961
user3140961

Reputation:

Is there a way to format TNumberBox.text?

I want the displayed text o a TNumberBox to be formatted eg. when value = 0 to show 'zero', showing thousands separator etc.

Is there a way to do this ?

Upvotes: 0

Views: 702

Answers (1)

Tom Brunberg
Tom Brunberg

Reputation: 21045

You can do it in the OnPaint() event as follows:

procedure TForm14.NumberBox1Paint(Sender: TObject; Canvas: TCanvas;
  const ARect: TRectF);
var
  nb: TNumberBox;
  rf: TRectF;
  tx: string;
begin
  nb := Sender as TNumberBox;

  if nb.Value = 0 then
    tx := 'zero'
  else
    tx := format('%.0n',[nb.Value]);

  rf := ARect;
  Canvas.ClearRect(ARect, TAlphaColors.Lightpink);
  rf.inflate(-4, -2);
  Canvas.Fill.Color := TAlphaColors.Black;

  Canvas.FillText(rf, tx, False, 1, [], TTextAlign.Leading, TTextAlign.Center);
end;

Upvotes: 1

Related Questions