Page 1 of 1

Print Preview

Posted: Friday 29th January 2021 4:53pm
by cogier
I am trying to access the 'Printer.Preview()'. I have tried all sorts of things but without success. Printer help.

I have looked at examples within Gambas, and it looks simple, but it's not working! Any ideas?

Using gb.gui.qt the following code brings up the form, but it's blank and the program crashes.
Using gb.gui the form is not seen and the program crashes.
''Requires gb.form.print

Printer1 As Printer

Public Sub Form_Open()

  Printer1 = New Printer As "Printer1"
  Printer1.Preview()

End

Re: Print Preview

Posted: Friday 29th January 2021 5:01pm
by PJBlack
i'm with today's 3.15.90 / gb.gui.qt and your code crashes gambas completely ...

Re: Print Preview

Posted: Friday 29th January 2021 6:37pm
by sjsepan
I was able to hang up the running app (but not Gambas itself -- 3.15.2) when calling Preview(), so that I had to click the Stop button. If I instead called the Configure() method, there was a Preview button, but showing the preview that way did not hang the app, as long as I did not call the Preview() in code. In either case, the Preview window showed up, and was blank.


Maybe it (Preview method) is hanging up expecting something to be defined that wasn't yet.

http://gambaswiki.org/wiki/howto/print?nl

"You have to implement the draw event. All other events are optional."

Re: Print Preview

Posted: Friday 29th January 2021 7:32pm
by cage
Charlie here is an example that I use in my File Cabinet program that does work.....
' gambas class file

'by postapase

Public Sub btnClear_Click()
TextArea1.Clear
End

Public Sub btnToPrint_Click()
Printer1.Print()
End

Public Sub Printer1_Draw()
Dim PRINT_MARGIN As Float = Paint.Width / Printer1.PaperWidth * 10
Dim Data As String[]
Dim DataAlinePrinter, Xlinea As String

Data = Split(TextArea1.Text, gb.NewLine) 'separating each line by the TextArea line break

For Each Xlinea In Data
DataAimprimir &= Trim$(Xlinea) & gb.CrLf 'added line break understandable by the printer
Next

Paint.DrawRichText(DataAlinePrinter, PRINT_MARGIN, PRINT_MARGIN,,, Align.TopNormal)

End

Public Sub btnExit_Click()
Me.Close
End

Public Sub Form_Open()
Me.Center
End
You can look at how I did it in the File Cabinet program in Project Showcase

Re: Print Preview

Posted: Saturday 30th January 2021 12:32pm
by cogier
PJBlack
I'm with today's 3.15.90 / gb.gui.qt and your code crashes gambas completely ...
Thanks for trying.

sjsepan
Maybe it (Preview method) is hanging up expecting something to be defined that wasn't yet.
Thanks, but I can't work out what.

cage
Charlie here is an example that I use in my File Cabinet program that does work.....
Thanks. I have managed to get the print routine working, I just wanted to try the preview.

The program prints on 1" (25.4mm) x 2" (50.8mm) labels. The main problem is that after 1 print of say 5 labels, the next print only prints 1, even though the 'No of copies' is set to 5.

If you want to see how the 'Preview' works just press [Ctrl] + P in the IDE.

The actual code I am using is: -
Public Sub PrintButtons_Click()

  iPrint = Last.Tag
  Printer1.PaperHeight = 25.4
  Printer1.PaperWidth = 50.8
  Printer1.MarginBottom = 0
  Printer1.MarginTop = 0
  Printer1.MarginLeft = 0
  Printer1.MarginRight = 0
  Printer1.Orientation = Printer.Portrait
  If Printer1.Configure() Then Return
  Printer1.Print

End

Public Sub Printer1_Draw()

  Dim hCode As Label[] = [LabelCodeSO, LabelCode]
  Dim hLabels As Label[] = [LabelNoSO, LabelTitle]
  Dim hBrush As PaintBrush

  Dim sFont1Size As String = Settings["barcode/size1", "40"]
  Dim sFont2Size As String = Settings["barcode/size2", "16"]
  Dim sFont1 As String = "code128," & sFont1Size
  Dim sFont2 As String = "freeserif,Bold," & sFont2Size

  Dim iY1 As Integer = Settings["barcode/y1", 0]
  Dim iY2 As Integer = Settings["barcode/y2", 25]

  Paint.Begin(Printer1)
  hBrush = Paint.Color(Color.Black)
  Paint.Font = Font[sFont1]
  Paint.DrawText(hCode[iPrint].Text, 0, iY1, 105, 20, Align.Center)
  Paint.Font = Font[sFont2]
  Paint.DrawText(hLabels[iPrint].Text, 0, iY2, 105, 20, Align.Center)
  Paint.Stroke
  Paint.End

End
This is the output
Image

Re: Print Preview

Posted: Saturday 30th January 2021 4:27pm
by cogier
Solution found :D :D :D

For those who are interested try this code: -
''Requires gb.form.print

Printer1 As Printer

Public Sub Form_Open()

  Printer1 = New Printer As "Printer1"
  Printer1.Preview()

End

Public Sub Printer1_Begin()

  Printer1.Count = 1

End
It's very strange that this works as the 'Printer.Count' is 1 when it goes into the '_Begin' routine but without it it fails. :?

Re: Print Preview

Posted: Saturday 3rd April 2021 1:57am
by echo8hink
I use the following snippet to activate print preview of a data report from a form button click:

Code: Select all

'Call a report preview from a form button
'snippet code only

Static Public $hReport As Report

Public Sub Button3_Click()
  'print recipe preview from button click on form

  'renew the report for each run
  'report object name is "rptRecipe"
  'Gambas IDE changes first letter of report name to upper case
  
  $hReport = New RptRecipe
  $hReport.Preview()
  
End
The report has a .class file that fleshes it out with data from tables and some pretty stuff. If you start doing printing and reports, plan to spend some time in the examples found on the farm, and elsewhere, and experiment a lot.

-Dave