How to print a form in vb.net with multiple tab pages!!
-
Hi This is to print a form which contains a tab control.For this you have to instal Microsoft.Visual Basic.PowerPacks.Printing.dll.Then add one command button and a tab control to your form.Then add some other controls to your tabpages.Drag PrintDocument1 control from the tool box to your form.After that paste the code in the code behind.Press F5 to run it.Click on the button1 to print the pages. Option Strict Off Imports System.Drawing Imports System.Drawing.Printing Imports System.Runtime.InteropServices Public Class Form1 Dim TabId As Integer Dim i As Integer Dim img As Bitmap Dim WithEvents pd As PrintDocument Public Sub New() ' This call is required by the Windows Form Designer. InitializeComponent() ' Add any initialization after the InitializeComponent() call. End Sub Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Me.TabControl1.SelectTab(0) End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Try TabId = Me.TabControl1.TabCount For i = 0 To TabId - 1 Me.TabControl1.TabIndex = i Me.TabControl1.SelectTab(i) 'MsgBox(i) Me.Refresh() img = CaptureForm1() pd = New PrintDocument pd.Print() Next Catch ex As Exception MsgBox(ex) End Try End Sub Function CaptureForm1() As Bitmap Dim g1 As Graphics = Me.CreateGraphics() Dim MyImage = New Bitmap(Me.ClientRectangle.Width, (Me.ClientRectangle.Height), g1) Dim g2 As Graphics = Graphics.FromImage(MyImage) Dim dc1 As IntPtr = g1.GetHdc() Dim dc2 As IntPtr = g2.GetHdc() BitBlt(dc2, 0, 0, Me.ClientRectangle.Width, (Me.ClientRectangle.Height), dc1, 0, 0, 13369376) g1.ReleaseHdc(dc1) g2.ReleaseHdc(dc2) 'saves image to c drive just, u can comment it also MyImage.Save("c:\abc.bmp") Return MyImage End Function Sub pd_PrintPage(ByVal sender As Object, ByVal e As PrintPageEventArgs) Handles pd.PrintPage Dim x As Integer = e.MarginBounds.X Dim y As Integer = e.MarginBounds.Y e.Graphics.DrawImage(img, x, y) e.HasMorePages = False End Sub