Question
“How can I launch the Report wizard directly in code?”
Solution
a. Simplest solution is to use the Designer
1 2 3 4 5 6 7 8 |
uses ppWizard; begin // note: this example works with RB 10 and later myDesigner.Form.LayoutManager.DocumentController.New(ppDefaultReportWizard); end; |
b. It is also possible to launch the report wizard without the Designer
Start by manually creating a TppReportWizard object and implementing the necessary events.
– OnCreateReport: Create a report object and assign a datapipeline.
– OnGetDataNames: Assign the field names from the pipeline to the DataNameList.
– OnGetFields: Add the datapipeline fields to the field list.
– OnValidateName: Set the aNameValid parameter to True.
Next execute the report wizard to show the dialog.
Download: LaunchReportWizardInCode.zip
Sample Delphi code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
uses ppRptWiz; procedure TForm1.btnLaunchWizardClick(Sender: TObject); var lWizard: TppReportWizard; begin lWizard := TppReportWizard.Create(Self); lWizard.OnCreateReport := CreateReportEvent; lWizard.OnGetDataNames := GetDataNamesEvent; lWizard.OnGetFields := GetFieldsEvent; lWizard.OnValidateName := ValidateNameEvent; FReport := nil; if (lWizard.Execute) then FReport.Print; lWizard.Free; FReport.Free; end; procedure TForm1.CreateReportEvent(Sender: TObject; const aDataName: String; var aReport: TObject); begin FReport := TppReport.Create(Self); FReport.DataPipeline := DataPipelineForName(aDataName); aReport := FReport; end; procedure TForm1.GetDataNamesEvent(Sender: TObject; aDataNameList: TStrings); begin aDataNameList.Assign(FDataPipelines); end; procedure TForm1.GetFieldsEvent(Sender: TObject; const aDataName: String; aFieldList: TStrings); var lDataPipeline: TppDataPipeline; liIndex: Integer; begin lDataPipeline := DataPipelineForName(aDataName); if (lDataPipeline <> nil) then for liIndex := 0 to lDataPipeline.FieldCount - 1 do aFieldList.AddObject(lDataPipeline.Fields[liIndex].FieldAlias, lDataPipeline.Fields[liIndex]); end; procedure TForm1.ValidateNameEvent(Sender: TObject; const aName: String; var aNameValid: Boolean); begin aNameValid := True; end; function TForm1.DataPipelineForName(const aDataName: String): TppDataPipeline; var liIndex: Integer; begin liIndex := FDataPipelines.IndexOf(aDataName); if (liIndex <> -1) then Result := TppDataPipeline(FDataPipelines.Objects[liIndex]) else Result := nil; end; procedure TForm1.FormCreate(Sender: TObject); begin FDataPipelines := TStringList.Create; FDataPipelines.AddObject('Customers', plCustomer); FDataPipelines.AddObject('Orders', plOrder); FDataPipelines.AddObject('Sea Fish', plSeaFish); end; |