File-Based Applications: |
|
The .NET Framework has a great deal of support for file processing. It does this through various stream-based classes of the System.IO namespace. For example, the FileInfo class provides information and operations on files. Serialization can be performed using any list-based class. Besides file processing, the .NET Framework also supports printing, which is done through the Graphics class. Printing in Microsoft Windows has traditionally been difficult. To do this, you had to involve drawing, hooks, and callback functions, etc. In the .NET Framework, a great deal of the job was simplified by creating simple-to-use classes. In fact, many aspects were also made clearer such as what class does what and when. We reviewed those classes in Print or Page Setup. We will use the Print and Graphics classes here to see how to print.
Besides saving or opening files, another operation users perform on a document consists of printing it. Printing is the ability to render, on paper, the result of a control's content or the contents of various controls. This is performed using an external device called a printer peripheral or simply a printer. To do this, users need access to a printer device. There are two main ways users print a document. They can ask the application they are using to send the document directly to a printer or they can use a dialog box to decide how the printing should be done. |
Practical Learning: Introducing Printing |
|
Private Sub btnCalculateOrder_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles btnCalculateOrder.Click Dim part1UnitPrice As Double Dim part1SubTotal As Double Dim part2UnitPrice As Double Dim part2SubTotal As Double Dim part3UnitPrice As Double Dim part3SubTotal As Double Dim part4UnitPrice As Double Dim part4SubTotal As Double Dim part5UnitPrice As Double Dim part5SubTotal As Double Dim totalParts As Double Dim part1Quantity As Integer Dim part2Quantity As Integer Dim part3Quantity As Integer Dim part4Quantity As Integer Dim part5Quantity As Integer Dim job1Price As Double Dim job2Price As Double Dim job3Price As Double Dim job4Price As Double Dim job5Price As Double Dim totalLabor As Double Dim taxRate As Double Dim taxAmount As Double Dim totalOrder As Double ' Don't charge a part unless it is clearly identified If Me.txtPartName1.Text = "" Then Me.txtUnitPrice1.Text = "0.00" Me.txtQuantity1.Text = "0" Me.txtSubTotal1.Text = "0.00" part1UnitPrice = 0.0 Else Try part1UnitPrice = CDbl(Me.txtUnitPrice1.Text) Catch exc As FormatException MessageBox.Show("Invalid Unit Price") Me.txtUnitPrice1.Text = "0.00" Me.txtUnitPrice1.Focus() End Try Try part1Quantity = CInt(Me.txtQuantity1.Text) Catch exc As FormatException MessageBox.Show("Invalid Quantity") Me.txtQuantity1.Text = "0" Me.txtQuantity1.Focus() End Try End If If Me.txtPartName2.Text = "" Then Me.txtUnitPrice2.Text = "0.00" Me.txtQuantity2.Text = "0" Me.txtSubTotal2.Text = "0.00" part2UnitPrice = 0.0 Else Try part2UnitPrice = CDbl(Me.txtUnitPrice2.Text) Catch exc As FormatException MessageBox.Show("Invalid Unit Price") Me.txtUnitPrice2.Text = "0.00" Me.txtUnitPrice2.Focus() End Try Try part2Quantity = CInt(Me.txtQuantity2.Text) Catch exc As FormatException MessageBox.Show("Invalid Quantity") Me.txtQuantity2.Text = "0" Me.txtQuantity2.Focus() End Try End If If Me.txtPartName3.Text = "" Then Me.txtUnitPrice3.Text = "0.00" Me.txtQuantity3.Text = "0" Me.txtSubTotal3.Text = "0.00" part3UnitPrice = 0.0 Else Try part3UnitPrice = CDbl(Me.txtUnitPrice3.Text) Catch exc As FormatException MessageBox.Show("Invalid Unit Price") Me.txtUnitPrice3.Text = "0.00" Me.txtUnitPrice3.Focus() End Try Try part3Quantity = CInt(Me.txtQuantity3.Text) Catch exc As FormatException MessageBox.Show("Invalid Quantity") Me.txtQuantity3.Text = "0" Me.txtQuantity3.Focus() End Try End If If Me.txtPartName4.Text = "" Then Me.txtUnitPrice4.Text = "0.00" Me.txtQuantity4.Text = "0" Me.txtSubTotal4.Text = "0.00" part4UnitPrice = 0.0 Else Try part4UnitPrice = CDbl(Me.txtUnitPrice4.Text) Catch exc As FormatException MessageBox.Show("Invalid Unit Price") Me.txtUnitPrice4.Text = "0.00" Me.txtUnitPrice4.Focus() End Try Try part4Quantity = CInt(Me.txtQuantity4.Text) Catch exc As FormatException MessageBox.Show("Invalid Quantity") Me.txtQuantity4.Text = "0" Me.txtQuantity4.Focus() End Try End If If Me.txtPartName5.Text = "" Then Me.txtUnitPrice5.Text = "0.00" Me.txtQuantity5.Text = "0" Me.txtSubTotal5.Text = "0.00" part5UnitPrice = 0.0 Else Try part5UnitPrice = CDbl(Me.txtUnitPrice5.Text) Catch exc As FormatException MessageBox.Show("Invalid Unit Price") Me.txtUnitPrice5.Text = "0.00" Me.txtUnitPrice5.Focus() End Try Try part5Quantity = CInt(Me.txtQuantity5.Text) Catch exc As FormatException MessageBox.Show("Invalid Quantity") Me.txtQuantity5.Text = "0" Me.txtQuantity5.Focus() End Try End If ' Don't bill the customer for a job that is not specified If Me.txtJobPerformed1.Text = "" Then Me.txtJobPrice1.Text = "0.00" job1Price = 0.0 Else Try job1Price = CDbl(Me.txtJobPrice1.Text) Catch exc As FormatException MessageBox.Show("Invalid Job Price") Me.txtJobPrice1.Text = "0.00" Me.txtJobPrice1.Focus() End Try End If If Me.txtJobPerformed2.Text = "" Then Me.txtJobPrice2.Text = "0.00" job2Price = 0.0 Else Try job2Price = CDbl(Me.txtJobPrice2.Text) Catch exc As FormatException MessageBox.Show("Invalid Job Price") Me.txtJobPrice2.Text = "0.00" Me.txtJobPrice2.Focus() End Try End If If Me.txtJobPerformed3.Text = "" Then Me.txtJobPrice3.Text = "0.00" job3Price = 0.0 Else Try job3Price = CDbl(Me.txtJobPrice3.Text) Catch exc As FormatException MessageBox.Show("Invalid Job Price") Me.txtJobPrice3.Text = "0.00" Me.txtJobPrice3.Focus() End Try End If If Me.txtJobPerformed4.Text = "" Then Me.txtJobPrice4.Text = "0.00" job4Price = 0.0 Else Try job4Price = CDbl(Me.txtJobPrice4.Text) Catch exc As FormatException MessageBox.Show("Invalid Job Price") Me.txtJobPrice4.Text = "0.00" Me.txtJobPrice4.Focus() End Try End If If Me.txtJobPerformed5.Text = "" Then Me.txtJobPrice5.Text = "0.00" job5Price = 0.0 Else Try job5Price = CDbl(Me.txtJobPrice5.Text) Catch exc As FormatException MessageBox.Show("Invalid Job Price") Me.txtJobPrice5.Text = "0.00" Me.txtJobPrice5.Focus() End Try End If part1SubTotal = part1UnitPrice * part1Quantity part2SubTotal = part2UnitPrice * part2Quantity part3SubTotal = part3UnitPrice * part3Quantity part4SubTotal = part4UnitPrice * part4Quantity part5SubTotal = part5UnitPrice * part5Quantity Me.txtSubTotal1.Text = part1SubTotal.ToString("F") Me.txtSubTotal2.Text = part2SubTotal.ToString("F") Me.txtSubTotal3.Text = part3SubTotal.ToString("F") Me.txtSubTotal4.Text = part4SubTotal.ToString("F") Me.txtSubTotal5.Text = part5SubTotal.ToString("F") totalParts = part1SubTotal + part2SubTotal + part3SubTotal + _ part4SubTotal + part5SubTotal totalLabor = job1Price + job2Price + job3Price + _ job4Price + job5Price Try taxRate = CDbl(Me.txtTaxRate.Text) Catch exc As FormatException MessageBox.Show("Invalid Tax Rate") Me.txtTaxRate.Text = "7.75" Me.txtTaxRate.Focus() End Try Dim totalPartsAndLabor As Double = totalParts + totalLabor taxAmount = totalPartsAndLabor * taxRate / 100 totalOrder = totalPartsAndLabor + taxAmount Me.txtTotalParts.Text = totalParts.ToString("F") Me.txtTotalLabor.Text = totalLabor.ToString("F") Me.txtTaxAmount.Text = taxAmount.ToString("F") Me.txtRepairTotal.Text = totalOrder.ToString("F") End Sub |
Private Sub btnResetOrder_Click(ByVal sender As Object, ByVal e As System.EventArgs) _ Handles btnResetOrder.Click Me.txtCustomerName.Text = "" Me.txtAddress.Text = "" Me.txtCity.Text = "" Me.txtState.Text = "" Me.txtZIPCode.Text = "" Me.txtMake.Text = "" Me.txtModel.Text = "" Me.txtCarYear.Text = "" Me.txtProblem.Text = "" Me.txtPartName1.Text = "" Me.txtUnitPrice1.Text = "0.00" Me.txtQuantity1.Text = "0" Me.txtSubTotal1.Text = "0.00" Me.txtPartName2.Text = "" Me.txtUnitPrice2.Text = "0.00" Me.txtQuantity2.Text = "0" Me.txtSubTotal2.Text = "0.00" Me.txtPartName3.Text = "" Me.txtUnitPrice3.Text = "0.00" Me.txtQuantity3.Text = "0" Me.txtSubTotal3.Text = "0.00" Me.txtPartName4.Text = "" Me.txtUnitPrice4.Text = "0.00" Me.txtQuantity4.Text = "0" Me.txtSubTotal4.Text = "0.00" Me.txtPartName5.Text = "" Me.txtUnitPrice5.Text = "0.00" Me.txtQuantity5.Text = "0" Me.txtSubTotal5.Text = "0.00" Me.txtJobPerformed1.Text = "" Me.txtJobPrice1.Text = "0.00" Me.txtJobPerformed2.Text = "" Me.txtJobPrice2.Text = "0.00" Me.txtJobPerformed3.Text = "" Me.txtJobPrice3.Text = "0.00" Me.txtJobPerformed4.Text = "" Me.txtJobPrice4.Text = "0.00" Me.txtJobPerformed5.Text = "" Me.txtJobPrice5.Text = "0.00" Me.txtTotalParts.Text = "0.00" Me.txtTotalLabor.Text = "0.00" Me.txtTaxRate.Text = "7.75" Me.txtTaxAmount.Text = "0.00" Me.txtRepairTotal.Text = "0.00" Me.txtRecommendations.Text = "" Me.txtCustomerName.Focus() End Sub |
Imports System.IO Imports System.Globalization Public Class Form1 Inherits System.Windows.Forms.Form |
Private Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) _ Handles btnSave.Click ' Just in case the user forgot to first calculate, do it now Me.btnCalculateOrder_Click(sender, e) ' Get the date this order is/was processed Dim selDate As DateTime = Me.dtpOrderDate.Value ' The list of months Dim strMonth() As String = {"January", "February", "March", "April", _ "May", "June", "July", "August", _ "September", "October", "November", "December"} ' Locate the numeric value of the month from the selected date Dim selMonth As Integer = selDate.Month - 1 ' Locate the year value Dim selYear As Integer = selDate.Year ' This is the name of the folder where the order will be saved Dim strFolder As String = "C:\College Park Auto Shop\" & _ strMonth(selMonth) & " " & CStr(selYear) ' This number will be used to incrementally create the files by their names Dim incremental As Integer = 1000 ' Check the above folder. If it exists, don't create it ' If it doesn't exist, then create it Dim dirInfo As DirectoryInfo = Directory.CreateDirectory(strFolder) ' Get the list of files, if any, from the above folder Dim fleList() As FileInfo = dirInfo.GetFiles() ' If there is no file in the directory, then get ready to create the first file If fleList.Length = 0 Then ' Get ready to display it in the Save dialog box Me.saveFileDialog1.FileName = Me.txtOrderNumber.Text Else ' If there was at least one file in the directory ' Get a reference to the last file Dim fleLast As FileInfo = fleList(fleList.Length - 1) ' Get the name of the last file without its extension Dim fwe As String = Path.GetFileNameWithoutExtension(fleLast.FullName) ' Increment the name of the file by 1 incremental = CInt(fwe) + 1 ' Get ready to display it in the Save dialog box Me.saveFileDialog1.FileName = incremental.ToString() End If ' For convenience, display the Save dialog box in the directory ' created above Me.saveFileDialog1.InitialDirectory = dirInfo.FullName ' Find out if the user clicked OK after displaying the Save dialog box If Me.saveFileDialog1.ShowDialog() = DialogResult.OK Then ' Create a new file using the name of the Save dialog box Dim fleCPAS As FileStream = New FileStream(Me.saveFileDialog1.FileName, _ FileMode.Create, FileAccess.Write, FileShare.Write) Dim bnrCPAS As BinaryWriter = New BinaryWriter(fleCPAS) ' Write each value in the file bnrCPAS.Write(Me.dtpOrderDate.Value.ToString()) bnrCPAS.Write(Me.dtpOrderTime.Value.ToString()) bnrCPAS.Write(Me.txtOrderNumber.Text) bnrCPAS.Write(Me.txtCustomerName.Text) bnrCPAS.Write(Me.txtAddress.Text) bnrCPAS.Write(Me.txtCity.Text) bnrCPAS.Write(Me.txtState.Text) bnrCPAS.Write(Me.txtZIPCode.Text) bnrCPAS.Write(Me.txtMake.Text) bnrCPAS.Write(Me.txtModel.Text) bnrCPAS.Write(Me.txtCarYear.Text) bnrCPAS.Write(Me.txtProblem.Text) bnrCPAS.Write(Me.txtPartName1.Text) bnrCPAS.Write(Me.txtUnitPrice1.Text) bnrCPAS.Write(Me.txtQuantity1.Text) bnrCPAS.Write(Me.txtSubTotal1.Text) bnrCPAS.Write(Me.txtPartName2.Text) bnrCPAS.Write(Me.txtUnitPrice2.Text) bnrCPAS.Write(Me.txtQuantity2.Text) bnrCPAS.Write(Me.txtSubTotal2.Text) bnrCPAS.Write(Me.txtPartName3.Text) bnrCPAS.Write(Me.txtUnitPrice3.Text) bnrCPAS.Write(Me.txtQuantity3.Text) bnrCPAS.Write(Me.txtSubTotal3.Text) bnrCPAS.Write(Me.txtPartName4.Text) bnrCPAS.Write(Me.txtUnitPrice4.Text) bnrCPAS.Write(Me.txtQuantity4.Text) bnrCPAS.Write(Me.txtSubTotal4.Text) bnrCPAS.Write(Me.txtPartName5.Text) bnrCPAS.Write(Me.txtUnitPrice5.Text) bnrCPAS.Write(Me.txtQuantity5.Text) bnrCPAS.Write(Me.txtSubTotal5.Text) bnrCPAS.Write(Me.txtJobPerformed1.Text) bnrCPAS.Write(Me.txtJobPrice1.Text) bnrCPAS.Write(Me.txtJobPerformed2.Text) bnrCPAS.Write(Me.txtJobPrice2.Text) bnrCPAS.Write(Me.txtJobPerformed3.Text) bnrCPAS.Write(Me.txtJobPrice3.Text) bnrCPAS.Write(Me.txtJobPerformed4.Text) bnrCPAS.Write(Me.txtJobPrice4.Text) bnrCPAS.Write(Me.txtJobPerformed5.Text) bnrCPAS.Write(Me.txtJobPrice5.Text) bnrCPAS.Write(Me.txtTotalParts.Text) bnrCPAS.Write(Me.txtTotalLabor.Text) bnrCPAS.Write(Me.txtTaxRate.Text) bnrCPAS.Write(Me.txtTaxAmount.Text) bnrCPAS.Write(Me.txtRepairTotal.Text) bnrCPAS.Write(Me.txtRecommendations.Text) Me.btnResetOrder_Click(sender, e) End If End Sub |
Private Sub btnOpen_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnOpen.Click Me.openFileDialog1.InitialDirectory = "C:\College Park Auto Shop" If Me.openFileDialog1.ShowDialog() = DialogResult.OK Then Dim fleCPAS As FileStream = New FileStream(Me.openFileDialog1.FileName, _ FileMode.Open, FileAccess.Read, FileShare.Read) Dim bnrCPAS As BinaryReader = New BinaryReader(fleCPAS) Me.dtpOrderDate.Value = _ DateTime.Parse(bnrCPAS.ReadString(), New System.Globalization.CultureInfo("en-US", True)) Me.dtpOrderTime.Value = _ DateTime.Parse(bnrCPAS.ReadString(), New System.Globalization.CultureInfo("en-US", True)) Me.txtOrderNumber.Text = bnrCPAS.ReadString() Me.txtCustomerName.Text = bnrCPAS.ReadString() Me.txtAddress.Text = bnrCPAS.ReadString() Me.txtCity.Text = bnrCPAS.ReadString() Me.txtState.Text = bnrCPAS.ReadString() Me.txtZIPCode.Text = bnrCPAS.ReadString() Me.txtMake.Text = bnrCPAS.ReadString() Me.txtModel.Text = bnrCPAS.ReadString() Me.txtCarYear.Text = bnrCPAS.ReadString() Me.txtProblem.Text = bnrCPAS.ReadString() Me.txtPartName1.Text = bnrCPAS.ReadString() Me.txtUnitPrice1.Text = bnrCPAS.ReadString() Me.txtQuantity1.Text = bnrCPAS.ReadString() Me.txtSubTotal1.Text = bnrCPAS.ReadString() Me.txtPartName2.Text = bnrCPAS.ReadString() Me.txtUnitPrice2.Text = bnrCPAS.ReadString() Me.txtQuantity2.Text = bnrCPAS.ReadString() Me.txtSubTotal2.Text = bnrCPAS.ReadString() Me.txtPartName3.Text = bnrCPAS.ReadString() Me.txtUnitPrice3.Text = bnrCPAS.ReadString() Me.txtQuantity3.Text = bnrCPAS.ReadString() Me.txtSubTotal3.Text = bnrCPAS.ReadString() Me.txtPartName4.Text = bnrCPAS.ReadString() Me.txtUnitPrice4.Text = bnrCPAS.ReadString() Me.txtQuantity4.Text = bnrCPAS.ReadString() Me.txtSubTotal4.Text = bnrCPAS.ReadString() Me.txtPartName5.Text = bnrCPAS.ReadString() Me.txtUnitPrice5.Text = bnrCPAS.ReadString() Me.txtQuantity5.Text = bnrCPAS.ReadString() Me.txtSubTotal5.Text = bnrCPAS.ReadString() Me.txtJobPerformed1.Text = bnrCPAS.ReadString() Me.txtJobPrice1.Text = bnrCPAS.ReadString() Me.txtJobPerformed2.Text = bnrCPAS.ReadString() Me.txtJobPrice2.Text = bnrCPAS.ReadString() Me.txtJobPerformed3.Text = bnrCPAS.ReadString() Me.txtJobPrice3.Text = bnrCPAS.ReadString() Me.txtJobPerformed4.Text = bnrCPAS.ReadString() Me.txtJobPrice4.Text = bnrCPAS.ReadString() Me.txtJobPerformed5.Text = bnrCPAS.ReadString() Me.txtJobPrice5.Text = bnrCPAS.ReadString() Me.txtTotalParts.Text = bnrCPAS.ReadString() Me.txtTotalLabor.Text = bnrCPAS.ReadString() Me.txtTaxRate.Text = bnrCPAS.ReadString() Me.txtTaxAmount.Text = bnrCPAS.ReadString() Me.txtRepairTotal.Text = bnrCPAS.ReadString() Me.txtRecommendations.Text = bnrCPAS.ReadString() End If End Sub |
Private Sub btnClose_Click(ByVal sender As Object, ByVal e As System.EventArgs) _ Handles btnClose.Click End End Sub |
|
||
Home | Copyright © 2005-2016, FunctionX | Next |
|