How To..Clickable DrawCommand with Custom Info

Question

“How can I associate custom info with a hot-clickable component on my report?”

Solution

This example creates a TmyCustomInfo class that stores the CustNo and Country for a customer. The DBText OnDrawCommandCreate event is used to associate an instance of TMyCustInfo with each DrawCommand generated for the page. The DBText OnDrawCommandClick event shows how to access the CustomInfo.

Download: DrawCommandCustInfo.zip

Sample Delphi code:

  {TmyCustInfo
    This class is used to store customer information for each draw command}

  TmyCustInfo = class(TComponent)
    private
      FCustNo: Integer;
      FCountry: String;
    public
      constructor CreateCustInfo(aOwner: TComponent; aCustNo: Integer; aCountry: String); virtual;

      property CustNo: Integer read FCustNo;
      property Country: String read FCountry;
    end;


implementation


constructor TmyCustInfo.CreateCustInfo(aOwner: TComponent; aCustNo: Integer; aCountry: String);
begin
  inherited Create(aOwner);

  FCustNo  := aCustNo;
  FCountry := aCountry;

end;

procedure TForm3.Button1Click(Sender: TObject);
begin
  ppReport1.Print;
end;


procedure TForm3.ppDBText1DrawCommandCreate(Sender, aDrawCommand: TObject);
var
  lCustInfo: TmyCustInfo;
begin
  {create customer info}
  lCustInfo := TmyCustInfo.CreateCustInfo(Self, plCustomer['CustNo'], plCustomer['Country']);

  {attach to draw command}
  TComponent(aDrawCommand).Tag := Integer(lCustInfo);

end;

procedure TForm3.ppDBText1DrawCommandClick(Sender, aDrawCommand: TObject);
var
  lCustInfo: TmyCustInfo;
  lsCustNo: String;
begin
  {get customer info}
  lCustInfo := TmyCustInfo(TComponent(aDrawCommand).Tag);

  lsCustNo := IntToStr(lCustInfo.CustNo);

  ShowMessage('CustNo: ' + lsCustNo + #13#10 +
              'Country: ' + lCustInfo.Country) ;

end;