» Home
» Tutorials
» Search  
» Contact us 

Home » Tutorials » Introduction to Visual Basic .NET » Chapter 7

Chapter 7: Displaying Dialog Boxes
7.6: The PrintDialog Control
7.6.4: The PrintDocument Class
7.6.4.1: How It Works

How It Works

You begin the btnPrint button’s Click event procedure by declaring an object as a PrintDocument. You use this object to perform the actual printing:

Dim objPrintDocument As PrintDocument = New PrintDocument()

Next, you set the DocumentName property for the PrintDocument object. This will be the name that you see when the document is printing and also the name that is shown in the printer queue:

objPrintDocument.DocumentName = “Text File Print Demo”

You then set some properties of the PrintDialog control. This will control the options on the Print dialog box. Since you are only doing basic printing in this example, you want the Print to File check box to be disabled along with the Pages and Selection radio buttons. The next three lines of code do this by setting these properties to False:

PrintDialog1.AllowPrintToFile = False
PrintDialog1.AllowSelection = False
PrintDialog1.AllowSomePages = False

With the PrintDialog control’s properties set, you set its Document property equal to the PrintDocument object:

PrintDialog1.Document = objPrintDocument

Then you show the Print dialog box, so you execute the ShowDialog method of the PrintDialog control in an If statement, as shown in the code next. Notice that you also are checking the DialogResult returned from the PrintDialog control:

If PrintDialog1.ShowDialog() = DialogResult.OK Then

If the user clicks the OK button in the Print dialog box, you actually want to execute the code for printing. The first thing that you do is to set the objStreamToPrint object to a new StreamReader class and pass it the strFileName variable:

objStreamToPrint = New StreamReader(strFileName)

Remember that this variable is set to the path and filename every time that you open or save a file. This will be the file that you print.

Next, you want to set the objPrintFont object to a valid font and font size. You have chosen an Arial font here and a font size of 10 points, but you could have put in any font and size that you wanted:

objPrintFont = New Font(“Arial”, 10)

You now want to add an event handler for the PrintPage event. Since the objPrintDocument object raises this event, you specify this object and the event. You then specify the address of the objPrintDocument_PrintPage procedure:

AddHandler objPrintDocument.PrintPage, _
    AddressOf objPrintDocument_PrintPage

Next, you set the PrinterSettings property of the objPrintDocument object equal to the Printer Settings property of the PrintDialog control. This specifies the printer used, page orientation, and print quality chosen by the user:

objPrintDocument.PrinterSettings = PrintDialog1.PrinterSettings

You then call the Print method of the objPrintDocument object. Calling this method will raise the PrintPage event and the code inside the objPrintDocument_PrintPage procedure will be executed:

objPrintDocument.Print()

In the objPrintDocument_PrintPage procedure, you need to add two parameters: the first of which is the sender. Like every other procedure defined in this project, this argument is an object that lets you know what object called this procedure. The second parameter that you need to add is the Print PageEventArgs object. The PrintPage event receives this argument and it contains data related to the PrintPage event, such as margin boundaries and page boundaries.

Private Sub objPrintDocument_PrintPage(ByVal sender As Object, _
    ByVal e As System.Drawing.Printing.PrintPageEventArgs)

The first thing that you want to do in this procedure is to declare some variables and set their default values. Notice that you are setting the values for the sngLeftMargin and sngTopMargin variables using the PrintPageEventArgs that were passed to this procedure:

Dim sngLinesPerpage As Single = 0
Dim sngVerticalPosition As Single = 0
Dim intLineCount As Integer = 0
Dim sngLeftMargin As Single = e.MarginBounds.Left
Dim sngTopMargin As Single = e.MarginBounds.Top
Dim strLine As String

Next, you want to determine the number of lines that will fit on one page. You do this using the MarginBounds.Height property of PrintPageEventArgs. This property was set when you set the PrinterSettings property of the objPrintDocument to the PrinterSettings property of the PrintDialog control. WYoue divide the MarginBounds.Height by the height of the font that was set in the objPrintFont:

sngLinesPerpage = _
    e.MarginBounds.Height / objPrintFont.GetHeight(e.Graphics)

Next, you read the first line from the text file and place the contents of that line in your strLine variable. Then you enter a loop to read and process all lines from the text file. You only want to process this loop while the intLineCount variable is less than the sngLinesPerPage variable and the strLine variable contains data to be printed:

strLine = objStreamToPrint.ReadLine()
While (intLineCount < sngLinesPerpage And Not (strLine Is Nothing))

Inside your While loop, you set the vertical position of the text to be printed. You calculate this position using the sngTopMargin variable and the intLineCount multiplied by the height of the printer font:

sngVerticalPosition = sngTopMargin + _
    (intLineCount * objPrintFont.GetHeight(e.Graphics))

Using the DrawString method of the Graphics class, you actually send a line of text to the printer. Here you pass the strLine variable (which contains a line of text to be printed), the font to be used when printing, the brush color to be used, the left margin, vertical position, and the format to be used:

e.Graphics.DrawString(strLine, objPrintFont, Brushes.Black, _
    sngLeftMargin, sngVerticalPosition, New StringFormat())

Next, you increment the line count on this page in the intLineCount variable:

intLineCount = intLineCount + 1

If the actual line count is less than the number of lines per page, you want to read another line from the text file to print. Then you go back to the beginning of your loop and process the next line of text:

    If (intLineCount < sngLinesPerpage) Then
        strLine = objStreamToPrint.ReadLine()
    End If

End While

Having completed your While loop, enter an If statement to test the value of strLine:

If (strLine <> Nothing) Then
    e.HasMorePages = True
Else
    e.HasMorePages = False
End If

If the strLine variable is not Nothing, then you have more printing to do so you set HasMorePages property to True, causing the PrintPage event to be fired again, and you will continue to read the text file and print another page.

If strLine is Nothing, you have no more printing to do so you set the HasMorePages property to False, causing the Print method of the objPrintDocument object to end processing and move to the next line of code within the btnPrint_Click event procedure.

Since the printing has completed, you clean up by closing the text file that was used for printing and freeing up the resources used by the objStreamToPrint object:

objStreamToPrint.Close()
objStreamToPrint = Nothing

 

click next Page - The FolderBrowserDialog Control.

Home | Link to Us | Partner Links | About us | Contact us

Copyright © 2009-2012 F1tutorials.com | All Rights Reserved