Print, PrintPreview Dialog and Print Dialog : PrintPreview Dialog « GUI « VB.Net Tutorial

Home
VB.Net Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statements
5.Date Time
6.Class Module
7.Development
8.Collections
9.Generics
10.Attributes
11.Event
12.LINQ
13.Stream File
14.GUI
15.GUI Applications
16.Windows Presentation Foundation
17.2D Graphics
18.I18N Internationlization
19.Reflection
20.Regular Expressions
21.Security
22.Socket Network
23.Thread
24.Windows
25.XML
26.Database ADO.net
27.Design Patterns
VB.Net
VB.Net by API
VB.Net Tutorial » GUI » PrintPreview Dialog 
14.66.2.Print, PrintPreview Dialog and Print Dialog
Print, PrintPreview Dialog and Print Dialog
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
Imports System.Drawing.Printing

public class PrintPreviewDemo
   public Shared Sub Main
        Application.Run(New MainForm)
   End Sub
End class



Public Class MainForm
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        '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 NothingThen
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    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 printButton As System.Windows.Forms.Button
    Friend WithEvents previewButton As System.Windows.Forms.Button
    Friend WithEvents previewControlButton As System.Windows.Forms.Button
    Friend WithEvents pageSetupButton As System.Windows.Forms.Button
    Friend WithEvents pageSetupDialog1 As System.Windows.Forms.PageSetupDialog
    Friend WithEvents printDialog1 As System.Windows.Forms.PrintDialog
    Friend WithEvents printDocument1 As System.Drawing.Printing.PrintDocument
    Friend WithEvents printPreviewDialog1 As System.Windows.Forms.PrintPreviewDialog
    <System.Diagnostics.DebuggerStepThrough()Private Sub InitializeComponent()
        Me.printButton = New System.Windows.Forms.Button()
        Me.previewButton = New System.Windows.Forms.Button()
        Me.previewControlButton = New System.Windows.Forms.Button()
        Me.pageSetupButton = New System.Windows.Forms.Button()
        Me.pageSetupDialog1 = New System.Windows.Forms.PageSetupDialog()
        Me.printDialog1 = New System.Windows.Forms.PrintDialog()
        Me.printDocument1 = New System.Drawing.Printing.PrintDocument()
        Me.printPreviewDialog1 = New System.Windows.Forms.PrintPreviewDialog()
        Me.SuspendLayout()
        '
        'printButton
        '
        Me.printButton.Location = New System.Drawing.Point(2416)
        Me.printButton.Name = "printButton"
        Me.printButton.Size = New System.Drawing.Size(11223)
        Me.printButton.TabIndex = 3
        Me.printButton.Text = "Print"
        '
        'previewButton
        '
        Me.previewButton.Location = New System.Drawing.Point(2480)
        Me.previewButton.Name = "previewButton"
        Me.previewButton.Size = New System.Drawing.Size(11223)
        Me.previewButton.TabIndex = 6
        Me.previewButton.Text = "Preview Dialog"
        '
        'previewControlButton
        '
        Me.previewControlButton.Location = New System.Drawing.Point(2448)
        Me.previewControlButton.Name = "previewControlButton"
        Me.previewControlButton.Size = New System.Drawing.Size(11223)
        Me.previewControlButton.TabIndex = 4
        Me.previewControlButton.Text = "Preview Control"
        '
        'pageSetupButton
        '
        Me.pageSetupButton.Location = New System.Drawing.Point(24112)
        Me.pageSetupButton.Name = "pageSetupButton"
        Me.pageSetupButton.Size = New System.Drawing.Size(11223)
        Me.pageSetupButton.TabIndex = 5
        Me.pageSetupButton.Text = "Page Setup"
        '
        'pageSetupDialog1
        '
        Me.pageSetupDialog1.MinMargins = New System.Drawing.Printing.Margins(50505050)
        Me.pageSetupDialog1.ShowHelp = True
        '
        'printDialog1
        '
        Me.printDialog1.Document = Me.printDocument1
        '
        'printDocument1
        '
        '
        'printPreviewDialog1
        '
        Me.printPreviewDialog1.AutoScrollMargin = New System.Drawing.Size(00)
        Me.printPreviewDialog1.AutoScrollMinSize = New System.Drawing.Size(00)
        Me.printPreviewDialog1.ClientSize = New System.Drawing.Size(400300)
        Me.printPreviewDialog1.Enabled = True
        Me.printPreviewDialog1.Location = New System.Drawing.Point(4458)
        Me.printPreviewDialog1.MaximumSize = New System.Drawing.Size(00)
        Me.printPreviewDialog1.Name = "printPreviewDialog1"
        Me.printPreviewDialog1.Opacity = 1
        Me.printPreviewDialog1.TransparencyKey = System.Drawing.Color.Empty
        Me.printPreviewDialog1.Visible = False
        '
        'MainForm
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(513)
        Me.ClientSize = New System.Drawing.Size(160150)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.printButton, Me.previewButton, Me.previewControlButton, Me.pageSetupButton})
        Me.Name = "MainForm"
        Me.Text = "MainForm"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Dim totalPages As Integer = 13
    Dim page As Integer
    Dim maxPage As Integer
    Dim myFont As Font = Nothing

    Private Sub printButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgsHandles printButton.Click
        printDocument1.DocumentName = "myFile.txt"
        printDocument1.PrinterSettings.FromPage = 1
        printDocument1.PrinterSettings.ToPage = totalPages
        printDocument1.PrinterSettings.MinimumPage = 1
        printDocument1.PrinterSettings.MaximumPage = totalPages
        printDialog1.AllowSomePages = True
        If printDialog1.ShowDialog() = DialogResult.OK Then
            If printDialog1.PrinterSettings.PrintRange = Drawing.Printing.PrintRange.SomePages Then
                page = printDocument1.PrinterSettings.FromPage
                maxPage = printDocument1.PrinterSettings.ToPage
            Else
                page = 1
                maxPage = totalPages
            End If
        End If
        printDocument1.Print()
    End Sub

    Sub printDocument1_PrintPage(ByVal sender As Object, ByVal e As PrintPageEventArgsHandles printDocument1.PrintPage
        Dim As Graphics = e.Graphics
        g.DrawString("Hello, " & vbCrLf & "Printer" & vbCrLf & "Page: " & page, myFont, Brushes.Black, RectangleF.op_Implicit(e.MarginBounds))
        page = page + 1
        e.HasMorePages = (page <= maxPage)
    End Sub


    Private Sub previewControlButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgsHandles previewControlButton.Click
        page = 1
        maxPage = totalPages
        printPreviewDialog1.Document = printDocument1
        printPreviewDialog1.ShowDialog()
    End Sub

    Private Sub previewButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgsHandles previewButton.Click
        page = 1
        maxPage = totalPages
        Dim dlg As CustomPrintPreviewDialog = New CustomPrintPreviewDialog()
        dlg.Document = printDocument1
        dlg.ShowDialog()
    End Sub

    Private Sub pageSetupButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgsHandles pageSetupButton.Click
        pageSetupDialog1.Document = printDocument1
        pageSetupDialog1.ShowDialog()
    End Sub

    Private Sub printDocument1_BeginPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgsHandles printDocument1.BeginPrint
        myFont = New Font("Lucide Console"72)
    End Sub

    Private Sub printDocument1_EndPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgsHandles printDocument1.EndPrint
        myFont.Dispose()
        myFont = Nothing
    End Sub
End Class




Public Class CustomPrintPreviewDialog
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        '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 NothingThen
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    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 printPreviewControl1 As System.Windows.Forms.PrintPreviewControl
    <System.Diagnostics.DebuggerStepThrough()Private Sub InitializeComponent()
        Me.printPreviewControl1 = New System.Windows.Forms.PrintPreviewControl()
        Me.SuspendLayout()
        '
        'printPreviewControl1
        '
        Me.printPreviewControl1.AutoZoom = False
        Me.printPreviewControl1.Dock = System.Windows.Forms.DockStyle.Fill
        Me.printPreviewControl1.Name = "printPreviewControl1"
        Me.printPreviewControl1.Size = New System.Drawing.Size(376398)
        Me.printPreviewControl1.TabIndex = 1
        Me.printPreviewControl1.UseAntiAlias = True
        Me.printPreviewControl1.Zoom = 0.30000001192092896
        '
        'CustomPrintPreviewDialog
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(513)
        Me.ClientSize = New System.Drawing.Size(376398)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.printPreviewControl1})
        Me.Name = "CustomPrintPreviewDialog"
        Me.Text = "CustomPrintPreviewDialog"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub printPreviewControl1_Click(ByVal sender As Object, ByVal e As System.EventArgsHandles printPreviewControl1.Click
        If (Control.ModifierKeys And Keys.ShiftThen
            printPreviewControl1.Zoom = printPreviewControl1.Zoom * 2.0
        Else
            printPreviewControl1.Zoom = printPreviewControl1.Zoom / 2.0
        End If
    End Sub

    Public Property Document() As PrintDocument
        Get
            Return printPreviewControl1.Document
        End Get
        Set(ByVal Value As PrintDocument)
            printPreviewControl1.Document = Value
        End Set
    End Property
End Class
14.66.PrintPreview Dialog
14.66.1.PrintPreview page countPrintPreview page count
14.66.2.Print, PrintPreview Dialog and Print DialogPrint, PrintPreview Dialog and Print Dialog
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.