printing a listbox or a datagrid?


Jun 16, 2004
Programming Experience
Hello, does anybody know how to print the contents of a listbox or a datagrid? I found the code to print a datagrid, but it only prints the part of the screen that is visible. (I also read you can transform the contents of the listbox as a long string and then print it as a textbox, but i can't imagine how it happens!) Thanks for your help


VB.NET Forum Moderator
Staff member
Jun 3, 2004
Lansing, MI; USA
Programming Experience
this is another solution (targeting listbox's) but you can always set up your own type of report if all your doing is looping through listbox's or combobox's here's what you do

add a PrintPreview (named ppdPreviewReport) and a PrintDocument (prtReport)control to your form

Module Level Varibles:
    Private blnHeadingReq As Boolean = True
    Private x As Single
    Private y As Single

Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
        ppdPreviewReport.Document = prtReport
    End Sub

Private Sub PrintHeadings(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)
        Static intPageNo As Integer = 0
        Dim fntHeadingFont As New Font("Arial", 16, FontStyle.Bold)
        Dim stzStringSize As SizeF
        Dim strPageNo As String
        Dim sngPageNoPosition As Single

        With e.Graphics

            y = 25                                          'Set position of first line
            intPageNo += 1                                  'Increment page number
            strPageNo = "Page #" & intPageNo.ToString       'Create the page no as a string
            '                                               'Calculate the X coordinate for centering the page no
            sngPageNoPosition = CSng(e.PageBounds.Width / 2 - e.Graphics.MeasureString(strPageNo, fntHeadingFont).Width / 2)
            '                                               'Draw the Page no on the document
            .DrawString(strPageNo, fntHeadingFont, Brushes.Black, sngPageNoPosition, y)
            y = 50                                          'Advance to the second heading line
            '                                               'Draw the first two coloumn headings
            .DrawString("String Value", fntHeadingFont, Brushes.Black, 100, y)
        End With

        y = 100                                              'Initialize y to top of page
        blnHeadingReq = False                                'set blnHeadingReq = false
End Sub

Private Sub prtReport_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles prtReport.PrintPage
        Static i As Integer = 0
        Static fntPrintFont As New Font("Arial", 10, FontStyle.Regular)
        Static sngPageBottom As Single = e.MarginBounds.Bottom
        Static intEndOfList As Integer = ListBox1.Items.Count - 1
        Static sngLineHeight As Single = fntPrintFont.GetHeight * 2        'dblspaced

        Dim strString As String
        Dim stzStringSize As New SizeF

        Do While i <= intEndOfList And y < sngPageBottom
            If blnHeadingReq Then
                Call PrintHeadings(sender, e)
            End If

            strString = ListBox1.Items(i).ToString
            e.Graphics.DrawString(strLastName, fntPrintFont, Brushes.Black, 100, y)
            y += sngLineHeight                                       'increment y position
            i += 1                                                   'Increment to the next list item

        'Check for bottom of page 
        If y > sngPageBottom Then
            blnHeadingReq = True
            e.HasMorePages = True           'Setting this causes the event to start again
            '                               'like a recursive call.
            y = 0                           'MUST RESET Y or else infinite call backs
            e.HasMorePages = False          'Stop executing the print
        End If

    End Sub

and what that does is put all the items in the listbox on the paper in a column with 1 item per row, then of course i've included multiple pages as well. if you use my code here, you WILL have to modify it for your application good luck

P.S. Paszt that is a neat article, thanx for posting the link
Top Bottom