Recent

Author Topic: Report created only from code - not displaying correctly  (Read 1693 times)

Graeme

  • Hero Member
  • *****
  • Posts: 1430
    • Graeme on the web
Report created only from code - not displaying correctly
« on: November 18, 2015, 05:04:08 pm »
I'm trying to recreate a very simple report I initially created with the report designer. A Report Title Band with one title memo. A master data band with a single memo field.  The report is driven from a User Dataset (a StringList with 10 items). Using the report designer, this was easy to accomplish. Now I'm trying to recreate that report using only code.

Below is my attempt, but it is not working 100%. The report title appears, and the first data item is printed (verified by a writeln() in rptGetValue()).

Issues:
  • The MasterData band prints over the ReportTitle band
  • Why is only the first data item written - what about the other 9 items
  • How do I position the Memo fields. eg: Report Title in the centre of the page, and data items on the left of the page


I searched all over the internet, and haven't found a single example of this "report done in code" for LazReport. I have found one for FastReport v4, but many things have changed between the latest FastReport and LazReport, so that code example didn't work as-is.

I hope somebody can help me out here. Once this is working, I'll submit it to Mantis for inclusion with the rest of the lazreport examples.

Code: Pascal  [Select]
  1. unit mainform;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, LR_Class,
  9.   LR_DSet, StdCtrls;
  10.  
  11. type
  12.   TForm1 = class(TForm)
  13.     Button1: TButton;
  14.     rpt: TfrReport;
  15.     usrdata: TfrUserDataset;
  16.     Memo1: TMemo;
  17.     procedure Button1Click(Sender: TObject);
  18.     procedure FormCreate(Sender: TObject);
  19.     procedure FormDestroy(Sender: TObject);
  20.     procedure rptGetValue(const ParName: String; var ParValue: Variant);
  21.   private
  22.     sl: TStringList;
  23.     procedure DesignReport;
  24.   public
  25.     { public declarations }
  26.   end;
  27.  
  28. var
  29.   Form1: TForm1;
  30.  
  31. implementation
  32.  
  33.  
  34. uses
  35.   Printers; // for page orientation enums
  36.  
  37. {$R *.lfm}
  38.  
  39. { TForm1 }
  40.  
  41. procedure TForm1.rptGetValue(const ParName: String; var ParValue: Variant);
  42. begin
  43.   if ParName = 'element' then
  44.   begin
  45.     writeln('rptGetValue'); // only appears once in console output, but we have 10 "data records"??
  46.     ParValue := sl[usrdata.RecNo];
  47.   end;
  48. end;
  49.  
  50. { This report is designed 100% in code - no report designer }
  51. procedure TForm1.DesignReport;
  52. var
  53.   Page: TfrPage;
  54.   TitleBand: TfrBand;
  55.   DataBand: TfrBand;
  56.   Memo: TfrMemoView;
  57. begin
  58.   { clear the report }
  59.   rpt.Clear;
  60.  
  61.   { add dataset to list of datasets accessible in report }
  62. //  rpt.Dataset := usrdata;
  63.  
  64.   { add page }
  65.   Page := rpt.Pages.Add();
  66.   Page.PgSize := 9; // that should equate to A4 I assume
  67.   { create a unique name }
  68.   Page.CreateUniqueName;
  69.  
  70.  
  71.   { add report title band}
  72.   TitleBand := TfrBand.Create(btReportTitle, Page);
  73.   TitleBand.CreateUniqueName;
  74.   { only "Top" coordinate and height of TitleBand need setting both in pixels }
  75.   TitleBand.Top := 0;
  76.   TitleBand.Height := 20;
  77.  
  78.   { add object to report title band }
  79.   Memo := TfrMemoView.Create(Page);
  80.   Memo.Parent := TitleBand;
  81.   Memo.CreateUniqueName;
  82.   Memo.Memo.Text := 'Hello LazReport!';
  83.   Memo.Height := 20;
  84.   { this object will be stretched to band's width }
  85.   Memo.AutoSize := True;
  86. //  Memo.Align := baWidth;
  87.  
  88.   { add masterdata band }
  89.   DataBand := TfrBand.Create(btMasterData, Page);
  90.   DataBand.CreateUniqueName;
  91.   DataBand.DataSet := usrdata;
  92.   { "Top " should be greater than previously added band's top + height }
  93.   DataBand.Top := 25;
  94.   DataBand.Height := 20;
  95.  
  96.   { add object on masterdata }
  97.   Memo := TfrMemoView.Create(Page);
  98.   Memo.Parent := DataBand;
  99.   Memo.CreateUniqueName;
  100.   { connect to data }
  101. //  Memo.DataSet := usrdata;
  102. //  Memo.DataField := 'CustNo';
  103.   Memo.SetBounds(0, 0, 100, 20);
  104.   Memo.Memo.Text := '[element]';
  105.   Memo.AutoSize := True;
  106.   { align text to object's right margin }
  107. //  Memo.HAlign := haRight;
  108.  
  109.  
  110. end;
  111.  
  112. procedure TForm1.FormCreate(Sender: TObject);
  113. begin
  114.   sl := TStringList.Create;
  115.   sl.Add('1');
  116.   sl.Add('2');
  117.   sl.Add('3');
  118.   sl.Add('4');
  119.   sl.Add('5');
  120.   sl.Add('6');
  121.   sl.Add('7');
  122.   sl.Add('8');
  123.   sl.Add('9');
  124.   sl.Add('10');
  125. end;
  126.  
  127. procedure TForm1.Button1Click(Sender: TObject);
  128. begin
  129.   usrdata.RangeEnd := reCount;
  130.   usrdata.RangeEndCount := sl.Count;
  131.   DesignReport;
  132. //  rpt.LoadFromFile('stringlist.lrf');
  133.   rpt.ShowReport;
  134. end;
  135.  
  136. procedure TForm1.FormDestroy(Sender: TObject);
  137. begin
  138.   sl.Free;
  139. end;
  140.  
  141. end.
  142.  
  143.  
--
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/