Recent

Author Topic: printer error trap  (Read 6479 times)

xenblaise

  • Sr. Member
  • ****
  • Posts: 358
printer error trap
« on: October 14, 2010, 11:11:04 am »
I am using that code to print a memo, however if I have no printer installed, it errors, how can I trap the error? so that, it will not crash the program.

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);
    Line := Line + Abs(Printer.Canvas.Font.Height);
    if (Line >= Printer.PageHeight) then
      Printer.NewPage;
  end;
  Printer.EndDoc;
end;

procedure TForm1.BTNprintClick(Sender: TObject);
begin
if Printer.Printers.Strings[-1]<>'' then
begin

try
PrintTStrings(Memo1.lines);
except
showmessage('NO printer');
end;

end;

end;


using that code because assignprn() is gone.

mas steindorff

  • Hero Member
  • *****
  • Posts: 560
Re: printer error trap
« Reply #1 on: October 14, 2010, 07:43:22 pm »
if you drop a printer dialog on your form and test it's result, you can skip the printout

Code: [Select]
if not printdialog1.execute then
  exit;
there are other dialogs you can chose from if this one does not do the "scan for printers" operation your after.
windows 10 &11, Ubuntu 21+ IDE 3.4 general releases

xenblaise

  • Sr. Member
  • ****
  • Posts: 358
Re: printer error trap
« Reply #2 on: October 14, 2010, 10:03:53 pm »
very good answer,

Ok;

do you have any idea how would i print my dbgrid datas to a printer?


thanks

mas steindorff

  • Hero Member
  • *****
  • Posts: 560
Re: printer error trap
« Reply #3 on: October 14, 2010, 10:21:07 pm »
very good answer,

do you have any idea how would i print my dbgrid datas to a printer?
you should be able to use your existing code.  does it work normally when you have a printer? 
when I convert arrays and grids to strings, I like to use a TStringList.  that way I can convert number at a time.
 loop for each value in the grid...
 sline := sline +format('%3.5f,',[value1]);
 loopend
and then add the line to the tstring list
Tstr.add(sline);
later I can step the TString as a line per index

Code: [Select]
if printDialog1.execute then begin
       PrintTStrings(Memo1.lines);
    end else begin
       showmessage('NO printer selected');
    end;
windows 10 &11, Ubuntu 21+ IDE 3.4 general releases

xenblaise

  • Sr. Member
  • ****
  • Posts: 358
Re: printer error trap
« Reply #4 on: October 15, 2010, 12:07:37 am »
Thanks

I got that.
« Last Edit: October 18, 2010, 08:39:04 am by xenablaise »

 

TinyPortal © 2005-2018