Recent

Author Topic: Setting PaperName Causes Error - How To handle?  (Read 2476 times)

Dan3468298

  • Full Member
  • ***
  • Posts: 131
Setting PaperName Causes Error - How To handle?
« on: July 24, 2020, 03:17:54 am »
I have two Brother printers and Dymo LabelWriter 450.   The Brother printers support DefaultPaperName but the Dymo does not.   I used Try to catch the error of the Dymo not having a US Letter paper size but this doesn't work.   This error happens when changing the printer from one of the Brothers to the Demo and Brother used US Letter paper size.   How can I handle this error?   

Code: Pascal  [Select][+][-]
  1. procedure TForm1.ListBox2DblClick(Sender: TObject);
  2. begin
  3.  
  4.   Printers.Printer.SetPrinter(ListBox2.Items[ListBox2.ItemIndex]);
  5.  
  6.   try
  7.     Printer.PaperSize.PaperName := printer.PaperSize.DefaultPaperName;
  8.   except
  9.     showmessage('Yes');
  10.     Printer.PaperSize.PaperName := 'Shipping';
  11.   end;
  12.  
  13.   ShowMessage(Printer.PaperSize.PaperName);
  14.  
  15.   Panel1.Invalidate;
  16.  
  17. end;  
« Last Edit: July 24, 2020, 03:25:21 am by Dan3468298 »
MacOS 10.15.5/Lazarus 2.0.10 Build 2020-07-07

jamie

  • Hero Member
  • *****
  • Posts: 7891
Re: Setting PaperName Causes Error - How To handle?
« Reply #1 on: July 24, 2020, 03:49:59 am »
my guess is and it is most likely that is you didn't call the EndDOC method to complete the current selected printer operation so its still there.

 The other option is to first SetPrinter('') nothing, change the values and then SetPrinter to what you want.

The only true wisdom is knowing you know nothing

Dan3468298

  • Full Member
  • ***
  • Posts: 131
Re: Setting PaperName Causes Error - How To handle?
« Reply #2 on: July 24, 2020, 04:10:18 am »
I never executed BeginDoc.  Just inspecting and setting properties.

MacOS 10.15.5/Lazarus 2.0.10 Build 2020-07-07

Dan3468298

  • Full Member
  • ***
  • Posts: 131
Re: Setting PaperName Causes Error - How To handle?
« Reply #3 on: July 24, 2020, 02:18:27 pm »
SetPrinter('') did not help.  Current code and errors attached.   Appreciate the help!

Code: Pascal  [Select][+][-]
  1. procedure TForm1.ListBox2DblClick(Sender: TObject);
  2. begin
  3.   if ListBox2.ItemIndex <> -1 Then
  4.     try
  5.       Printers.Printer.SetPrinter(ListBox2.Items[ListBox2.ItemIndex]);
  6.       Printer.PaperSize.PaperName := printer.PaperSize.DefaultPaperName;
  7.     except
  8.       on E: Exception do
  9.         ShowMessage( 'Error: '+ E.ClassName + #13#10 + E.Message );
  10.     end;
  11.  
  12.   Panel1.Invalidate;
  13.  
  14. end;  
« Last Edit: July 24, 2020, 02:38:29 pm by Dan3468298 »
MacOS 10.15.5/Lazarus 2.0.10 Build 2020-07-07

jamie

  • Hero Member
  • *****
  • Posts: 7891
Re: Setting PaperName Causes Error - How To handle?
« Reply #4 on: July 24, 2020, 04:34:54 pm »
Ok so instead changing the name. First change the paper size and the the printer name.

Also maybe u want a try and finally instead so u can complete the operation.
Run it outside of debug mode.
The only true wisdom is knowing you know nothing

Dan3468298

  • Full Member
  • ***
  • Posts: 131
Re: Setting PaperName Causes Error - How To handle?
« Reply #5 on: July 24, 2020, 05:13:29 pm »
OK, changed the order of the property changes and changed try from except to finally.  Same US Letter not supported error but now worse since code gets stuck there without changing the printer.

 Code:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.ListBox2DblClick(Sender: TObject);
  2. begin
  3.  
  4.   if ListBox2.ItemIndex <> -1 Then
  5.     try
  6.       Printer.PaperSize.PaperName := printer.PaperSize.DefaultPaperName;
  7.       Printers.Printer.SetPrinter(ListBox2.Items[ListBox2.ItemIndex]);
  8.     //except
  9.     //  on E: Exception do
  10.     //    ShowMessage( 'Error: '+ E.ClassName + #13#10 + E.Message );
  11.     finally
  12.       //place holder
  13.     end;
  14.  
  15.   Panel1.Invalidate;
  16.  
  17. end;    
« Last Edit: July 24, 2020, 05:20:13 pm by Dan3468298 »
MacOS 10.15.5/Lazarus 2.0.10 Build 2020-07-07

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Setting PaperName Causes Error - How To handle?
« Reply #6 on: July 24, 2020, 05:25:59 pm »
Hi!

Before you set a paper size ,you should ask your printer which formats are supported:

procedure TPrinter.DoEnumPapers(Lst: TStrings);

Now you can select one format from the list Lst.

Winni

Dan3468298

  • Full Member
  • ***
  • Posts: 131
Re: Setting PaperName Causes Error - How To handle?
« Reply #7 on: July 24, 2020, 06:35:03 pm »
Hi!

Before you set a paper size ,you should ask your printer which formats are supported:

procedure TPrinter.DoEnumPapers(Lst: TStrings);

Now you can select one format from the list Lst.

Winni

Being a newbie I am not sure how to make the call to DoEnumPapers.  This is an unpublished function?  Please give me an example.  Thx!
MacOS 10.15.5/Lazarus 2.0.10 Build 2020-07-07

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Setting PaperName Causes Error - How To handle?
« Reply #8 on: July 24, 2020, 07:19:19 pm »
Try this.
Start a new Lazarus GUI application.
In the Project Inspector add a new requirement for the printer4lazarus package.
Drop a listbox on the form.
Double-click the form to generate an OnCreate handler. Complete it as follows:
Code: Pascal  [Select][+][-]
  1. uses
  2.    ... PrintersDlgs, ...;
  3. procedure TForm1.FormCreate(Sender: TObject);
  4. var
  5.   dlg: TPrintDialog;
  6. begin
  7.   dlg := TPrintDialog.Create(Nil);
  8.   try
  9.     if dlg.Execute then
  10.       ListBox1.Items.Assign(Printer.PaperSize.SupportedPapers);
  11.   finally
  12.     dlg.Free;
  13.   end;
  14. end;
If you click the Print button in the dialog that shows when the app starts, it will populate the listbox with whatever paper sizes your default printer supports.

Dan3468298

  • Full Member
  • ***
  • Posts: 131
Re: Setting PaperName Causes Error - How To handle?
« Reply #9 on: July 24, 2020, 11:40:32 pm »
Thanks for the useful replies! 
MacOS 10.15.5/Lazarus 2.0.10 Build 2020-07-07

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2770
    • havefunsoft.com
Re: Setting PaperName Causes Error - How To handle?
« Reply #10 on: July 25, 2020, 05:55:52 am »
This is might widgetset specific implementation problem.
You might want to try to replicate the same problem under Win32. (Just make sure you don't use the workaround code).

If you cannot replicate the problem under Win32. (i.e. switching the printer does not retain the paper size)

Then you might want to file a bug report regarding Cocoa implementation of printers.

Note, that you might be the person that's held responsible for the proper testing of the fix.
The nature of the issue is very hardware specific.
Most of the printers that usual people get hands on do support "Letter" and "A4".

Dan3468298

  • Full Member
  • ***
  • Posts: 131
Re: Setting PaperName Causes Error - How To handle?
« Reply #11 on: July 27, 2020, 07:12:06 pm »
I only have MacOS Catalina now.  I am thinking about using Parallels to install Windows 10 but not rushing on that.  I am trying currently to get Trunk install running.
MacOS 10.15.5/Lazarus 2.0.10 Build 2020-07-07

Dan3468298

  • Full Member
  • ***
  • Posts: 131
Re: Setting PaperName Causes Error - How To handle?
« Reply #12 on: July 30, 2020, 12:36:54 am »
I have Trunk running now so am going to license Parallels and Windows 10.   
MacOS 10.15.5/Lazarus 2.0.10 Build 2020-07-07

 

TinyPortal © 2005-2018