Printing Listbox From Page x to Page y

This is a follow up of a conversation (I lost it) where the question was asked (How to print from x to y, I think) and I started to answer (some logic : some code) without telling too much and I forgot to mention I understand the question as narrow as to Print the Listbox contents (not all purpose, so no TextEdit).

Now, I have done it. The method below print the (passed) Listbox (contents) even if you want to print from Page x to Page y, without pre-printing useless pages.

The day I started this method (modified an old method), I had the idea, but not the focus, nor the time. Sorry.

BTW: in my own project (I stated that in code, but…), I add two methods:

but I do not add any code in them. A phone call, time to do something else, whatever and when I came back, I forgot to add code in them (and nearly three years goes away 'till now !). :wink:

Feel free to make suggestionsif you had some, report errors if you found some, etc.

At last, ask if something looks strange, dark, anything.

[code]Sub print_Print(LB As ListBox)
// ---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
// This is a Method to print a Listbox contents (nearly a bare bones… [skeleton ?] method)
// Method: print_Print
// Inputs: LB As ListBox
// Outputs: None
// Syntax: print_Print(LB)
// Purpose: Implement a simple Print From Page x to Page y Method (Black and white)
// Original: 2014-11-15; 5:00
// Changes: 2017-07-01; 17:45
// Emile Schwarz
// ---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------

// ---------- ---------- ---------- ----------
// 1. Declare the needed variable
Dim LoopIdx As Integer // Main Loop Counter
Dim Row_Idx As Integer // Number of Rows in the Paper’s printable area ?
Dim Row_Max As Integer // Max number of Rows in a page (Paper’s printable area)
Dim Row_Cnt As Integer // Number of Rows in the ListBox (used in the Main Loop)
Dim Row_Height As Integer // Height of a Row
Dim Row_Pos As Integer // Y position of the line to draw in the printable area
Dim Row_Top As Integer // Default Y position to print the first line of the page
Dim Page_Height As Integer // Height of the Paper’s printable area
Dim Page_Width As Integer // Width of the Paper’s printable area
Dim Page_Left As Integer // Left position of the Paper’s printable area
Dim Page_Top As Integer // Top position of the Paper’s printable area
Dim Font_Name As String // Font Name is the print text Font Name: usually the ListBox Font Name
Dim Font_Size As Single // Font Size is the print text size height: usually the ListBox Font Size
Dim Font_Color As Color // Font Color is the print text color: usually Black
Dim p As New PrinterSetup
Dim g As Graphics
Dim Row_Start As Integer // Row # to start the Print process (First Row to Print…)

// ---------- ---------- ---------- ----------
// 2. Set a Print Setup String and Start the Print process

// Avoir exception error if p is Nil
If p = Nil Then Return

// Use the (eventual) previous Setup String
If gPrint_Settings <> “” Then
// Use the previous Setup String
p.SetupString = gPrint_Settings
End If

// Set the Print orientation
#If Not TargetLinux // Do not ask me (old code)
p.Landscape = True

If p.PageSetupDialog Then
// Store the Setup String
gPrint_Settings = p.SetupString

// Display the Printer Dialog
g = OpenPrinterDialog(p)
If g = Nil Then
  // Error ? Left the Method !
End If

End If

// Avoid error exception(s) if g = Nil [The user pressed the Cancel button…
If g = Nil Then Return

// ---------- ---------- ---------- ----------
// 3. Fill some values
Page_Height = p.Height // Height Printable Area
Page_Width = p.Width // Width Printable Area
Page_Top = 10 // Top of the Printable Area
Page_Left = 10 // Left of the Printable Area

// Font Name, Size and Color (Black)
Font_Name = LB.TextFont
Font_Size = LB.TextSize
Font_Color = &c000000

// Some Row related
Row_Cnt = LB.ListCount // Get the number of Rows in the LisbBox (to print)
Row_Height = LB.RowHeight // Heigh of a Row (Line height)

// Get the number of Rows to print in each page
Row_Max = (Page_Height \ Row_Height) - (gHeader_Height + gFooter_Height) // Compute the number of lines in a page

// The value below may change if a Page Header is drawn: add 15 (or so) pixels in that case
Row_Top = Row_Height + gHeader_Height // Row_Height. Here: Height of the first line

// Only one variable value (Row_Top) to change its value if a Header is printed…
Row_Pos = Row_Top

// Apply the default Text Font, Size and Color to the Graphics Object (I forgot it !)
g.TextFont = Font_Name
g.TextSize = Font_Size
g.ForeColor = Font_Color

// [2017-07-01] Print from Page x to Page y
Dim Print_Page_Start As Integer // First page to print
Dim Print_Page_End As Integer // Last page to print
Dim Print_Page_Number As Integer // Current page number (the “page currently in print”)

// Get the page range to print
Print_Page_Start = g.FirstPage
Print_Page_End = g.LastPage
Print_Page_Number = 1

If Print_Page_Start > 1 And Print_Page_End > 1 Then
// Compute the first Row to print
// Row_Max is the number of Rows for each page
// Print_Page_Start is the first page to Print
Row_Start = Print_Page_Start * Row_Max

// Set the “current page #" value…
Print_Page_Number = Print_Page_Start

Row_Start = 1
End If

// ---------- ---------- ---------- ----------
// 4. Loop thru the whole Listbox
For LoopIdx = Row_Start To Row_Cnt
If Print_Page_Number >= Print_Page_Start Then
// ---------- ---------- ---------- ---------- HEADER
// Print a Header (eventually)
If Row_Idx = 1 Then
// gHeader_Height = Height of the Header
// Set in print_Header(g)
print_Header(g) // Do not do like me: populte the print_Header(g) Method !
End If

  // ---------- ---------- ---------- ---------- CORE PAGE
  // Core of the print process
  // Print the whole Row at X = 0, Y = Row_Pos
  g.DrawString LB.Cell(LoopIdx - 1,-1), 0,Row_Pos // NO TAB USED… DIY !
  // ---------- ---------- ---------- ---------- FOOTER
  If Row_Idx = Row_Max Then
    // Print a Footer (eventually)
    // Set in print_Footer(g)
    print_Footer(g) // Do not do like me: populte the print_Footer(g) Method !
    // Reset some loop variables
    Row_Idx = 1       // Row Loop Indice [not used ?]
    Row_Pos = Row_Top // Reset the Y Print value (Row_Pos)
    // Issue a Page feed
    If Print_Page_Number >= Print_Page_End Then
      Exit // The asked number of pages have been printed !
    Else // I can print another page
      Print_Page_Number = Print_Page_Number + 1 // Inc the Page #
      g.NextPage // Issue a Form Feed (next page to print
    End If
  Else // Update the variables to print the next line
    Row_Pos = Row_Pos + Row_Top // Row_Top is filled far above (only one location to change its value)
    Row_Idx = Row_Idx + 1
  End If
End If

// To avoid 1, Infinite Loop
If UserCancelled Then Exit

End Sub[/code]