Visual Basic (NOT VBA for Excel): The code below prints 1 page of data from a te
ID: 3868936 • Letter: V
Question
Visual Basic (NOT VBA for Excel): The code below prints 1 page of data from a text file and stops without printing the rest of the data in the file. (The text file contents are included at the end of the code.) There are enough records in the file to print a complete page 1 and start page 2, but page 2 never starts. How do I get this routine to print more than one page?
Private Sub printCustomerReport_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles printCustomerReport.PrintPage
Dim count As Integer = 0
Dim intVertPos As Integer
e.Graphics.DrawString("Customer Account Report", New Font("Courier New", 12, FontStyle.Bold), Brushes.Black, 200, 10)
' ID the file to open.
txtfileName = "Records.txt"
' Open the file to read the records.
txtFile = File.OpenText(txtfileName)
' Use the setData function to place the file data into the text boxes
setData()
searchFile = File.OpenText(txtfileName)
intVertPos = 50
Try
' Read the file to end of file.
' Print the contents to the PrintDocument.
While Not searchFile.EndOfStream
If count = 10 Then
count = 0
intVertPos += 20
Else
e.Graphics.DrawString(String.Format("{0,20}{1,10}", "Last Name:",
searchFile.ReadLine()),
New Font("Courier New", 12, FontStyle.Regular),
Brushes.Black, 15, intVertPos)
intVertPos += 15
count += 1
e.Graphics.DrawString(String.Format("{0,20}{1,10}", "First Name:",
searchFile.ReadLine()),
New Font("Courier New", 12, FontStyle.Regular),
Brushes.Black, 15, intVertPos)
intVertPos += 15
count += 1
e.Graphics.DrawString(String.Format("{0,20}{1,10}", "Customer Number:",
searchFile.ReadLine()),
New Font("Courier New", 12, FontStyle.Regular),
Brushes.Black, 15, intVertPos)
intVertPos += 15
count += 1
e.Graphics.DrawString(String.Format("{0,20}{1,10}", "Address:",
searchFile.ReadLine()),
New Font("Courier New", 12, FontStyle.Regular),
Brushes.Black, 15, intVertPos)
intVertPos += 15
count += 1
e.Graphics.DrawString(String.Format("{0,20}{1,10}", "City:",
searchFile.ReadLine()),
New Font("Courier New", 12, FontStyle.Regular),
Brushes.Black, 15, intVertPos)
intVertPos += 15
count += 1
e.Graphics.DrawString(String.Format("{0,20}{1,10}", "State:",
searchFile.ReadLine()),
New Font("Courier New", 12, FontStyle.Regular),
Brushes.Black, 15, intVertPos)
intVertPos += 15
count += 1
e.Graphics.DrawString(String.Format("{0,20}{1,10}", "ZIP Code:",
searchFile.ReadLine()),
New Font("Courier New", 12, FontStyle.Regular),
Brushes.Black, 15, intVertPos)
intVertPos += 15
count += 1
e.Graphics.DrawString(String.Format("{0,20}{1,10}", "Telephone Number:",
searchFile.ReadLine()),
New Font("Courier New", 12, FontStyle.Regular),
Brushes.Black, 15, intVertPos)
intVertPos += 15
count += 1
e.Graphics.DrawString(String.Format("{0,20}{1,10}", "Account Balance:",
searchFile.ReadLine()),
New Font("Courier New", 12, FontStyle.Regular),
Brushes.Black, 15, intVertPos)
intVertPos += 15
count += 1
e.Graphics.DrawString(String.Format("{0,20}{1,10}", "Date of Last Payment:",
searchFile.ReadLine()),
New Font("Courier New", 12, FontStyle.Regular),
Brushes.Black, 15, intVertPos)
intVertPos += 15
count += 1
End If
End While
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Records.txt
Syker
Mike
1
1258 Wilder Ave.
Lima
OH
44836
456-789-2291
500.00
5/30/2017
Glee
Grace
2
12334 Main St.
Lima
OH
44836
456-123-7458
23.00
5/2/2017
Smith
Mark
3
23 Sky Drive
Lima
OH
44836
456-789-6588
58.00
5/2/2017
Martin
Mary
4
789 Cross St.
Lima
OH
44836
789-369-5544
500.00
5/4/2017
Jones
Jim
5
12 Kline Dr.
Lima
OH
44839
456-987-6636
98.89
5/8/2017
Deen
Jon
6
54 Lemon Lane
PHX
AZ
85693
480-851-8803
.37
7/29/2017
Nofts
Paul
7
123 Wilder Ave.
Lima
OH
44836
409-433-8562
56.00
5/3/2017
Wargo
Paul
8
1236 Wilder Ave.
Lima
OH
44836
456-789-3625
500.00
6/8/2017
Thanks for the help!
Explanation / Answer
Private Sub PrintDocument1_BeginPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles PrintDocument1.BeginPrint
'--- Start printing at the start of the string
mStringPos = 0
End Sub
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
'--- Setup graphics context
Dim fnt As New Font("Arial", 10)
e.Graphics.TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAlias
'--- Calculate the number of characters that fit inside the margins
Dim area As SizeF = New SizeF(e.MarginBounds.Width, e.MarginBounds.Height)
Dim lines, chars As Integer
e.Graphics.MeasureString(PrintText.Substring(mStringPos), fnt, area, _
StringFormat.GenericTypographic, chars, lines)
'--- Print the amount of text that fits inside the margins
Dim rc As New RectangleF(e.MarginBounds.Left, e.MarginBounds.Top, e.MarginBounds.Width, e.MarginBounds.Height)
e.Graphics.DrawString(PrintText.Substring(mStringPos, chars), fnt, _
Brushes.Black, rc, StringFormat.GenericTypographic)
'--- Advance print position by <chars>
mStringPos += chars
'--- We'll need another page if not all characters got printed
e.HasMorePages = mStringPos < PrintText.Length
'--- Cleanup
fnt.Dispose()
End Sub
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.