Create a Printing Job in Windows Application using VB.NET

Here in this aricle, I am explaining how to create a printing job in windows application.
  • 7394

Introduction

Here in this aricle, I am  explaining how to create a printing job in windows application. Here printing in Windows Forms specifies the PrintPage event. you will write code to handle this event for specifying what to write and what to print. For loading the page you will write code on the main page of the form by double click on the form page. For printing you will write code on print document control by double click on the print button. you also want to write code for Begin and End events. In this you can add a PrintDialog to your form so that you can  provide a clean user interface to the users. Print preview component is used to show printed sheet on a Form. It is implemented by PrintPreview dialog button from the ToolBox. In this add a Print document component, Printpreview component and a Button control from the ToolBox to the main form.

Steps to be followed for the coding are:

Step1: Open the visual Studio 2010, click on the new project.

Step2: Select the VB category and click on the Windows Form Apllication.

Step3: Start adding controls on the Form from the toolbox.

Step4: Double click on the Main Form, it generates a default code for the page loading in which you have to add the rest of the code as shown below.

Private Sub MainForm_Load(ByVal sender As System.ObjectByVal e As System.EventArgs)Handles MyBase.Load
 

        AddHandler PrintDocument1.PrintPage, AddressOf Me.printDocument1_PrintPage

        Me.PrintPreviewDialog1.Document = Me.PrintDocument1

    End Sub

St
ep5:
 Now click on the button, it will show the default code of the click event where you have to do the rest of the code.

 

    Private Sub btnPrint_Click(ByVal sender As ObjectByVal e As EventArgsHandles btnprint.Click

        Me.PrintPreviewDialog1.ShowDialog()

    End Sub

 
Step6: Now right click on the Print document button, default code will be generate where you have to do the rest of the coding.

Private Sub printDocument1_PrintPage(ByVal sender As ObjectByVal e AsSystem.Drawing.Printing.PrintPageEventArgs)

        w(Pen(Color.Black))

        Dim f As Font = New Font("Vanada", 15)

        Dim br As SolidBrush = New SolidBrush(Color.Black)

        Dim p As Pen = Ne

        e.Graphics.DrawString("Hi i am Ankur.", f, br, 100, 100)

        e.Graphics.DrawRectangle(p, 50, 100, 300, 150)

    End Sub

The whole code will look like this:
 

Public Class Form1

 

    Private Sub printDocument1_PrintPage(ByVal sender As ObjectByVal e AsSystem.Drawing.Printing.PrintPageEventArgs)

        (Pen(Color.Black))

        Dim f As Font = New Font("Vanada", 15)

        Dim br As SolidBrush = New SolidBrush(Color.Black)

        Dim p As Pen = New Pen(Color.Black)

        e.Graphics.DrawString("Hi i am Ankur.", f, br, 100, 100)

        e.Graphics.DrawRectangle(p, 50, 100, 300, 150)

    End Sub

 

    Private Sub MainForm_Load(ByVal sender As System.ObjectByVal e As System.EventArgs)Handles MyBase.Load

 

        ' Handle the PrintPage event to write the print logic.

        AddHandler PrintDocument1.PrintPage, AddressOf Me.printDocument1_PrintPage

 

        ' Specify a PrintDocument instance for the PrintPreviewDialog component.

        Me.PrintPreviewDialog1.Document = Me.PrintDocument1

    End Sub

 

    Private Sub btnPrint_Click(ByVal sender As ObjectByVal e As EventArgsHandles btnprint.Click

        Me.PrintPreviewDialog1.ShowDialog()

    End Sub 

End Class

 
Step7: Press F5 for debug the code you will get the output.

 

 01.jpg

 02.jpg

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.