How To… Highlight Clicked Detail Bands

Question

“How can I highlight every clicked detail band in the preview?”

Solution

This example shows how to change the color of a shape inside the detail band if the shape is clicked. Begin by assigning the detail band count to the Tag property of each shape drawcommand inside the OnDrawCommandCreate event. Then using a list to keep track of the highlighted detail bands, add or remove enteries each time a shape (detail band) is clicked. Finally regnerate the page updating the color of the shape based on the list values in the DetailBand.BeforePrint event.

Download: ClickHighlightDetails.zip

Sample Delphi code:

procedure TForm1.ppShape1DrawCommandCreate(Sender, aDrawCommand: TObject);
begin
  //Assign detail count to the drawcommand.tag for later use
  TppDrawCommand(aDrawCommand).Tag := ppReport1.DetailBand.Count - 1;

end;
procedure TForm1.ppShape1DrawCommandClick(Sender, aDrawCommand: TObject);
var
  liBandNo: Integer;
begin

  if aDrawCommand is TppDrawShape then
    begin
      liBandNo := TppDrawShape(aDrawCommand).Tag;

      //Add or remove the detail number from the list
      if FHighlightList.IndexOf(IntToStr(liBandNo)) > -1 then
        FHighlightList.Delete(FHighlightList.IndexOf(IntToStr(liBandNo)))
      else
        FHighlightList.Add(IntToStr(TppDrawShape(aDrawCommand).Tag));

      ppReport1.Reset;
      ppReport1.Engine.Reset;

      TppDrawShape(aDrawCommand).RedrawPage := True;
    end;

end;
procedure TForm1.ppDetailBand1BeforePrint(Sender: TObject);
begin
  //Only highlight the bands in the list
  if FHighlightList.IndexOf(IntToStr(ppReport1.DetailBand.Count)) > -1 then
    ppShape1.Brush.Color := clRed
  else
    ppShape1.Brush.Color := clWhite;

end;