Imports System.IO
Public Class Report
'File Name
Private Const strFILENAME As String = "CustomersReport.txt"
'Create structure and variables
Public Structure CustomerInfo
Dim strNumber As String 'Customer number
Dim strLast As String 'Last name
Dim strFirst As String 'first name
Dim strAddress As String 'Address
Dim strCity As String 'city
Dim strState As String 'state
Dim strZip As String 'zip
Dim strPhone As String 'phone number
Dim decBalance As Decimal 'account balance
Dim dtmPayment As Date 'date of last payment
End Structure
Private Sub mnuFileExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFileExit.Click
'Close Form
Me.Close()
End Sub
Private Sub Report_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim readFile As StreamReader 'object variable
Dim readRecord As CustomerInfo 'structure variable
Dim strHeaderFormat As String = "{0,150}"
Dim strDateHeaderFormat As String = "{0,240}"
Dim strCurrent As String = String.Format(strDateHeaderFormat, Now)
Dim strFormat As String = "{0,-10} {1,25} {2,25} {3,30} {4,25} {5,15} {6,10} {7,20} {8,15} {9,20}"
Dim strFirstFormat As String = "{0,-10} {1,25} {2,25} {3,30} {4,25} {5,15} {6,10} {7,20}"
Dim strLabels As String = String.Format(strFormat, "Customer ID", "Last Name", "First Name", "Address", "City", "State", "ZIP", "Telephone", "Balance", "Last Payment")
Dim strHeading As String = String.Format(strHeaderFormat, "Customer Account Report")
Dim strRecord As String = String.Format(strFirstFormat, readRecord.strNumber, readRecord.strLast, readRecord.strFirst, readRecord.strAddress, readRecord.strCity, readRecord.strState, readRecord.strZip, readRecord.strPhone)
'type the report header
lstReport.Items.Add(strHeading)
'Current date and time
lstReport.Items.Add(strCurrent)
'write blank line
lstReport.Items.Add("")
'Write labels
lstReport.Items.Add(strLabels)
'open text file to read
readFile = File.OpenText("CustomersReport.txt")
'read through each record
Try
Do Until readFile.EndOfStream
readRecord.strNumber = readFile.ReadLine()
readRecord.strLast = readFile.ReadLine()
readRecord.strFirst = readFile.ReadLine()
readRecord.strAddress = readFile.ReadLine()
readRecord.strCity = readFile.ReadLine()
readRecord.strState = readFile.ReadLine()
readRecord.strZip = readFile.ReadLine()
readRecord.strPhone = readFile.ReadLine()
'write the record
lstReport.Items.Add(strRecord)
Loop
Catch ex As Exception
MessageBox.Show("Unable to generate report.")
End Try
End Sub
Private Sub mnuHelpAbout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuHelpAbout.Click
'Message box about this form
MessageBox.Show("Displays report showing all customers and their data.")
End Sub
Private Sub pdPrint_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles pdPrint.PrintPage
Dim intYPosition As Integer = 40
For Each customer As String In lstReport.Items
e.Graphics.DrawString(customer, New Font("Courier New", 12, FontStyle.Regular), Brushes.Black, 10, intYPosition)
intYPosition += 40
Next
End Sub
Private Sub mnuFilePrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFilePrint.Click
'Print the report
pdPrint.Print()
End Sub
End Class