PrintDocument.PrinterSettings : PrintDocument « System.Drawing.Printing « VB.Net by API

Home
VB.Net by API
1.Microsoft.VisualBasic
2.Microsoft.Win32
3.System
4.System.Collections
5.System.Collections.Generic
6.System.Collections.Specialized
7.System.ComponentModel
8.System.Configuration.ConfigurationSettings
9.System.Data
10.System.Data.Odbc
11.System.Data.OleDb
12.System.Data.OracleClient
13.System.Data.SqlClient
14.System.Diagnostics
15.System.Drawing
16.System.Drawing.Drawing2D
17.System.Drawing.Imaging
18.System.Drawing.Printing
19.System.Drawing.Text
20.System.Globalization
21.System.IO
22.System.IO.IsolatedStorage
23.System.IO.Ports
24.System.Media
25.System.Messaging
26.System.Net
27.System.Net.Sockets
28.System.Reflection
29.System.Runtime.InteropServices
30.System.Runtime.Remoting.Channels
31.System.Runtime.Serialization
32.System.Runtime.Serialization.Formatters.Binary
33.System.Runtime.Serialization.Formatters.Soap
34.System.Security.Cryptography
35.System.Security.Cryptography.X509Certificates
36.System.Security.Permissions
37.System.Security.Principal
38.System.ServiceProcess
39.System.Text
40.System.Text.RegularExpressions
41.System.Threading
42.System.Web
43.System.Web.Mail
44.System.Windows.Forms
45.System.Xml
46.System.Xml.Schema
47.System.Xml.Serialization
48.System.Xml.Xsl
VB.Net by API » System.Drawing.Printing » PrintDocument 
PrintDocument.PrinterSettings
  

Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Windows.Forms
Imports System.Data
Imports System.Configuration
Imports System.Resources
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.IO
Imports System.Drawing.Printing


Public Class MainClass
    Shared Sub Main()
        Dim myform As Form = New Dialogs()
        Application.Run(myform)
    End Sub

End Class

Public Class Dialogs

    'Declare variable
    Private strFileName As String
    Private objStreamToPrint As StreamReader
    Private objPrintFont As Font

    Private Sub btnOpen_Click(ByVal sender As Object, _
        ByVal e As System.EventArgsHandles btnOpen.Click

        'Set the Open dialog properties
        With OpenFileDialog1
            .Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"
            .FilterIndex = 1
            .Title = "Demo Open File Dialog"
        End With

        'Show the Open dialog and if the user clicks the Open button,
        'load the file
        If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            Dim allText As String
            Try
                'Save the file name
                strFileName = OpenFileDialog1.FileName
                'Read the contents of the file
                allText = My.Computer.FileSystem.ReadAllText(strFileName)
                'Display the file contents in the TextBox
                txtFile.Text = allText
            Catch fileException As Exception
                Throw fileException
            End Try
        End If
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgsHandles btnSave.Click

        'Set the Save dialog properties
        With SaveFileDialog1
            .DefaultExt = "txt"
            .FileName = strFileName
            .Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"
            .FilterIndex = 1
            .OverwritePrompt = True
            .Title = "Demo Save File Dialog"
        End With

        'Show the Save dialog and if the user clicks the Save button,
        'save the file
        If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            Try
                'Save the file name
                strFileName = SaveFileDialog1.FileName
                Dim filePath As String
                'Open or Create the file
                filePath = System.IO.Path.Combine_
                    My.Computer.FileSystem.SpecialDirectories.MyDocuments, _
                    strFileName)
                'Replace the contents of the file
                My.Computer.FileSystem.WriteAllText(filePath, txtFile.Text, False)
            Catch fileException As Exception
                Throw fileException
            End Try
        End If
    End Sub

    Private Sub btnFont_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgsHandles btnFont.Click

        'Set the FontDialog control properties
        FontDialog1.ShowColor = True

        'Show the Font dialog
        If FontDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            'If the OK button was clicked set the font
            'in the text box on the form
            txtFile.Font = FontDialog1.Font
            'Set the color of the font in the text box on the form
            txtFile.ForeColor = FontDialog1.Color
        End If
    End Sub

    Private Sub btnColor_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgsHandles btnColor.Click

        'Show the Color dialog
        If ColorDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            'Set the BackColor property of the form
            Me.BackColor = ColorDialog1.Color
        End If
    End Sub

    Private Sub btnPrint_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgsHandles btnPrint.Click

        'Declare an object for the PrintDocument class
        Dim objPrintDocument As PrintDocument = New PrintDocument()

        'Set the DocumentName property
        objPrintDocument.DocumentName = "Text File Print Demo"

        PrintDialog1.AllowPrintToFile = False
        PrintDialog1.AllowSelection = False
        PrintDialog1.AllowSomePages = False

        PrintDialog1.Document = objPrintDocument

        If PrintDialog1.ShowDialog() = DialogResult.OK Then
            objStreamToPrint = New StreamReader(strFileName)

            objPrintFont = New Font("Arial"10)

            AddHandler objPrintDocument.PrintPage, _
                AddressOf objPrintDocument_PrintPage

            objPrintDocument.PrinterSettings = PrintDialog1.PrinterSettings

            objPrintDocument.Print()

            objStreamToPrint.Close()
            objStreamToPrint = Nothing
        End If
    End Sub

    Private Sub objPrintDocument_PrintPage(ByVal sender As Object, _
        ByVal e As System.Drawing.Printing.PrintPageEventArgs)

        Dim sngLinesPerpage As Single = 0
        Dim sngVerticalPosition As Single = 0
        Dim intLineCount As Integer = 0
        Dim sngLeftMargin As Single = e.MarginBounds.Left
        Dim sngTopMargin As Single = e.MarginBounds.Top
        Dim strLine As String

        sngLinesPerpage = _
           e.MarginBounds.Height / objPrintFont.GetHeight(e.Graphics)

        strLine = objStreamToPrint.ReadLine()

        While (intLineCount < sngLinesPerpage And Not (strLine Is Nothing))

            sngVerticalPosition = sngTopMargin + _
                (intLineCount * objPrintFont.GetHeight(e.Graphics))

            e.Graphics.DrawString(strLine, objPrintFont, Brushes.Black, _
                sngLeftMargin, sngVerticalPosition, New StringFormat())
            intLineCount = intLineCount + 1

            If (intLineCount < sngLinesPerpageThen
                strLine = objStreamToPrint.ReadLine()
            End If

        End While

        If (strLine <> NothingThen
            e.HasMorePages = True
        Else
            e.HasMorePages = False
        End If
    End Sub

    Private Sub btnBrowse_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgsHandles btnBrowse.Click

        FolderBrowserDialog1.Description = "Select a folder for your backups:"
        FolderBrowserDialog1.RootFolder = Environment.SpecialFolder.MyComputer
        FolderBrowserDialog1.ShowNewFolderButton = False

        If FolderBrowserDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            'Display the selected folder
            txtFile.Text = FolderBrowserDialog1.SelectedPath
        End If
    End Sub
End Class



<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Public Class Dialogs
    Inherits System.Windows.Forms.Form

    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing AndAlso components IsNot Nothing Then
            components.Dispose()
        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.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.txtFile = New System.Windows.Forms.TextBox
        Me.btnOpen = New System.Windows.Forms.Button
        Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog
        Me.btnSave = New System.Windows.Forms.Button
        Me.SaveFileDialog1 = New System.Windows.Forms.SaveFileDialog
        Me.btnFont = New System.Windows.Forms.Button
        Me.FontDialog1 = New System.Windows.Forms.FontDialog
        Me.btnColor = New System.Windows.Forms.Button
        Me.ColorDialog1 = New System.Windows.Forms.ColorDialog
        Me.btnPrint = New System.Windows.Forms.Button
        Me.PrintDialog1 = New System.Windows.Forms.PrintDialog
        Me.btnBrowse = New System.Windows.Forms.Button
        Me.FolderBrowserDialog1 = New System.Windows.Forms.FolderBrowserDialog
        Me.SuspendLayout()
        '
        'txtFile
        '
        Me.txtFile.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom_
                    Or System.Windows.Forms.AnchorStyles.Left_
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.txtFile.Location = New System.Drawing.Point(88)
        Me.txtFile.Multiline = True
        Me.txtFile.Name = "txtFile"
        Me.txtFile.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
        Me.txtFile.Size = New System.Drawing.Size(352264)
        Me.txtFile.TabIndex = 0
        '
        'btnOpen
        '
        Me.btnOpen.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.btnOpen.Location = New System.Drawing.Point(3678)
        Me.btnOpen.Name = "btnOpen"
        Me.btnOpen.Size = New System.Drawing.Size(7523)
        Me.btnOpen.TabIndex = 1
        Me.btnOpen.Text = "Open"
        '
        'OpenFileDialog1
        '
        Me.OpenFileDialog1.FileName = "OpenFileDialog1"
        '
        'btnSave
        '
        Me.btnSave.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.btnSave.Location = New System.Drawing.Point(36738)
        Me.btnSave.Name = "btnSave"
        Me.btnSave.Size = New System.Drawing.Size(7523)
        Me.btnSave.TabIndex = 2
        Me.btnSave.Text = "Save"
        '
        'btnFont
        '
        Me.btnFont.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.btnFont.Location = New System.Drawing.Point(36768)
        Me.btnFont.Name = "btnFont"
        Me.btnFont.Size = New System.Drawing.Size(7523)
        Me.btnFont.TabIndex = 3
        Me.btnFont.Text = "Font"
        '
        'btnColor
        '
        Me.btnColor.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.btnColor.Location = New System.Drawing.Point(36798)
        Me.btnColor.Name = "btnColor"
        Me.btnColor.Size = New System.Drawing.Size(7523)
        Me.btnColor.TabIndex = 4
        Me.btnColor.Text = "Color"
        '
        'btnPrint
        '
        Me.btnPrint.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.btnPrint.Location = New System.Drawing.Point(367128)
        Me.btnPrint.Name = "btnPrint"
        Me.btnPrint.Size = New System.Drawing.Size(7523)
        Me.btnPrint.TabIndex = 5
        Me.btnPrint.Text = "Print"
        '
        'btnBrowse
        '
        Me.btnBrowse.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.btnBrowse.Location = New System.Drawing.Point(367158)
        Me.btnBrowse.Name = "btnBrowse"
        Me.btnBrowse.Size = New System.Drawing.Size(7523)
        Me.btnBrowse.TabIndex = 6
        Me.btnBrowse.Text = "Browse"
        '
        'FolderBrowserDialog1
        '
        Me.FolderBrowserDialog1.SelectedPath = "FolderBrowserDialog1"
        '
        'Dialogs
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(448277)
        Me.Controls.Add(Me.btnBrowse)
        Me.Controls.Add(Me.btnPrint)
        Me.Controls.Add(Me.btnColor)
        Me.Controls.Add(Me.btnFont)
        Me.Controls.Add(Me.btnSave)
        Me.Controls.Add(Me.btnOpen)
        Me.Controls.Add(Me.txtFile)
        Me.Name = "Dialogs"
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
        Me.Text = "Dialogs"
        Me.ResumeLayout(False)
        Me.PerformLayout()

    End Sub
    Friend WithEvents txtFile As System.Windows.Forms.TextBox
    Friend WithEvents btnOpen As System.Windows.Forms.Button
    Friend WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog
    Friend WithEvents btnSave As System.Windows.Forms.Button
    Friend WithEvents SaveFileDialog1 As System.Windows.Forms.SaveFileDialog
    Friend WithEvents btnFont As System.Windows.Forms.Button
    Friend WithEvents FontDialog1 As System.Windows.Forms.FontDialog
    Friend WithEvents btnColor As System.Windows.Forms.Button
    Friend WithEvents ColorDialog1 As System.Windows.Forms.ColorDialog
    Friend WithEvents btnPrint As System.Windows.Forms.Button
    Friend WithEvents PrintDialog1 As System.Windows.Forms.PrintDialog
    Friend WithEvents btnBrowse As System.Windows.Forms.Button
    Friend WithEvents FolderBrowserDialog1 As System.Windows.Forms.FolderBrowserDialog

End Class

   
    
  
Related examples in the same category
1.PrintDocument.PrintPage
2.PrintDocument.PrinterSettings.Copies
3.PrintDocument.PrinterSettings.FromPage
4.PrintDocument.PrinterSettings.PrintRange
5.PrintDocument.PrinterSettings.PrinterName
6.PrintDocument.PrinterSettings.ToPage
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.