Click here to Skip to main content
15,880,503 members
Articles / Programming Languages / Visual Basic
Article

How to Print Data from Datagrid with All Pages (from Paging) in Silverlight with VB.NET

Rate me:
Please Sign up or sign in to vote.
3.64/5 (7 votes)
5 Aug 2011CPOL2 min read 36K   1.8K   12   5
This example shows how to print and display print previews in VB .NET. It shows how to use the PrintDocument object to print, how to print with the PrintDialog control, and how to display a print preview with the PrintPreviewDialog.

Introduction

This example shows how to print and display print previews in VB.NET. It shows how to use the PrintDocument object to print, how to print with the PrintDialog control, and how to display a print preview with the PrintPreviewDialog.

To print a document or provide a preview, the program creates a PrintDocument object. It assigns event handlers to this object's BeginPrint, QueryPageSettings, PrintPage, and EndPrint events. PrintPage is the one that generates the output and it's the only one that is required. In this program, subroutine PreparePrintDocument creates the PrintDocument and sets its PrintPage event handler.

The event handler draws on the Graphics object it is passed as a parameter. It uses the e.MarginBounds parameter to learn where the margins are on the page. It finishes by setting e.HasMorePages to False to indicate that the printout is done.

VB.NET
' Make and return a PrintDocument object that's ready
' to print the paragraphs.
Private Function PreparePrintDocument() As PrintDocument
    ' Make the PrintDocument object.
    Dim print_document As New PrintDocument

    ' Install the PrintPage event handler.
    AddHandler print_document.PrintPage, AddressOf _
        Print_PrintPage

    ' Return the object.
    Return print_document
End Function

' Print the next page.
Private Sub Print_PrintPage(ByVal sender As Object, ByVal e _
    As System.Drawing.Printing.PrintPageEventArgs)
    ' Draw a rectangle at the margins.
    e.Graphics.DrawRectangle(Pens.Black, e.MarginBounds)

    ' Draw a thick, dashed ellipse.
    Dim dotted_pen As New Pen(Color.Black, 5)
    dotted_pen.DashStyle = Drawing2D.DashStyle.Dash
    e.Graphics.DrawEllipse(dotted_pen, e.MarginBounds)
    dotted_pen.Dispose()

    ' Draw a thick diamond.
    Dim x0 As Integer = e.MarginBounds.X
    Dim y0 As Integer = e.MarginBounds.Y
    Dim wid As Integer = e.MarginBounds.Width
    Dim hgt As Integer = e.MarginBounds.Height
    Dim pts() As Point = { _
        New Point(x0, y0 + hgt \ 2), _
        New Point(x0 + wid \ 2, y0), _
        New Point(x0 + wid, y0 + hgt \ 2), _
        New Point(x0 + wid \ 2, y0 + hgt) _
    }
    e.Graphics.DrawPolygon(New Pen(Color.Black, 5), pts)

    ' There are no more pages.
    e.HasMorePages = False
End Sub

To display a print preview, the program uses the PreparePrintDocument function to make a PrintDocument object and saves the result in a PrintPreviewDialog's Document property. It calls the dialog's ShowDialog method and the rest is automatic. The user can use the dialog to zoom in and out, examine the printouts different pages (in this program, the printout only has one page), and print the document.

VB.NET
' Display a print preview.
Private Sub btnPrintPreview_Click(ByVal sender As _
    System.Object, ByVal e As System.EventArgs) Handles _
    btnPrintPreview.Click
    ' Make a PrintDocument and attach it to 
    ' the PrintPreview dialog.
    dlgPrintPreview.Document = PreparePrintDocument()

    ' Preview.
    dlgPrintPreview.WindowState = FormWindowState.Maximized
    dlgPrintPreview.ShowDialog()
End Sub

To display the print dialog, the program uses the PreparePrintDocument function to make a PrintDocument object and saves the result in a PrintDialog's Document property. It calls the dialog's ShowDialog method and the rest is automatic. The user can use the dialog to select the printer and change the printer's settings, and then launch or cancel the printout.

VB.NET
' Print with the print dialog.
Private Sub btnPrintWithDialog_Click(ByVal sender As _
    System.Object, ByVal e As System.EventArgs) Handles _
    btnPrintWithDialog.Click
    ' Make a PrintDocument and attach it to 
    ' the Print dialog.
    dlgPrint.Document = PreparePrintDocument()

    ' Display the print dialog.
    dlgPrint.ShowDialog()
End Sub

To print the document immediately to the currently selected printer, the program uses the PreparePrintDocument function to make a PrintDocument object and calls that object's Print method.

VB.NET
' Print immediately.
Private Sub btnPrintNow_Click(ByVal sender As _
    System.Object, ByVal e As System.EventArgs) Handles _
    btnPrintNow.Click
    ' Make a PrintDocument object.
    Dim print_document As PrintDocument = _
        PreparePrintDocument()

    ' Print immediately.
    print_document.Print()
End Sub

History

  • 4th August, 2011: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 2 Pin
JP®19-Mar-12 3:36
JP®19-Mar-12 3:36 
GeneralMy vote of 1 Pin
Росен Христов18-Jan-12 3:15
Росен Христов18-Jan-12 3:15 
QuestionSilverlight? Pin
Jay Douglas10-Aug-11 7:10
Jay Douglas10-Aug-11 7:10 
GeneralMessage Closed Pin
10-Aug-11 23:43
sathishsingam10-Aug-11 23:43 
GeneralRe: Silverlight? Pin
Росен Христов18-Jan-12 3:10
Росен Христов18-Jan-12 3:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.