Print a listbox

Hi,

I want to print the content of a listbox. The method that I am using is saving all listbox content into a textarea, then I print.

dim i as integer
for i=0 to listBox1.listcount-1
    TextOut.writeline listBox1.cell(i,0)+" "+listBox1.cell(i,2)+chr(9)+chr(9)+listBox1.cell(i,4)
   next

TextOut.Writeline " "
TextOut.Writeline "================================"
TextOut.Writeline " "
TextOut.Writeline " "
TextOut.Writeline Label27.text
TextOut.Writeline Chr(9)+" Total "
TextOut.Writeline Chr(9)+" Pajak 11%"
TextOut.Writeline Chr(9)+" "
TextOut.Writeline Chr(9)+" Service Chg"
TextOut.Close

there are two problem from this code,
the output looks ugly because the total (numbers) alignment is not proper, and using chr(9) is not help much when the item name has different length.

is there any other solution to print the content of listbox.?

thanks
Arief

set the font to a proportionnal font before printing, this will align the rows.
on macos I use monaco or courrier font for that.
you could also write directly to the printer using a graphic object
but then the page setup is more tricky, and more if there are multiples pages.

https://documentation.xojo.com/api/printing/printersetup.html

see also the example in xojo folder
Xojo/Example Projects/Printing and Reporting/Printing/PrintingText.xojo_binary_project

Are they bills ?

Depending on the ListBox contents, I suggest you do the whole work by yopurself to get quality.

But in that case, you have to compute everything:
Creeate your own tab locations / alignment,
Compute the line numbers per page(s).

Also, beware of the page width (do not print text out of the page width.

In short, you print using Graphics each cell individually, using your own \tab value (x depends on the Column value).
For bills, the Alignment must be on the dot (decimal character to get a vertical alignment).
Colum 1 Contents is printed at x = 20
Colum 2 Contents is printed at x = 200
Colum 3 Contents is printed at x = 400
etc. (x values are for the example…)

Also, if you made some later adjustement on the page, remember ot adapt the printing routine (if you add lines in address - top - below total amount - bottom - , if yopu print bills).
Else, one day you will not understand why your print miss some lines or have two (or more) lines printed at the same y value)…
I recently made changes, then I noticed some lines disappeared between pages (wrong # of printed lines per page… my fault).

Hi,

yes this is for the bills.
I think so, printing using graphic should work. But I don’t have any sample to do it.

I have try using Listbox Printer from Mr. Alex Restrepo, Its work fine if its print into normal printer with A4 size, but dont work properly on mini thermal printer 58mm.

I am using Mini Printer for this matter.

Thanks
Arief

I do not know these.

Did you try to print a simple text that resides in a TextArea, just to know how it goes ?

Do you know the printer default DPI ?

Simple example (mostly from the LR):

Var ps As New PrinterSetup
Var g As Graphics
g = ps.ShowPrinterDialog()
If g <> Nil Then
  // Draw text 1 inch across and 1 inch down
  g.DrawText("Hello World", ps.HorizontalResolution, ps.VerticalResolution)
End If

Try that, then, populate a DesktopTextArea with two or three lines of text, then change the g.DrawText line above to:


g.DrawText DT_TextArea.Text, ps.HorizontalResolution, ps.VerticalResolution)

If the result is OK, you can continue now using the DesktopListBox contents.
Else, check the LR for using the printer native resolution (if > 72 dpi).

To Print from DesktopListBox (DTLB), you have to determine the tab values (in pixels), and print - as already said - each Cell individually in a Row Loop:

g.DrawText DTLB.CellTextAt(row, 0),20,20 // Cell 1
g.DrawText DTLB.CellTextAt(row, 1),200,20 // Cell 2
g.DrawText DTLB.CellTextAt(row, 2),300,20 // Cell 3

Of course in your code, you have to replace 20 with y and compute y for each of your lines.
20, 200 and 300, etc. are the x position of each of your tabs; you also have to deal with them.

Is it clear now ?

PS: I hope my code is correct for the current Xojo; but the idea is there.

Of course, you can add images (logo), print using styles (Bold, Italic, …), using colors if available in the printer…

All you have to do is to read the LR, and takes sometimes to implement your print routines.

And… this will be reusable in other projects, as is or so if the same printer, with some changes for other printers.

Remember to add comments for later understanding/changes.

Thanks for the help, yes, its clear.

will try this method.

thanks
regards,
arief