stu nicholls dot com | menu - Professional dropdown #3

click here


PrintDialog

This enables a user to select a printer and choose other print options such as number of copies and page orientation.

Public Class PrinterSetUpCode
' this will set the setting for Printer and send the print 'to the document titled PageSetup.pdf insted of txt.
'some water mark is stored in the PageSetUp.pdf file
Dim PrintDialog1 As New PrintDialog()
Dim PrintDocument1 As New
System.Drawing.Printing.PrintDocument()
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
PrintDialog1.Document = PrintDocument1
PrintDocument1.DocumentName = "PageSetup.pdf"
PrintDialog1.PrinterSettings.Copies = 5
PrintDialog1.PrinterSettings.FromPage = 1
PrintDialog1.PrinterSettings.ToPage = 5
MessageBox.Show("From Page: " & PrintDialog1.PrinterSettings.FromPage)
MessageBox.Show("To Page: " & PrintDialog1.PrinterSettings.ToPage)
MessageBox.Show("Copies: " & PrintDialog1.PrinterSettings.Copies)
PrintDocument1.Print() ' this will do the printing
End Sub
End Class

Fig15


PrintPreviewDialog: Displays a dialog box that shows the user a preview of associated document look like when printed. ' Place a button on the form (PrintPreviewShow)
' PLace a textbox control on the form (TextData)
'whatever typed in the textbox is displayed in the 'printpreview control
Imports System.Drawing
Imports System.Drawing.Printing
Public Class PrintPreview
Private Doc As New PrintDocument()
Private Sub Print_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)
Dim g As Graphics = e.Graphics
Dim Brush As New SolidBrush(Color.Black)
g.DrawString(Me.TextData.Text, New Font("arial", 9), Brush, 10, 10)
End Sub
Private Sub PrintPreviewShow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintPreviewShow.Click
Dim ppDlg As New PrintPreviewDialog()
ppDlg.Document = Doc
ppDlg.Show()
AddHandler Doc.PrintPage, AddressOf Print_PrintPage
End Sub
End Class

Fig16


Whatever you typed in the text box will be displayed in PrintPreview Control.

Fig17


Fig18


Note: In Fig 11 you can see the Print Preview dialog box has appeared.

PrintPreviewControl: Represents only a portion of Print Preview that shows the document being previewed. It does not contain any dialog boxes or buttons.
‘this program will display the contents of the selected file in ‘the preview box PrintPreviewControl is
added to the form from ‘the tool box and not by code Clicking on the button ‘PrintPreviewDialog box is shown and the contents from the file ‘is shown there but you can change the different parameter of ‘the preview window. Here the preview control is created by code. ‘If u select to print it will store the contents into a file ‘named as Document
Imports System.Drawing.Printing
Imports System.IO
Public Class PrintPreviewFile
Dim FileData As String
Dim Doc As New PrintDocument()
Private Sub PrintPreviewFile_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
PrintPreviewControl1.Document = Doc
PrintPreviewControl1.Zoom = 1.5
Dim sReader As New StreamReader("D:\VB.Net- book\controls.txt")
FileData = sReader.ReadToEnd()
AddHandler Doc.PrintPage, AddressOf Print_PrintPage
End Sub
Private Sub Print_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)
Dim g As Graphics = e.Graphics
Dim Brush As New SolidBrush(Color.Black)
g.DrawString(Me.FileData, New Font("arial", 9), Brush, 10, 10)
End Sub

Private Sub PrintPreviewShow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintPreviewShow.Click
Dim ppDlg As New PrintPreviewDialog()
ppDlg.Document = Doc
ppDlg.Show()
AddHandler Doc.PrintPage, AddressOf Print_PrintPage
End Sub
End Class
Fig19


Fig20


PrintDocument: Defines an object that sends output to printer.

<< Previous   Next>>


comments powered by Disqus
click here
Footer1