Printing Tutorial

This is the first part of my Print Tutorial ! OK will help you begin =)

In this small tutor I 'll show you some basics of printing with VB 2005, I made and tested my self.
Ok may be the first thing to think on is to find printers on the computer.
So take a look @ this code
Ok add a new combo box and all should works well !
Dim InstalledPrinters As String
' Find all printers installed
For Each InstalledPrinters In _
Next InstalledPrinters
' Set the combo to the first printer in the list
If combobox1.Items.Count > 0 Then
combobox1.SelectedIndex = 0
End If
Now let's learn some basics of preview and print. So let's think on how to preview a document.
First let's make a design to our preview (some limits) we need then to use VB Pen
Take a look, the following code will help you to show a pageview !
This is the whole code to more help you !
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
'This call is required by the Windows Form Designer.
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
End If
End If
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents btnPreview As System.Windows.Forms.Button
Friend WithEvents pdocTest As System.Drawing.Printing.PrintDocument
Friend WithEvents ppdTest As System.Windows.Forms.PrintPreviewDialog
Private Sub InitializeComponent()
Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Form1))
Me.btnPreview = New System.Windows.Forms.Button
Me.pdocTest = New System.Drawing.Printing.PrintDocument
Me.ppdTest = New System.Windows.Forms.PrintPreviewDialog
Me.btnPreview.Location = New System.Drawing.Point(79, 43)
Me.btnPreview.Name = "btnPreview"
Me.btnPreview.TabIndex = 0
Me.btnPreview.Text = "Preview"
Me.ppdTest.AutoScrollMargin = New System.Drawing.Size(0, 0)
Me.ppdTest.AutoScrollMinSize = New System.Drawing.Size(0, 0)
Me.ppdTest.ClientSize = New System.Drawing.Size(400, 300)
Me.ppdTest.Document = Me.pdocTest
Me.ppdTest.Enabled = True
Me.ppdTest.Icon = CType(resources.GetObject("ppdTest.Icon"), System.Drawing.Icon)
Me.ppdTest.Location = New System.Drawing.Point(144, 18)
Me.ppdTest.MinimumSize = New System.Drawing.Size(375, 250)
Me.ppdTest.Name = "ppdTest"
Me.ppdTest.TransparencyKey = System.Drawing.Color.Empty
Me.ppdTest.Visible = False
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(232, 109)
Me.Name = "Form1"
Me.Text = "Form1"
End Sub
#End Region
Private Sub btnPreview_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPreview.Click
' Convert the dialog into a Form.
Dim dlg As Form = DirectCast(ppdTest, Form)
' Set a "restore" size.
dlg.Width = 600
dlg.Height = 400
' Start the dialog maximized.
dlg.WindowState = FormWindowState.Maximized
' Show the dialog.
End Sub
Private Sub pdocTest_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles pdocTest.PrintPage
Dim the_pen As New Pen(Color.Blue, 10)
e.Graphics.DrawRectangle(the_pen, e.MarginBounds)
e.HasMorePages = False
End Sub
End Class

That's quite good for today come some days later coz me only I don't know much and I'm learning ;)


Post a Comment