The PrintForm-Component in the Visual Basic Powerpack for Windows Forms

Did you ever wanted to print a Form without calling any native code? Well, with Windows Forms and Visual Studio 2008 this is a really simple exercise. When you design your WinForms-Application you’ll find a Tab Visual Basic PowerPacks in the Toolbox of Visual Studio 2008. In this Tab, you’ll find a component called PrintForm. Simply drop this component on your form, and you’ll find all the functionality you need. Let’s take a look at a small sample by using a very simple Windows Forms application.

The application just has a Browse-Button to browse an image from the filesystem, a PictureBox to display the image and a TextBox displaying the path to the image.

printForm_pic1

The Browse-Button does nothing more than showing an OpenFileDialog and setting the Text-Property of the TextBox to the selected file and assigning the chosen image to the Image-Property of the PictureBox.

private void btnBrowse_Click(object sender, EventArgs e)
{
  var dlg = new OpenFileDialog
    {
      Multiselect = false,
      Filter = "JPEG (*.jpg)|*.jpg|JPEG (*.jpeg)|*.jpeg"
               + "|GIF(*.gif)|*.gif|Bitmap (*.bmp)|*.bmp"
    };
  if(dlg.ShowDialog() == DialogResult.OK)
  {
    txtImagePath.Text = dlg.FileName;
    pictureBox1.Image = Image.FromFile(dlg.FileName);
  }
}

The Form above already contains a "Print Form!"-Button. The eventhandler of that Button is currently empty. Let’s implement it. But before that, go to the designer and drag’n’drop a PrintForm-component out of the Toolbox on the Form.

printForm_pic2

After dropping the PrintForm-component, you’ll find it in the Component Tray of the Windows Forms-Designer. It’s named printForm1 by default. I won’t change that here. Now let’s step to the EventHandler of the "Print Form!"-Button.

To print the form, first a PrintDialog is shown allowing the user to select a printer. Then the settings of the PrintDialog are assigned to the PrinterSettings-Property of the printForm1-component. The Print-Method on that component is called passing in the Form (this) and an Enum-Value specifying to print the full Window.

private void btnPrintForm_Click(object sender, EventArgs e)
{
  var dlg = new PrintDialog();
  if(dlg.ShowDialog()==DialogResult.OK)
  {
    printForm1.PrinterSettings = dlg.PrinterSettings;
    printForm1.Print(this, PrintForm.PrintOption.FullWindow);
  }
}

That’s all! :-) E.g. when I print into my PDFPrinter, the resulting PDF-Document will look like this:

printForm_pic3

Beside the PrinterSettings-Property the PrintForm-Component contains some more important properties . The most mentionable one is the PrintAction-Property. It’s of type PrintAction, an enum in Namespace System.Drawing.Printing. The PrintAction-enum contains three values, PrintToPrinter (the default for the PrintForm’s PrintAction-Property), PrintToFile and PrintToPreview.

So to show a preview of the Print-output, you simply need the following to lines:

printForm1.PrintAction = PrintAction.PrintToPreview;
printForm1.Print(this, PrintForm.PrintOption.FullWindow);

Replace the code in btnPrintForm_Click with the two lines above, and you’ll automatically get a preview, like shown below. But be careful. The user can print out of the Preview. If you’ve no PrinterSettings assigned to the PrinterSettings-Property of the PrintForm, the Print will be implicitly written to the users default-Printer.

printForm_pic4

So, that’s all. Get the sample here.

Share this post

Comments (9)

  • andy Reply

    Hi Thomas,

    First thanks for the step by step instruction on printform article.
    I can work with this for single form but If I want to use this functionality for multiple
    forms I’m getting problem.

    I have two forms, form1 and form2. On button click on form 1 I am sending some values to form2 and displaying it. I want to print only form2 with the displayed content.

    Is it possible to do it.

    Please help me with this.

    August 2, 2010 at 5:03 pm
  • jitesh Reply

    thanks, i really appreciate it

    September 6, 2011 at 8:52 pm
  • jnel Reply

    simple code but rocks! help me alot!

    April 11, 2012 at 10:04 am
  • Keetu Reply

    I was struggling with Print Dialog and Print Document Tools. This was a life saver. Thanks a ton!

    For VB code:
    Private Sub Printbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Printbtn.Click
    Dim dlg As New PrintDialog()
    Dim result As DialogResult = dlg.ShowDialog
    If result = DialogResult.OK Then
    PrintForm1.PrinterSettings = dlg.PrinterSettings
    PrintForm1.Print()
    End If

    End Sub

    March 21, 2013 at 3:07 pm
  • Steve Reply

    My form is bigger than the screen and has a datagridview of a sub-table on it of meeting dates, checkboxes and text field.

    I need this all to print – ok if it goes to 2 pages.

    January 30, 2014 at 10:06 pm
  • messi Reply

    Thank you

    February 4, 2015 at 4:44 pm
  • Hundsen Reply

    can you help me to convert my program to be an application

    April 11, 2017 at 7:31 am
  • Hundsen Reply

    visual basic.net 2012

    April 11, 2017 at 7:33 am

Leave a Reply

Your email address will not be published. Required fields are marked *

*