Home > Net >  How insert or connect two procedures?
How insert or connect two procedures?

Time:05-08

Hi I have a menu in which I have a print part for the memo and now I don't know how to when I click that part to run this print code I found.
How to link or possibly insert this part of the
PrintTStrings procedure (Lst: TStrings); in the part that is the menu of the procedure
TForm1.MenuItemPrintClick (Sender: TObject); begin end;

procedure TForm1.MenuItemPrinter(Sender: TObject);
begin

end; 

 procedure PrintTStrings(Lst : TStrings) ;
var
  I,
  Line : Integer;
begin
  I := 0;
  Line := 0 ;
  Printer.BeginDoc ;
  for I := 0 to Lst.Count - 1 do begin
    Printer.Canvas.TextOut(0, Line, Lst[I]);

    {Font.Height is calculated as -Font.Size * 72 / Font.PixelsPerInch which returns
     a negative number. So Abs() is applied to the Height to make it a non-negative
     value}
    Line := Line   Abs(Printer.Canvas.Font.Height);
    if (Line >= Printer.PageHeight) then
      Printer.NewPage;
  end;
  Printer.EndDoc;
end;                       

CodePudding user response:

You have to declare the PrintStrings procedure before you can use it. The proper way would be to make PrintTStrings a method of your form by declaring it in the private section of the form declaration:

type
  TForm1 = class(TForm)
    // Items you've dropped on the form, and methods assigned to event handlers
  private
    procedure PrintTStrings(Lst: TStrings);
  end;

You can then just call it directly from the menu item's OnClick event:

procedure TForm1.MenuItemPrinter(Sender: TObject);
begin
  PrintTStrings(PrinterMemo.Lines);
end;

If for some reason you can't make it a form method, you can just rearrange your code:

procedure PrintTStrings(Lst : TStrings) ;
var
  I,
  Line : Integer;
begin
  I := 0;
  Line := 0 ;
  Printer.BeginDoc ;
  for I := 0 to Lst.Count - 1 do begin
    Printer.Canvas.TextOut(0, Line, Lst[I]);

    {Font.Height is calculated as -Font.Size * 72 / Font.PixelsPerInch which returns
     a negative number. So Abs() is applied to the Height to make it a non-negative
     value}
    Line := Line   Abs(Printer.Canvas.Font.Height);
    if (Line >= Printer.PageHeight) then
      Printer.NewPage;
  end;
  Printer.EndDoc;
end;  

procedure TForm1.MenuItemPrinter(Sender: TObject);
begin
  PrintTStrings(PrinterMemo.Lines);
end; 
  • Related