Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Printing multiple ListBoxes 2

Status
Not open for further replies.

dredfern

Technical User
Dec 7, 2003
14
GB
Part of a program I'm developing works out the tax from an amount input by the user. The results are then placed in 3 ListBoxes, Amount, Tax and Amount+Tax. I would like to print out the results of these ListBoxes on one page in 3 adjacent columns, preferably with their corresponding 3 headings - Amount, Tax And Amount+Tax. Any ideas?
 
There are 101 possibilities. Here is one:

Code:
procedure TForm1.PrintButtClick(Sender: TObject);
const
  {Column left positions in inches}
  c_Col1=0.5;
  c_Col2=2;
  c_Col3=3.5;
var
  LRowHeight:integer;
  LY:integer;  {Current vertical position}
  LPPIX:integer;
  i:integer;
begin
  with Printer do
  begin
    BeginDoc;
    try
      {Initialise Canvas}
      Canvas.Font.Name:='Arial';
      Canvas.Font.Size:=12;
      Canvas.Font.Style:=[fsBold];
      LY:=50;
      LRowHeight:=Abs(Canvas.Font.Height);
      LPPIX:=GetDeviceCaps(Handle,LOGPIXELSX);  
       {Resolution of printer, pixels per inch}


      {Column headings}
      Canvas.TextOut(Trunc(c_Col1*LPPIX),LY,'Amount');
      Canvas.TextOut(Trunc(c_Col2*LPPIX),LY,'Tax');
      Canvas.TextOut(Trunc(c_Col3*LPPIX),LY,'Amount + Tax');
      Inc(LY,LRowHeight*2);
      Canvas.Font.Style:=[];

      {Data}
      for i:=0 to ListBox1.Items.Count-1 do
      begin
        Canvas.TextOut(Trunc(c_Col1*LPPIX),
         LY,AmountList.Items[i]);
        Canvas.TextOut(Trunc(c_Col2*LPPIX),
         LY,TaxList.Items[i]);
        Canvas.TextOut(Trunc(c_Col3*LPPIX),
         LY,AmountTaxList.Items[i]);
        Inc(LY,LRowHeight);
      end;

      EndDoc;
    except
      {Printer.}Abort;
      Raise;
    end;
  end;
end;

Above has no checking for less lines in second two lists than first one.

If you want to right align check out TCanvas.TextWidth and TCanvas.TextRect functions.

Hav fun
Simon
 
Use a TRichEdit, define the page tabs to have 3 columns and then write line by line, using tabs (#9) as separators.

If memory serves, you can right-align the tabs, what is a must for numbers.

Here you have some code to make a glimpse. The code is used to create and format a TRichEdit and dump the content of a TListBox in a formatted fashion. The report is organized in columns as you need.

Sorry I have no time to fully translate the comments (I've added some more in english), but feel free to ask here if you need some more help.

Code:
{----------------------------------------------------------}
procedure TADMFconns.CreateRTF(FileName : AnsiString; Stamp : TDateTime);
var
  Cursor  : TCursor;
  RTF     : TRichEdit;
  i       : integer;
  S       : AnsiString;
  Total   : cardinal;
begin
  Cursor := GetCursor;
  SetCursor(LoadCursor(0, IDC_Wait));
  Total := 0;
  RTF := TRichEdit.Create(Self);
  try
    RTF.Parent := Self;
    RTF.Visible := False;
    // Tabuladores - TAB RULER <--- COLUMNS
    RTF.Paragraph.Tab[0] := 0;                            // Dia
    RTF.Paragraph.Tab[1] := 50;                           // Fecha
    RTF.Paragraph.Tab[2] := RTF.Paragraph.Tab[1] + 60;    // Inicio
    RTF.Paragraph.Tab[3] := RTF.Paragraph.Tab[2] + 60;    // Fin
    RTF.Paragraph.Tab[4] := RTF.Paragraph.Tab[3] + 60;    // Total
    RTF.Paragraph.Tab[5] := RTF.Paragraph.Tab[4] + 60;    // Teléfono
    RTF.Paragraph.Tab[6] := RTF.Paragraph.Tab[5] + 80;    // Título
    //
    RTF.SelAttributes.Name := 'Arial';
    RTF.SelAttributes.Color := $00CE009C;
    RTF.SelAttributes.Size := 14;
    RTF.SelAttributes.Style := [fsBold, fsItalic];
    // Encabezado - HEADER
    // USERNAME IN LAST COLUMN
    S := 'Usuario ' + lbUsers.Items[lbUsers.ItemIndex];
    S := #9 + #9 + #9 + #9 + #9 + #9 + S;
    RTF.Lines.Add(S);
    RTF.Lines.Add('');
    //
    RTF.SelAttributes.Name := 'Courier New';
    RTF.SelAttributes.Color := clBlack;
    RTF.SelAttributes.Size := 9;
    RTF.SelAttributes.Style := [fsBold];
    // COLUMN NAMES
    RTF.SelText := 'Día' + #9 + 'Fecha' + #9 + 
                   'Inicio' + #9 + 'Fin' + #9 + 
                   'Total' + #9 + 'Teléfono' + #13#10;
    RTF.SelAttributes.Style := [];
    // Procese los elementos de la lista - BODY
    for i := 0 to lvScreen.Items.Count - 1 do
      begin
        if i mod 100 = 0
          then Application.ProcessMessages;
        Total := Total + PSession(lvScreen.Items[i].Data)^.sTotal;
        S := lvScreen.Items[i].Caption + #9;                // Día
        S := S + lvScreen.Items[i].SubItems[0] + #9;        // Fecha
        S := S + lvScreen.Items[i].SubItems[1] + #9;        // Inicio
        S := S + lvScreen.Items[i].SubItems[2] + #9;        // Fin
        S := S + lvScreen.Items[i].SubItems[3] + #9;        // Total
        S := S + Trim(lvScreen.Items[i].SubItems[4]) + #13#10;    // Telef
        RTF.SelText := S;
      end;
    // Pie - FOOTER
    RTF.Lines.Add('');
    RTF.Lines.Add('');
    RTF.SelAttributes.Name := 'Arial';
    RTF.SelAttributes.Size := 8;
    RTF.SelAttributes.Style := [];
    S := 'Emitido: ' + FormatDateTime('dd/mm/yy hh:nn:ss', Stamp);
    S := S + #9 + 'Tiempo total: ' + MinsStr(Total) + ':00';
    RTF.Lines.Add(S);
    if FileName <> ''
      then RTF.Lines.SaveToFile(FileName)
      else RTF.Print('S3admin - usuario ' + 
                     lbUsers.Items[lbUsers.ItemIndex]);
  finally
    RTF.Free;
    SetCursor(Cursor);
  end;
end;

HTH.
buho (A).
 
Thanks, Vintage Wine & Buho for your trouble between you you've solved my problem. Thanks again - Dave
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top