Imports System.IO
Imports System.Drawing.Printing

Public Class Form1
    Inherits System.Windows.Forms.Form

    ' Declare variable...
    Private strFileName As String = "C:\Temp\Text Document.txt"
    Private objStreamToPrint As StreamReader
    Private objPrintFont As Font

#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 Nothing) Then
                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 txtFile As System.Windows.Forms.TextBox
    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 btnOpen 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 OpenFileDialog1 As System.Windows.Forms.OpenFileDialog
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.btnOpen = New System.Windows.Forms.Button()
        Me.txtFile = New System.Windows.Forms.TextBox()
        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.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog()
        Me.SuspendLayout()
        '
        'btnOpen
        '
        Me.btnOpen.Anchor = (System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right)
        Me.btnOpen.Location = New System.Drawing.Point(368, 8)
        Me.btnOpen.Name = "btnOpen"
        Me.btnOpen.TabIndex = 0
        Me.btnOpen.Text = "Open"
        '
        'txtFile
        '
        Me.txtFile.Anchor = (((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                    Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right)
        Me.txtFile.Location = New System.Drawing.Point(8, 8)
        Me.txtFile.Multiline = True
        Me.txtFile.Name = "txtFile"
        Me.txtFile.Size = New System.Drawing.Size(352, 264)
        Me.txtFile.TabIndex = 1
        Me.txtFile.Text = ""
        '
        'btnSave
        '
        Me.btnSave.Anchor = (System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right)
        Me.btnSave.Location = New System.Drawing.Point(368, 40)
        Me.btnSave.Name = "btnSave"
        Me.btnSave.TabIndex = 2
        Me.btnSave.Text = "Save"
        '
        'SaveFileDialog1
        '
        Me.SaveFileDialog1.FileName = "doc1"
        '
        'btnFont
        '
        Me.btnFont.Anchor = (System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right)
        Me.btnFont.Location = New System.Drawing.Point(368, 72)
        Me.btnFont.Name = "btnFont"
        Me.btnFont.TabIndex = 3
        Me.btnFont.Text = "Font"
        '
        'btnColor
        '
        Me.btnColor.Anchor = (System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right)
        Me.btnColor.Location = New System.Drawing.Point(368, 104)
        Me.btnColor.Name = "btnColor"
        Me.btnColor.TabIndex = 4
        Me.btnColor.Text = "Color"
        '
        'btnPrint
        '
        Me.btnPrint.Anchor = (System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right)
        Me.btnPrint.Location = New System.Drawing.Point(368, 136)
        Me.btnPrint.Name = "btnPrint"
        Me.btnPrint.TabIndex = 5
        Me.btnPrint.Text = "Print"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(448, 277)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnPrint, Me.btnColor, Me.btnFont, Me.btnSave, Me.txtFile, Me.btnOpen})
        Me.Name = "Form1"
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
        Me.Text = "Dialogs"
        Me.ResumeLayout(False)

    End Sub

#End Region


    Public Sub btnOpen_Click(ByVal sender As System.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
            .InitialDirectory = "C:\Temp\"
            .Title = "Demo Open File Dialog"
        End With

        ' Show the Open dialog and if the user clicks the OK button,
        ' load the file...
        If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
            strFileName = OpenFileDialog1.FileName
            Dim objReader As StreamReader = New StreamReader(strFileName)
            txtFile.Text = objReader.ReadToEnd()
            objReader.Close()
            objReader = Nothing
        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
            .InitialDirectory = "C:\Temp\"
            .OverwritePrompt = True
            .Title = "Demo Save File Dialog"
        End With

        ' Show the Save dialog and if the user clicks the Save button,
        ' load the file...
        If SaveFileDialog1.ShowDialog() = DialogResult.OK Then
            strFileName = SaveFileDialog1.FileName
            Dim objWriter As StreamWriter = _
                New StreamWriter(strFileName, False)
            objWriter.Write(txtFile.Text)
            objWriter.Close()
            objWriter = Nothing
        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() = 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() = DialogResult.OK Then
            ' Set the ForeColor property of all the controls on the form...
            btnOpen.ForeColor = ColorDialog1.Color
            btnSave.ForeColor = ColorDialog1.Color
            btnFont.ForeColor = ColorDialog1.Color
            btnColor.ForeColor = 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"

        ' Set the PrintDialog properties...
        PrintDialog1.AllowPrintToFile = False
        PrintDialog1.AllowSelection = False
        PrintDialog1.AllowSomePages = False

        ' Set the Document property to the objPrintDocument object...
        PrintDialog1.Document = objPrintDocument

        ' Show the Print dialog...
        If PrintDialog1.ShowDialog() = DialogResult.OK Then
            ' If the user clicked on the OK button then set the 
            ' StreamReader object to the file name in the strFileName
            ' variable...
            objStreamToPrint = New StreamReader(strFileName)

            ' Set the print font...
            objPrintFont = New Font("Arial", 10)

            ' Add an event handler for the PrintPage event of the
            'objPrintDocument object...
            AddHandler objPrintDocument.PrintPage, AddressOf objPrintDocument_PrintPage

            ' Set the PrinterSettings property of the objPrintDocument
            ' object to the PrinterSettings property returned from the
            ' PrintDialog control...
            objPrintDocument.PrinterSettings = PrintDialog1.PrinterSettings

            ' Print the text file...
            objPrintDocument.Print()

            ' Clean up...
            objStreamToPrint.Close()
            objStreamToPrint = Nothing
        End If

    End Sub


    Private Sub objPrintDocument_PrintPage(ByVal sender As Object, _
                ByVal e As System.Drawing.Printing.PrintPageEventArgs)
        ' Declare variables...
        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

        ' Work out the number of lines per page.
        ' Use the MarginBounds on the event to do this...
        sngLinesPerpage = _
           e.MarginBounds.Height / objPrintFont.GetHeight(e.Graphics)

        ' Now iterate through the file printing out each line.
        ' This assumes that a single line is not wider than the page
        ' width. Check intLineCount first so that we don't read a line
        ' that we won't print...
        strLine = objStreamToPrint.ReadLine()

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

            ' Calculate the vertical position on the page...
            sngVerticalPosition = sngTopMargin + _
                (intLineCount * objPrintFont.GetHeight(e.Graphics))

            ' Pass a StringFormat to DrawString for the
            ' Print Preview control...
            e.Graphics.DrawString(strLine, objPrintFont, Brushes.Black, _
                sngLeftMargin, sngVerticalPosition, New StringFormat())

            ' Increment the line count...
            intLineCount = intLineCount + 1

            ' If the line count is less than the lines per page then
            ' read another line of text...
            If (intLineCount < sngLinesPerpage) Then
                strLine = objStreamToPrint.ReadLine()
            End If

        End While

        ' If we have more lines then print another page...
        If (strLine <> Nothing) Then
            e.HasMorePages = True
        Else
            e.HasMorePages = False
        End If

    End Sub



End Class
