How To…Fille Line With Dots

Question

“How can I format each line with something like ‘Value…..’ such that ‘.’ characters are used to fill the remaining space?”

Solution

This example shows how to Use a Variable and its OnCalc event to append the appropriate number of ‘.’to each line.

Download: FillLineWithDots.zip

Sample Delphi code:

uses
  ppTypes,
  ppUtils;

procedure TForm1.ppVariable1Calc(Sender: TObject; var Value: Variant);
var
  ldMaxWidth: Double;
  liDotCount: Integer;
  lCanvas: TCanvas;
  lsValue: String;
begin

  // space available (report.Units are utInches here)
  ldMaxWidth := 2;

  lsValue := ppDBPipeline1['ShipVia'];

  // use printer canvase to perform calculations
  lCanvas := ppReport1.Printer.Canvas;
  lCanvas.Font := ppVariable1.Font;

  // calc number of dots that fit to the right of the text
  liDotCount := CaclulateDots(lsValue, ldMaxWidth, lCanvas, ppReport1.Printer);

  // append dots to the string value
  Value := lsValue + StringOfChar('.', liDotCount);

end;


function TForm1.CaclulateDots(aLeftText: String; aMaxWidth: Double; aCanvas: TCanvas; aPrinter: TppPrinter): Integer;
var
  ldValueWidthInInches: Double;
  ldMaxAvailableDotWidth: Double;
  ldDotWidthInInches: Double;
begin

  // calc text width in inches
  ldValueWidthInInches := aCanvas.TextWidth(aLeftText) / aPrinter.PixelsPerInch.X;

  // calc dot width in inches
  ldDotWidthInInches := aCanvas.TextWidth('.') / aPrinter.PixelsPerInch.X;

  // calc available width
  ldMaxAvailableDotWidth := aMaxWidth - ldValueWidthInInches;

  // calc number of dots 
  Result := Trunc(ldMaxAvailableDotWidth / ldDotWidthInInches);

end;