RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Raise Printing to a Fine Art in Windows Forms : Page 2

Learn how to format, preview, and print output the way you want it to look using the capabilities of the .NET System.Drawing.Printing namespace and the PrintDocument class.

Coding the Form
You are now ready to code the application. Double-click on Form1 to switch to the code-behind. In the Form1_Load event, code the following:

   Private Sub Form1_Load( _
      ByVal sender As System.Object, _
      ByVal e As System.EventArgs) Handles MyBase.Load
   End Sub
The updateBarcode() subroutine in the preceding code updates the encoding on the barcode based on the current record displayed as shown below:

   Private Sub updateBarcode()
      BarcodeProfessional1.Code = _
         EmployeeIDTextBox.Text & "-" & _
         LastNameTextBox.Text & " " & _
   End Sub
Figure 6. Adding a Print Button: Add a new button control to the form so users can access the print functionality.
When the current record changes, you also need to update the barcode. You do this through the CurrentChanged event of the EmployeesBindingSource control (which Visual Studio automatically adds to the form when you drag and drop the Employees table from the Data Sources window) and add a call to the updateBarcode() subroutine:

   Private Sub EmployeesBindingSource_CurrentChanged( _
      ByVal sender As System.Object, _
      ByVal e As System.EventArgs) _
      Handles EmployeesBindingSource.CurrentChanged
   End Sub
Adding Print Functionality
The next step is to add printing functionality to the form. Add a Button control to the bottom of the form and name it btnPrint (see Figure 6).

The Print button formats and sends the on-screen record to the printer. It will print a name card containing the current employee's details. Figure 7 shows what the name card will look like when printed. The positions of the various elements in the name card are stored in predefined constants.

Figure 7. Name Card: The figure shows the various positions of the information on the name card.
Switching to the code-behind of Form1, import the following namespace:

   Imports System.Drawing.Printing
This namespace contains the classes for printing in Windows Forms. Next, define the following constants and variables:

   Const COMPANY_NAME As String = "The XYZ Corporation"
   Const MARGIN_X As Integer = 10
   Const MARGIN_Y As Integer = 10
   Const CARD_WIDTH As Integer = 350
   Const CARD_HEIGHT As Integer = 200
   Const COMPANY_NAME_Y As Integer = 110
   Const EMPLOYEE_NAME_Y As Integer = 150
   Const EMPLOYEE_TITLE_Y As Integer = 165
   Const EMPLOYEE_DOB_Y As Integer = 180
   Private f_companyname As Font
   Private f_employeename As Font
   Private f_title As Font
   Private f_DOB As Font
When users click on the Print button, you first instantiate an instance of the PrintDocument class. The PrintDocument class defines the various methods that allow you to send output to the printer. There are three main events that you need to service in order to use the PrintDocument class to send output to the printer. They are:

  • BeginPrint—Occurs when the Print method is called and before the first page of the document prints. Typically, you make use of the BeginPrint event to initialize fonts, file streams, and manipulate other resources used during the printing process.
  • PrintPage—Occurs when the output to print for the current page is needed. This is the main event where you code the logic required for sending the output to the printer.
  • EndPrint—Occurs after the last page of the document has printed. Typically, you use the EndPrint event to release fonts, file streams, and other resources.
To start the printing process, use the Print() method of the PrintDocument class.

Code the Print button's Click event as follows:

   Private Sub btnPrint_Click( _
      ByVal sender As System.Object, _
      ByVal e As System.EventArgs) _
      Handles btnPrint.Click
      Dim printDoc As New PrintDocument()
      AddHandler printDoc.BeginPrint, _
         New PrintEventHandler(AddressOf Me._beginPrint)
      AddHandler printDoc.PrintPage, _
         New PrintPageEventHandler( _
         AddressOf Me._printPage)
      AddHandler printDoc.EndPrint, _
         New PrintEventHandler(AddressOf Me._endPrint)
   End Sub
In the BeginPrint event handler, initialize the fonts used while printing:

   Private Sub _beginPrint( _
      ByVal sender As Object,ByVal e As PrintEventArgs)
      '---initialize the fonts---
      f_employeename = New Font("Arial", 10, _
      f_title = New Font("Arial", 8)
      f_DOB = New Font("Arial", 8, FontStyle.Italic)
   End Sub
When printing ends, de-reference all the font variables used in the EndPrint event handler:

   Private Sub _endPrint( _
      ByVal sender As Object, _
      ByVal e As PrintEventArgs)
      '---de-reference the fonts---
      f_employeename = Nothing
      f_title = Nothing
      f_DOB = Nothing
   End Sub
You do the bulk of the work to send output to the printer in the PrintPage event handler by using the Graphics object in the PrintPageEventArgs class to specify the output you desire. For example, to draw a rectangle you would use the e.Graphics.DrawRectangle() method (where e is an instance of the PrintPageEventArgs class). To print a string, you use the e.Graphics.DrawString() method. You can specify the font to be used for each string by using the Font class.

Code the PrintPage event handler as shown in Listing 1.

That's it! To test the application, press F5. You can navigate to a particular employee and click the Print button. Your printer should print the name card of the selected employee.

Author's Note: You need to ensure that you have at least one printer driver installed on your computer. The printout will be sent to the printer configured as the default printer.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date