Printing in VB.NET 2008 question

Posted on 2009-12-30
Last Modified: 2012-05-08
I can't figure out how to include data in the preview dialog.  I will get it to show, but it doesn't contain any data.  What changes do I need to make?  
Thanks in advance.

Public Class frmPrint
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'PrintDocument1.Print()  ' this works, but I want to preview it first
    End Sub

    Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
        Dim printFont As Font
        Dim txtH As Integer = 0
        Dim txtW As Integer = 0
        Dim LMargin As Integer = 0
        Dim TMargin As Integer = 0
        Dim lineToPrint As String

        With PrintDocument1.DefaultPageSettings
            txtH = .PaperSize.Height - .Margins.Top - .Margins.Bottom
            txtW = .PaperSize.Width - .Margins.Left - .Margins.Right
            LMargin = PrintDocument1.DefaultPageSettings.Margins.Left
            TMargin = PrintDocument1.DefaultPageSettings.Margins.Top
        End With

        lineToPrint = "Yahoo!"
        printFont = New Font("Courier New", 10)
        Dim SF As New System.Drawing.StringFormat
        SF.LineAlignment = StringAlignment.Near

        e.Graphics.DrawString(lineToPrint, printFont, Brushes.Black, LMargin, TMargin, SF)
        e.HasMorePages = False

        PrintPreviewDialog1.ShowDialog()  'the dialog shows, but does not show "Yahoo!"
    End Sub
End Class
Question by:Agent909
    LVL 74

    Accepted Solution

    According to

    Do something like the following:
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Me.PrintPreviewDialog1.Document = Me.PrintDocument1
        End Sub

    Open in new window

    LVL 1

    Author Closing Comment

    Works great!  Thank you!
    LVL 74

    Expert Comment

    by:käµfm³d 👽
    NP :)

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    The new Microsoft OS looks great, is easier than ever to upgrade to, it is even free.  So what's the catch?  If you don't change the privacy settings, Microsoft will, in accordance with the (EULA) you clicked okay to without reading, collect all the…
    This collection of functions covers all the normal rounding methods of just about any numeric value.
    Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
    Viewers will learn how to maximize accessibility options in an Excel workbook for users with accessibility issues.

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now