Mega Code Archive

 
Categories / VB.Net by API / System Windows Forms
 

PrintDialog AllowSelection

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.EventArgs) Handles 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.EventArgs) Handles 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.EventArgs) Handles 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.EventArgs) Handles 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.EventArgs) Handles 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 < sngLinesPerpage) Then                 strLine = objStreamToPrint.ReadLine()             End If         End While         If (strLine <> Nothing) Then             e.HasMorePages = True         Else             e.HasMorePages = False         End If     End Sub     Private Sub btnBrowse_Click(ByVal sender As System.Object, _         ByVal e As System.EventArgs) Handles 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(8, 8)         Me.txtFile.Multiline = True         Me.txtFile.Name = "txtFile"         Me.txtFile.ScrollBars = System.Windows.Forms.ScrollBars.Vertical         Me.txtFile.Size = New System.Drawing.Size(352, 264)         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(367, 8)         Me.btnOpen.Name = "btnOpen"         Me.btnOpen.Size = New System.Drawing.Size(75, 23)         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(367, 38)         Me.btnSave.Name = "btnSave"         Me.btnSave.Size = New System.Drawing.Size(75, 23)         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(367, 68)         Me.btnFont.Name = "btnFont"         Me.btnFont.Size = New System.Drawing.Size(75, 23)         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(367, 98)         Me.btnColor.Name = "btnColor"         Me.btnColor.Size = New System.Drawing.Size(75, 23)         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(367, 128)         Me.btnPrint.Name = "btnPrint"         Me.btnPrint.Size = New System.Drawing.Size(75, 23)         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(367, 158)         Me.btnBrowse.Name = "btnBrowse"         Me.btnBrowse.Size = New System.Drawing.Size(75, 23)         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(448, 277)         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