Microsoft Visual Basic 2010 Step by Step eBook

(Tina Meador) #1

418 Part III Designing the User Interface


events that represent crucial actions in the life cycle of an object. You have been
working with event procedures several times already. For example, you just created the
Click event procedure for the Button1 object. The AddHandler statement is a way to
manually “wire up” an event procedure.
In this case, the event procedure being specified is related to printing services, and
the request comes with specific information about the page to be printed, the current
printer settings, and other attributes of the PrintDocument class. Technically, the
AddressOf operator is used to identify the PrintGraphic event procedure by determining
its internal address and storing it. The AddressOf operator implicitly creates an object
known as a delegate that forwards calls to the appropriate event procedure when an
event occurs.
The third line of the code you just entered uses the Print method of the
PrintDocument1 object to send a print request to the PrintGraphic event procedure.
This print request is located inside a Try code block to catch any printing problems that
might occur during the printing activity. I introduced the Try... Catch error handler in
Chapter 9, “Trapping Errors by Using Structured Error Handling .” Here the ex variable is
being declared of type Exception to get a detailed message about any errors that occur.


  1. Scroll above the Button1Click event procedure in the Code Editor to the general
    declaration space below the Public Class Form1 statement. Then type the following
    PrintGraphic event procedure:
    'Sub for printing graphic
    Private Sub PrintGraphic(ByVal sender As Object,

    ByVal ev As PrintPageEventArgs)
    ' Create the graphic using DrawImage
    ev.Graphics.DrawImage(Image.FromFile(TextBox1.Text), _
    ev.Graphics.VisibleClipBounds)
    ' Specify that this is the last page to print
    ev.HasMorePages = False
    End Sub
    This routine handles the printing event generated by the PrintDocument1.Print method.
    I’ve declared the Sub procedure within the form’s code, but you can also declare the
    Sub as a general-purpose procedure in a module. Note the ev variable in the argument
    list for the PrintGraphic procedure. This variable is the crucial carrier of information
    about the current print page, and it’s declared of type PrintPageEventArgs, a class in the
    System.Drawing.Printing namespace.
    To actually print the graphic, the procedure uses the Graphics.DrawImage method
    associated with the current print page to load a graphics file by using the file name
    stored in the Text property of the TextBox1 object. (By default, I set this property to
    C:\Vb10sbs\Chap15\Sun .ico—the same Sun icon used in Chapter 15, “Adding Graphics
    and Animation Effects”—but you can change this value at run time and print any
    artwork files that you like .) Finally, I set the ev.HasMorePages property to False so that
    Visual Basic understands that the print job doesn’t have multiple pages.

  2. Click the Save All button on the Standard toolbar to save your changes, and then
    specify the C:\Vb10sbs\Chap17 folder as the location.

Free download pdf