Printing Multi-page ListBox

Hello. If someone can help me I would be most pleased.

I have a ListBox with many lines but when I want to print, I can only get 1 page although there are many more lines to print.

I have desperately been trying all combinations of code with the right logic. I think

Please help. Thankyou. Ali.




Answer this question

Printing Multi-page ListBox

  • Jassim Rahma

    Cheers nobugz - It's all these 'little' bits that we (Me) have got to get a grip of.

    Thanks.



  • Keith Craig

    Hello. I looked into VB-Tips. Wow! Way above me at the moment - but I shall learn. I am teaching myself and don't know of any groups other than on the web so perhaps someone could put me right with this bit of code. ( On the button-click I have PrintPreviewDialog.ShowDialog) then this on PrintPage:

    Dim LinesPerPage As Single = 0

    Dim f As New Font("Arial", 16, FontStyle.Regular)

    Dim yPos As Single

    Dim count As Integer = 0

    Dim x As Single = e.MarginBounds.Left

    Dim y As Single = e.MarginBounds.Top

    Dim item As String = Nothing

    LinesPerPage = e.MarginBounds.Height / f.GetHeight(e.Graphics)

    Console.WriteLine("Lines = " & LinesPerPage)

    While count < LinesPerPage

    For Each item In lstToken.Items

    yPos = y + count * f.GetHeight(e.Graphics)

    e.Graphics.DrawString(item, f, Brushes.Black, x, yPos, New StringFormat())

    Next

    count += 1

    Console.WriteLine("Count = " & count)

    End While

    End Sub

    In the output window I get equality with numbers. This code is nearly right What do I need to do so it runs correctly Please help because this is holding-up my learning



  • Oren Novotny

    Your code looks good, it just needs a little work to make it print more than one page. You just need to keep track where you left off printing the previous page. Here's an example:

    Private WithEvents mPrint As PrintDocument
    Private mListIx As Integer

    Private Sub mPrint_BeginPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles mPrint.BeginPrint
    mListIx = 0
    End Sub

    Private Sub mPrint_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles mPrint.PrintPage
    Dim count As Integer
    Dim x As Single = e.MarginBounds.Left
    Dim y As Single = e.MarginBounds.Top
    Dim f As New Font("Arial", 16, FontStyle.Regular)
    Dim LinesPerPage As Integer = CInt(e.MarginBounds.Height / f.GetHeight(e.Graphics))
    Do While mListIx < ListBox1.Items.Count
    Dim ypos As Single = y + count * f.GetHeight(e.Graphics)
    e.Graphics.DrawString(ListBox1.Items(mListIx).ToString, f, Brushes.Black, x, ypos, StringFormat.GenericDefault)
    mListIx += 1
    count += 1
    If count >= LinesPerPage Then
    e.HasMorePages = True
    Exit Sub
    End If
    Loop
    End Sub



  • DaveC426913

    There is an example on the VB-Tips website on how to print multipage documents.


  • BigMouthTortoise

    Put "Imports System.Drawing.Printing" at the top of the source code file.

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    mPrint = New PrintDocument
    mPrint.Print
    mPrint.Dispose()
    End Sub



  • Recep TARAKCIO?LU

    nobugz wrote:
    Your code looks good, it just needs a little work to make it print more than one page. You just need to keep track where you left off printing the previous page. Here's an example:

    Private WithEvents mPrint As PrintDocument
    Private mListIx As Integer

    Private Sub mPrint_BeginPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles mPrint.BeginPrint
    mListIx = 0
    End Sub

    Private Sub mPrint_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles mPrint.PrintPage
    Dim count As Integer
    Dim x As Single = e.MarginBounds.Left
    Dim y As Single = e.MarginBounds.Top
    Dim f As New Font("Arial", 16, FontStyle.Regular)
    Dim LinesPerPage As Integer = CInt(e.MarginBounds.Height / f.GetHeight(e.Graphics))
    Do While mListIx < ListBox1.Items.Count
    Dim ypos As Single = y + count * f.GetHeight(e.Graphics)
    e.Graphics.DrawString(ListBox1.Items(mListIx).ToString, f, Brushes.Black, x, ypos, StringFormat.GenericDefault)
    mListIx += 1
    count += 1
    If count >= LinesPerPage Then
    e.HasMorePages = True
    Exit Sub
    End If
    Loop
    End Sub

    I just want to say that I have been looking all over google and programmers site and never found a full solution to my ListBox printing questions/problems. This worked first time and great. I thank you very much I come here a lot but alway aksed or looking for help for other issues. Out of desparation I just did a search and man am I happy, just need to do some tweaking and I'm set.


  • FishBlub

    Hello nobugz. I know it is silly to get a printed list from a listbox when I already put all the contents of it into a text file then print the list from that, but I want to know - just for the sake of it, and it will surely help me to get better at 2005.

    In the code I have measured how many linesperpage and I have a For Each item in Listbox........Next loop.

    In between the For and Next I have the e.Graphics.DrawString......line, a count line and another line that says If count = linesperpage then e.HasMorePages= true.

    But what happens is that it just prints what it can on the 1 page, ignoring the rest of the items. Yet in the Output window to a Console.PrintLine it gives every item!

    I am newish so there are things that I have not come across yet.

    Even if I do While count < numberoflines .......etc I can not get it to print the extra.

    Thanks for any help.



  • Garethvdl

    Make sure you exit the PrintPage event handler when you set e.HasMorePages = true. The print engine will call PrintPage again, now to print the 2nd page.


  • GregAbd

    Thanks nobugz and Ken.

    The code you gave was 'great'. I can see now what I was looking for. I changed the code I submitted in accordance with the bits you had in yours - and it works. Thanks. It was half past midnight so I could not write this then!!

    I started a new project with the code you gave. May I ask a couple of questions that will help me understand

    1. Private WithEvents mPrint As PrintDocument

    is the 1st line you gave. Why is PrintDocument wiggle-lined with "is not defined"

    2. I put a listbox and button on form but I could not work out how to get to print event.

    But, anyway, you have made me learn a fair bit.

    Thank you.



  • Siebe

    I assume you use the PrintDocument class to do the printing. In the PrintPage handler, set e.HasMorePages = true to get more than one page.



  • Printing Multi-page ListBox