Search code examples
delphiformatfiremonkey

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 ?


Solution

  • 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;