Print Preview

Post your Gambas programming questions here.
Post Reply
User avatar
cogier
Site Admin
Posts: 648
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Print Preview

Post by cogier » Friday 29th January 2021 4:53pm

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

User avatar
PJBlack
Posts: 110
Joined: Tuesday 09th June 2020 10:26pm
Location: Florstadt, Hessen, Germany

Re: Print Preview

Post by PJBlack » Friday 29th January 2021 5:01pm

i'm with today's 3.15.90 / gb.gui.qt and your code crashes gambas completely ...

User avatar
sjsepan
Posts: 62
Joined: Saturday 12th October 2019 10:11pm
Location: Leeper, PA, USA
Contact:

Re: Print Preview

Post by sjsepan » Friday 29th January 2021 6:37pm

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."

cage
Posts: 95
Joined: Monday 02nd September 2019 5:47am
Location: Phoenix Arizona

Re: Print Preview

Post by cage » Friday 29th January 2021 7:32pm

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

User avatar
cogier
Site Admin
Posts: 648
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: Print Preview

Post by cogier » Saturday 30th January 2021 12:32pm

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

User avatar
cogier
Site Admin
Posts: 648
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: Print Preview

Post by cogier » Saturday 30th January 2021 4:27pm

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. :?

Post Reply