Click here to Skip to main content
Click here to Skip to main content

Form Print Control

, 21 Jan 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
An extender control to make printing a form quick, easy, and flexible.

Introduction

One of the most common questions is "how can I print / print preview my form"? This is quite easily done by getting the form to "draw itself" on the printed page or using the VB Power Packs to do likewise, but this is often unsatisfactory because:

  • The location of components on the form may not be where you want them on the printed page
  • Text is drawn at screen resolution
  • Every control is drawn (e.g., buttons, the form border etc.)

The attached component addresses the problem in a different manner.... What you do is drop a FormPrintHelperComponent component onto your form and it extends each control on that form with a set of new properties (look in the Properties window for each control in the section named "MCL Form Print Helper"). 

Printing helper extended properties

You then set these to indicate:

  • Whether or not the control should be printed
  • Whether to print it as Text, Image, or Owner Drawn
  • The boundary rectangle to print it in
  • The vertical / horizontal alignment within that area
  • Font and background/foreground colour to use etc.

The component then allows you to preview the printed page at design time, and to preview or print it at run time.

How it works

The component implements System.ComponentModel.IExtenderProvider which means it provides properties to the other controls on the form - in the same way as the built-in ToolTip and F1 Help provider components do.

The class is decorated with ProvideProperty attributes which tells the IDE which properties the control provides to extend the properties of the controls on the form. Underlying each of these "properties" are a matched pair of a function and sub routine to get/set the property for the control passed in, which are called if an extended property is changed in the target control's property window.

Extended properties provided by the control

When the component is invoked to print or print preview, it iterates through all the controls on the form that have the extended property Print set to true. It then prints the control according to the other extended properties provided for that control such as boundary rectangle, font, foreground and background colour etc.

The PrintMode extended property affects how the control is printed. There are three options:-

  • PrintAsText - Prints the "Text" property of the control
  • PrintAsImage - Prints the "Image" property of the control if it has one; otherwise, prints the control as a bitmap exactly as it appears on screen
  • PrintAsOwnerDrawn - Passes the control to the OwnerDrawnControlprint event so the application can decide how to print the control

"Print as image" printing functionality

If the control doesn't have an Image attribute, the control is drawn onto a bitmap, which is then printed on the form by using the control class' DrawToBitmap method:

Private Function GetControlImage(ByVal ctlIn As Control) As Image

    Dim Imageproperty As System.Reflection.PropertyInfo
    Imageproperty = ctlIn.GetType.GetProperty("Image", GetType(Image))
    If Imageproperty Is Nothing Then
        Dim imgNew As New Bitmap(ctlIn.Width, ctlIn.Height)
        ctlIn.DrawToBitmap(imgNew, ctlIn.ClientRectangle)
        Return imgNew
    Else
        Return CType(Imageproperty.GetValue(ctlIn, Nothing), Image)
    End If
End Function

"Owner drawn" printing functionality

If you set the extended property PrintMode to PrintAsOwnerDrawn, then at run time, when the print or print preview logic is invoked, an OwnerDrawnControlPrint event will be raised to allow you to programmatically draw the control onto the page canvas.

Private Sub FormPrintHelperComponent1_OwnerDrawnControlPrint(ByVal sender As Object,  _ 
            ByVal e As MCL_Form_Print_Control.OwnerPrintControlEventArgs) _ 
            Handles FormPrintHelperComponent1.OwnerDrawnControlPrint

    '\\ The application code should draw this 
    '\\ control because it is special....
    With e
        If .ControlToPrint.Name = "DateTimePicker1" Then
            e.Canvas.FillRectangle(Brushes.Aqua, e.BoundingRectangle)
            e.Canvas.DrawString(">> " & .ControlToPrint.Text & _
                                "<<",  .ControlToPrint.Font, _
                                Brushes.Azure, e.BoundingRectangle)
        End If
    End With
End Sub

Worked example

First add a reference to the control's DLL to your application, and (optionally) add it to the toolbox. 

The next step is to layout your Windows form in Design mode with all the controls that you want on it. (You can add controls at a later date, and the extender will pick them up, so don't worry about having the full design in place to start with). You then drag an instance of the component from the tool bar on to your form.

Then, select the control(s) you want to print - e.g., on the form in the demo project in the source code above, the controls Label_Databasename, CheckedListBox_Databases etc. are to be printed, but the user does not want to print the buttons Button1 and Button2.

On the control properties pane for the controls to be printed, navigate down to the section "MCL Form Print Utility" and set the extended property named "Print on FormPrintHelperComponent1" to true

Setting the rich text box mode to PrintAsRichText means the content of a rich text box is printed in what-you-see-is-what-you-get mode.

Selecting where to print the component on the page

By default, the property "BoundingRectangle on FormPrinthelperComponent1" is set to the same rectangle as the control occupies on the form. You can alter this (to make the layout more suited to the size and orientation of the printed page) by altering the values in this property. To see how this change looks, you can preview the form print layout by selecting the FormPrintHelperComponent instance, and from its smart tag menu, select the option "Preview Document". 

Other extender provided properties

The component provides the following extended properties to the form on which it is sited:

  • DocumentName - The name of the document as it will appear in the print queue when printed. This defaults to the name of the form if not set.
  • PaperKind - The paper size to use to print the document (A4, A5, letter etc.).

The component provides the following extended properties to all the controls on the form on which the component is sited:

  • BackColour - The colour to fill the bounding rectangle of the control before printing it. If this is transparent, then the bounding rectangle will not obscure items printed before it.
  • BoundingRectangle - Where on the page to print the component.
  • ForeColour - The foreground colour to use to print the control (this has no effect if the print mode is not PrintAsText).
  • HorizontalAlignment, VerticalAlignment - How to align the control's text relative to its bounding rectangle for printing.
  • Print - True if it should be printed on the page, false if not.
  • PrintFont - The font to use to print the control's text if the print method is PrintAsText.
  • PrintMethod - How to print the control (see explanation above).
  • TopBorder, BottomBorder, LeftBorder, RightBorder - Pens to use to draw a border around the bounding rectangle on the printed document.

Multi-page documents

Of course, there are many cases where a single form needs to be printed over multiple logical pages - for example, if a form has many tabs on it, you might find it sensible to print one logical page per tab.

The component implements this functionality by having a property LogicalPages which is a positive number 1 or greater that is the number of logical pages that this form should be printed over. Then, for each control, two additional extended properties are added that control which pages that control is to be printed on: MultiPageprintMethod which is an enumerated type:

  • PrintOnEveryPage - Print the control on every logical page
  • PrintOnOddPages - Print the control on odd numbered logical pages
  • PrintOnEvenPages - Print the control on even numbered logical pages
  • PrintOnlyOnSpecifiedLogicalPages - Print the control only on the specified pages

If the MultiPageprintMethod is set to PrintOnlyOnSpecifiedLogicalPages, then a second extended property, PrintOnPages (which is a list of boolean values, one for each logical page) is used to set the page(s) to print it on.

Logical pages?

When the data in a control (such as a multi-line textbox, a rich textbox, or a grid) is too large for the area allocated to print it on the document, you can optionally set it to flow over to a new page until it reaches the end of the data by setting the extended property DataOverflowAction to CreateNewPage. However, the new page will be the same logical page as the current one (with all the same controls on it), but will be a different physical page. The use of logical pages at the design stage allows a multi part document to be created that does not need to change at run time to accommodate extra data.

Updates 

  • Added a UITypeEditor to allow you to set the colour and line width of all the border lines.
  • Added an extended property Trimming which controls how words wrap within their bounding rectangle.
  • Added an extended property PropertyToPrint to allow you to specify which property of the control you want to print.
  • Added a PrinterSettings method that invokes the printer setup dialog box to allow the settings to be changed at run time.
  • Added a PageSetup method that invokes a page setup dialog box to allow the user to change the page layout at run time.
  • Added multi-page functionality.
  • Added code to print a rich text box as rich text (what-you-see-is-what-you-get) by setting RTFPrintMethod
  • Added C# version of the component. 

License

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

Share

About the Author

Duncan Edwards Jones
Software Developer (Senior)
Ireland Ireland
C# / SQL Server developer
Microsoft MVP 2006, 2007
Visual Basic .NET
Follow on   Twitter   LinkedIn

Comments and Discussions

 
GeneralRe: Bounding Rectangle always 0,0,0,0 for me too PinmemberDuncan Edwards Jones23-Aug-09 1:19 
GeneralRe: Bounding Rectangle always 0,0,0,0 for me too PinmemberLimito21-Oct-09 6:16 
GeneralRe: Bounding Rectangle always 0,0,0,0 for me too PinmemberDuncan Edwards Jones13-Nov-09 1:39 
GeneralRe: Bounding Rectangle always 0,0,0,0 for me too [modified] PinmemberLimito13-Nov-09 7:10 
GeneralRe: Bounding Rectangle always 0,0,0,0 for me too PinmemberDuncan Edwards Jones16-Feb-10 3:59 
GeneralPrint Form Control PinmemberPat Tormey28-Jan-09 2:45 
GeneralRe: Print Form Control PinmemberDuncan Edwards Jones1-Feb-09 7:43 
GeneralSetting printer PinmemberGalena7-May-08 7:20 
GeneralRe: Setting printer PinmemberDuncan Edwards Jones7-May-08 7:55 
GeneralRe: Setting printer [modified] PinmemberDuncan Edwards Jones13-May-08 7:41 
GeneralRe: Setting printer PinmemberGalena13-May-08 8:24 
GeneralFont size, VB 2008, printer PinmemberGalena19-Apr-08 8:53 
GeneralRe: Font size, VB 2008, printer PinmemberDuncan Edwards Jones19-Apr-08 12:26 
GeneralRe: Font size, VB 2008, printer PinmemberGalena19-Apr-08 14:00 
GeneralRe: Font size, VB 2008, printer PinmemberDuncan Edwards Jones20-Apr-08 0:54 
GeneralPrint Preview PinmemberBrave_Gass4-Feb-08 23:48 
GeneralRe: Print Preview PinmemberDuncan Edwards Jones5-Feb-08 9:43 
GeneralBounding Rectangle always 0,0,0,0 PinmemberMember 465625022-Jan-08 7:24 
GeneralRe: Bounding Rectangle always 0,0,0,0 PinmemberDuncan Edwards Jones22-Jan-08 10:43 
GeneralRe: Bounding Rectangle always 0,0,0,0 PinmemberGrahamEd23-Jan-08 0:18 
GeneralRe: Bounding Rectangle always 0,0,0,0 PinmemberGrahamEd23-Jan-08 0:37 
GeneralRe: Bounding Rectangle always 0,0,0,0 PinmemberDuncan Edwards Jones29-Feb-08 10:59 
QuestionProblem with Labels [modified] PinmemberDavid Minder15-Jan-08 4:10 
GeneralRe: Problem with Labels PinmemberDuncan Edwards Jones22-Jan-08 10:48 
QuestionPtinting to file, logical pages PinmemberGalena19-Aug-07 2:01 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.1411028.1 | Last Updated 21 Jan 2009
Article Copyright 2007 by Duncan Edwards Jones
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid