Print Preview

Post

Posted
Rating:
#1 (In Topic #566)
Avatar
Guru
cogier is in the usergroup ‘Guru’
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.

Code (gambas)

  1. ''Requires gb.form.print
  2.  
  3. Printer1 As Printer
  4.  
  5. Public Sub Form_Open()
  6.  
  7.   Printer1 = New Printer As "Printer1"
  8.   Printer1.Preview()
  9.  
Online now: No Back to the top

Post

Posted
Rating:
#2
Avatar
Enthusiast
PJBlack is in the usergroup ‘Enthusiast’
 i'm with today's 3.15.90 / gb.gui.qt and your code crashes gambas completely …
Online now: No Back to the top

Post

Posted
Rating:
#3
Avatar
Regular
sjsepan is in the usergroup ‘Regular’
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.

/howto/print - Gambas Documentation

"You have to implement the draw event. All other events are optional."
Online now: No Back to the top

Post

Posted
Rating:
#4
Avatar
Regular
cage is in the usergroup ‘Regular’
Charlie here is an example that I use in my File Cabinet program that does work…..

Code (gambas)

  1. ' gambas class file
  2.  
  3. 'by postapase
  4.  
  5. Public Sub btnClear_Click()
  6. TextArea1.Clear
  7.  
  8. Public Sub btnToPrint_Click()
  9. Printer1.Print()
  10.  
  11. Public Sub Printer1_Draw()
  12. Dim PRINT_MARGIN As Float = Paint.Width / Printer1.PaperWidth * 10
  13. Dim Data As String[]
  14. Dim DataAlinePrinter, Xlinea As String
  15.  
  16. Data = Split(TextArea1.Text, gb.NewLine) 'separating each line by the TextArea line break
  17.  
  18. For Each Xlinea In Data
  19. DataAimprimir &= Trim$(Xlinea) & gb.CrLf 'added line break understandable by the printer
  20.  
  21. Paint.DrawRichText(DataAlinePrinter, PRINT_MARGIN, PRINT_MARGIN,,, Align.TopNormal)
  22.  
  23.  
  24. Public Sub btnExit_Click()
  25.  
  26. Public Sub Form_Open()
  27. Me.Center
  28.  

You can look at how I did it in the File Cabinet program in Project Showcase
Online now: No Back to the top

Post

Posted
Rating:
#5
Avatar
Guru
cogier is in the usergroup ‘Guru’
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: -

Code (gambas)

  1. Public Sub PrintButtons_Click()
  2.  
  3.   iPrint = Last.Tag
  4.   Printer1.PaperHeight = 25.4
  5.   Printer1.PaperWidth = 50.8
  6.   Printer1.MarginBottom = 0
  7.   Printer1.MarginTop = 0
  8.   Printer1.MarginLeft = 0
  9.   Printer1.MarginRight = 0
  10.   Printer1.Orientation = Printer.Portrait
  11.   If Printer1.Configure() Then Return
  12.   Printer1.Print
  13.  
  14.  
  15. Public Sub Printer1_Draw()
  16.  
  17.   Dim hCode As Label[] = [LabelCodeSO, LabelCode]
  18.   Dim hLabels As Label[] = [LabelNoSO, LabelTitle]
  19.   Dim hBrush As PaintBrush
  20.  
  21.   Dim sFont1Size As String = Settings["barcode/size1", "40"]
  22.   Dim sFont2Size As String = Settings["barcode/size2", "16"]
  23.   Dim sFont1 As String = "code128," & sFont1Size
  24.   Dim sFont2 As String = "freeserif,Bold," & sFont2Size
  25.  
  26.   Dim iY1 As Integer = Settings["barcode/y1", 0]
  27.   Dim iY2 As Integer = Settings["barcode/y2", 25]
  28.  
  29.   Paint.Begin(Printer1)
  30.   hBrush = Paint.Color(Color.Black)
  31.   Paint.Font = Font[sFont1]
  32.   Paint.DrawText(hCode[iPrint].Text, 0, iY1, 105, 20, Align.Center)
  33.   Paint.Font = Font[sFont2]
  34.   Paint.DrawText(hLabels[iPrint].Text, 0, iY2, 105, 20, Align.Center)
  35.   Paint.Stroke
  36.   Paint.End
  37.  

This is the output
<IMG src="https://www.cogier.com/gambas/barcode5.png"> </IMG>
Online now: No Back to the top

Post

Posted
Rating:
#6
Avatar
Guru
cogier is in the usergroup ‘Guru’
Solution found :D  :D  :D

For those who are interested try this code: -

Code (gambas)

  1. ''Requires gb.form.print
  2.  
  3. Printer1 As Printer
  4.  
  5. Public Sub Form_Open()
  6.  
  7.   Printer1 = New Printer As "Printer1"
  8.   Printer1.Preview()
  9.  
  10.  
  11. Public Sub Printer1_Begin()
  12.  
  13.   Printer1.Count = 1
  14.  

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. :?
Online now: No Back to the top

Post

Posted
Rating:
#7
Avatar
Trainee
I use the following snippet to activate print preview of a data report from a form button click:

Code

'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
Online now: No Back to the top
1 guest and 0 members have just viewed this.