![]() |
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. |
|
|
![]() |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
private void btnCalculateOrder_Click(object sender, System.EventArgs e)
{
decimal part1UnitPrice = 0.00M, part1SubTotal = 0.00M, part2UnitPrice = 0.00M,
part2SubTotal = 0.00M, part3UnitPrice = 0.00M, part3SubTotal = 0.00M,
part4UnitPrice = 0.00M, part4SubTotal = 0.00M, part5UnitPrice = 0.00M,
part5SubTotal = 0.00M, totalParts = 0.00M;
int part1Quantity = 0, part2Quantity = 0, part3Quantity = 0,
part4Quantity = 0, part5Quantity = 0;
decimal job1Price = 0.00M, job2Price = 0.00M, job3Price = 0.00M,
job4Price = 0.00M, job5Price = 0.00M;
decimal totalLabor = 0.00M;
decimal taxRate = 0.00M, taxAmount = 0.00M, totalOrder = 0.00M;
// Don't charge a part unless it is clearly identified
if( this.txtPartName1.Text == "" )
{
this.txtUnitPrice1.Text = "0.00";
this.txtQuantity1.Text = "0";
this.txtSubTotal1.Text = "0.00";
part1UnitPrice = 0.00M;
}
else
{
try
{
part1UnitPrice = decimal.Parse(this.txtUnitPrice1.Text);
}
catch(FormatException)
{
MessageBox.Show("Invalid Unit Price");
this.txtUnitPrice1.Text = "0.00";
this.txtUnitPrice1.Focus();
}
try
{
part1Quantity = int.Parse(this.txtQuantity1.Text);
}
catch(FormatException)
{
MessageBox.Show("Invalid Quantity");
this.txtQuantity1.Text = "0";
this.txtQuantity1.Focus();
}
}
if( this.txtPartName2.Text == "" )
{
this.txtUnitPrice2.Text = "0.00";
this.txtQuantity2.Text = "0";
this.txtSubTotal2.Text = "0.00";
part2UnitPrice = 0.00M;
}
else
{
try
{
part2UnitPrice = decimal.Parse(this.txtUnitPrice2.Text);
}
catch(FormatException)
{
MessageBox.Show("Invalid Unit Price");
this.txtUnitPrice2.Text = "0.00";
this.txtUnitPrice2.Focus();
}
try
{
part2Quantity = int.Parse(this.txtQuantity2.Text);
}
catch(FormatException)
{
MessageBox.Show("Invalid Quantity");
this.txtQuantity2.Text = "0";
this.txtQuantity2.Focus();
}
}
if( this.txtPartName3.Text == "" )
{
this.txtUnitPrice3.Text = "0.00";
this.txtQuantity3.Text = "0";
this.txtSubTotal3.Text = "0.00";
part3UnitPrice = 0.00M;
}
else
{
try
{
part3UnitPrice = decimal.Parse(this.txtUnitPrice3.Text);
}
catch(FormatException)
{
MessageBox.Show("Invalid Unit Price");
this.txtUnitPrice3.Text = "0.00";
this.txtUnitPrice3.Focus();
}
try
{
part3Quantity = int.Parse(this.txtQuantity3.Text);
}
catch(FormatException)
{
MessageBox.Show("Invalid Quantity");
this.txtQuantity3.Text = "0";
this.txtQuantity3.Focus();
}
}
if( this.txtPartName4.Text == "" )
{
this.txtUnitPrice4.Text = "0.00";
this.txtQuantity4.Text = "0";
this.txtSubTotal4.Text = "0.00";
part4UnitPrice = 0.00M;
}
else
{
try
{
part4UnitPrice = decimal.Parse(this.txtUnitPrice4.Text);
}
catch(FormatException)
{
MessageBox.Show("Invalid Unit Price");
this.txtUnitPrice4.Text = "0.00";
this.txtUnitPrice4.Focus();
}
try
{
part4Quantity = int.Parse(this.txtQuantity4.Text);
}
catch(FormatException)
{
MessageBox.Show("Invalid Quantity");
this.txtQuantity4.Text = "0";
this.txtQuantity4.Focus();
}
}
if( this.txtPartName5.Text == "" )
{
this.txtUnitPrice5.Text = "0.00";
this.txtQuantity5.Text = "0";
this.txtSubTotal5.Text = "0.00";
part5UnitPrice = 0.00M;
}
else
{
try
{
part5UnitPrice = decimal.Parse(this.txtUnitPrice5.Text);
}
catch(FormatException)
{
MessageBox.Show("Invalid Unit Price");
this.txtUnitPrice5.Text = "0.00";
this.txtUnitPrice5.Focus();
}
try
{
part5Quantity = int.Parse(this.txtQuantity5.Text);
}
catch(FormatException)
{
MessageBox.Show("Invalid Quantity");
this.txtQuantity5.Text = "0";
this.txtQuantity5.Focus();
}
}
// Don't bill the customer for a job that is not specified
if( this.txtJobPerformed1.Text == "" )
{
this.txtJobPrice1.Text = "0.00";
job1Price = 0.00M;
}
else
{
try
{
job1Price = decimal.Parse(this.txtJobPrice1.Text);
}
catch(FormatException)
{
MessageBox.Show("Invalid Job Price");
this.txtJobPrice1.Text = "0.00";
this.txtJobPrice1.Focus();
}
}
if( this.txtJobPerformed2.Text == "" )
{
this.txtJobPrice2.Text = "0.00";
job2Price = 0.00M;
}
else
{
try
{
job2Price = decimal.Parse(this.txtJobPrice2.Text);
}
catch(FormatException)
{
MessageBox.Show("Invalid Job Price");
this.txtJobPrice2.Text = "0.00";
this.txtJobPrice2.Focus();
}
}
if( this.txtJobPerformed3.Text == "" )
{
this.txtJobPrice3.Text = "0.00";
job3Price = 0.00M;
}
else
{
try
{
job3Price = decimal.Parse(this.txtJobPrice3.Text);
}
catch(FormatException)
{
MessageBox.Show("Invalid Job Price");
this.txtJobPrice3.Text = "0.00";
this.txtJobPrice3.Focus();
}
}
if( this.txtJobPerformed4.Text == "" )
{
this.txtJobPrice4.Text = "0.00";
job4Price = 0.00M;
}
else
{
try
{
job4Price = decimal.Parse(this.txtJobPrice4.Text);
}
catch(FormatException)
{
MessageBox.Show("Invalid Job Price");
this.txtJobPrice4.Text = "0.00";
this.txtJobPrice4.Focus();
}
}
if( this.txtJobPerformed5.Text == "" )
{
this.txtJobPrice5.Text = "0.00";
job5Price = 0.00M;
}
else
{
try
{
job5Price = decimal.Parse(this.txtJobPrice5.Text);
}
catch(FormatException)
{
MessageBox.Show("Invalid Job Price");
this.txtJobPrice5.Text = "0.00";
this.txtJobPrice5.Focus();
}
}
part1SubTotal = part1UnitPrice * part1Quantity;
part2SubTotal = part2UnitPrice * part2Quantity;
part3SubTotal = part3UnitPrice * part3Quantity;
part4SubTotal = part4UnitPrice * part4Quantity;
part5SubTotal = part5UnitPrice * part5Quantity;
this.txtSubTotal1.Text = part1SubTotal.ToString("F");
this.txtSubTotal2.Text = part2SubTotal.ToString("F");
this.txtSubTotal3.Text = part3SubTotal.ToString("F");
this.txtSubTotal4.Text = part4SubTotal.ToString("F");
this.txtSubTotal5.Text = part5SubTotal.ToString("F");
totalParts = part1SubTotal + part2SubTotal + part3SubTotal +
part4SubTotal + part5SubTotal;
totalLabor = job1Price + job2Price + job3Price +
job4Price + job5Price;
try
{
taxRate = decimal.Parse(this.txtTaxRate.Text);
}
catch(FormatException)
{
MessageBox.Show("Invalid Tax Rate");
this.txtTaxRate.Text = "7.75";
this.txtTaxRate.Focus();
}
decimal totalPartsAndLabor = totalParts + totalLabor;
taxAmount = totalPartsAndLabor * taxRate / 100;
totalOrder = totalPartsAndLabor + taxAmount;
this.txtTotalParts.Text = totalParts.ToString("F");
this.txtTotalLabor.Text = totalLabor.ToString("F");
this.txtTaxAmount.Text = taxAmount.ToString("F");
this.txtRepairTotal.Text = totalOrder.ToString("F");
}
|
private void btnResetOrder_Click(object sender, System.EventArgs e)
{
this.txtCustomerName.Text = "";
this.txtAddress.Text = "";
this.txtCity.Text = "";
this.txtState.Text = "";
this.txtZIPCode.Text = "";
this.txtMake.Text = "";
this.txtModel.Text = "";
this.txtCarYear.Text = "";
this.txtProblem.Text = "";
this.txtPartName1.Text = "";
this.txtUnitPrice1.Text = "0.00";
this.txtQuantity1.Text = "0";
this.txtSubTotal1.Text = "0.00";
this.txtPartName2.Text = "";
this.txtUnitPrice2.Text = "0.00";
this.txtQuantity2.Text = "0";
this.txtSubTotal2.Text = "0.00";
this.txtPartName3.Text = "";
this.txtUnitPrice3.Text = "0.00";
this.txtQuantity3.Text = "0";
this.txtSubTotal3.Text = "0.00";
this.txtPartName4.Text = "";
this.txtUnitPrice4.Text = "0.00";
this.txtQuantity4.Text = "0";
this.txtSubTotal4.Text = "0.00";
this.txtPartName5.Text = "";
this.txtUnitPrice5.Text = "0.00";
this.txtQuantity5.Text = "0";
this.txtSubTotal5.Text = "0.00";
this.txtJobPerformed1.Text = "";
this.txtJobPrice1.Text = "0.00";
this.txtJobPerformed2.Text = "";
this.txtJobPrice2.Text = "0.00";
this.txtJobPerformed3.Text = "";
this.txtJobPrice3.Text = "0.00";
this.txtJobPerformed4.Text = "";
this.txtJobPrice4.Text = "0.00";
this.txtJobPerformed5.Text = "";
this.txtJobPrice5.Text = "0.00";
this.txtTotalParts.Text = "0.00";
this.txtTotalLabor.Text = "0.00";
this.txtTaxRate.Text = "7.75";
this.txtTaxAmount.Text = "0.00";
this.txtRepairTotal.Text = "0.00";
this.txtRecommendations.Text = "";
this.txtCustomerName.Focus();
}
|
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.IO; using System.Globalization; |
private void btnSave_Click(object sender, System.EventArgs e)
{
// Just in case the user forgot to first calculate, do it now
this.btnCalculateOrder_Click(sender, e);
// Get the date this order is/was processed
DateTime selDate = this.dtpOrderDate.Value;
// The list of months
string[] strMonth = { "January", "February", "March", "April",
"May", "June", "July", "August",
"September", "October", "November", "December" };
// Locate the numeric value of the month from the selected date
int selMonth = selDate.Month - 1;
// Locate the year value
int selYear = selDate.Year;
// This is the name of the folder where the order will be saved
string strFolder = String.Concat("C:\\College Park Auto Shop\\",
strMonth[selMonth], " ", selYear.ToString());
// This number will be used to incrementally create the files by their names
int incremental = 1000;
// Check the above folder. If it exists, don't create it
// If it doesn't exist, then create it
DirectoryInfo dirInfo = Directory.CreateDirectory(strFolder);
// Get the list of files, if any, from the above folder
FileInfo[] fleList = dirInfo.GetFiles();
// If there is no file in the directory, then get ready to create the first file
if( fleList.Length == 0 )
{
// Get ready to display it in the Save dialog box
this.saveFileDialog1.FileName = this.txtOrderNumber.Text;
}
else // If there was at least one file in the directory
{
// Get a reference to the last file
FileInfo fleLast = fleList[fleList.Length-1];
// Get the name of the last file without its extension
string fwe = Path.GetFileNameWithoutExtension(fleLast.FullName);
// Increment the name of the file by 1
incremental = int.Parse(fwe) + 1;
// Get ready to display it in the Save dialog box
this.saveFileDialog1.FileName = incremental.ToString();
}
// For convenience, display the Save dialog box in the directory
// created above
this.saveFileDialog1.InitialDirectory = dirInfo.FullName;
// Find out if the user clicked OK after displaying the Save dialog box
if( this.saveFileDialog1.ShowDialog() == DialogResult.OK )
{
// Create a new file using the name of the Save dialog box
FileStream fleCPAS = new FileStream(this.saveFileDialog1.FileName,
FileMode.Create, FileAccess.Write, FileShare.Write);
BinaryWriter bnrCPAS = new BinaryWriter(fleCPAS);
// Write each value in the file
bnrCPAS.Write(this.dtpOrderDate.Value.ToString());
bnrCPAS.Write(this.dtpOrderTime.Value.ToString());
bnrCPAS.Write(this.txtOrderNumber.Text);
bnrCPAS.Write(this.txtCustomerName.Text);
bnrCPAS.Write(this.txtAddress.Text);
bnrCPAS.Write(this.txtCity.Text);
bnrCPAS.Write(this.txtState.Text);
bnrCPAS.Write(this.txtZIPCode.Text);
bnrCPAS.Write(this.txtMake.Text);
bnrCPAS.Write(this.txtModel.Text);
bnrCPAS.Write(this.txtCarYear.Text);
bnrCPAS.Write(this.txtProblem.Text);
bnrCPAS.Write(this.txtPartName1.Text);
bnrCPAS.Write(this.txtUnitPrice1.Text);
bnrCPAS.Write(this.txtQuantity1.Text);
bnrCPAS.Write(this.txtSubTotal1.Text);
bnrCPAS.Write(this.txtPartName2.Text);
bnrCPAS.Write(this.txtUnitPrice2.Text);
bnrCPAS.Write(this.txtQuantity2.Text);
bnrCPAS.Write(this.txtSubTotal2.Text);
bnrCPAS.Write(this.txtPartName3.Text);
bnrCPAS.Write(this.txtUnitPrice3.Text);
bnrCPAS.Write(this.txtQuantity3.Text);
bnrCPAS.Write(this.txtSubTotal3.Text);
bnrCPAS.Write(this.txtPartName4.Text);
bnrCPAS.Write(this.txtUnitPrice4.Text);
bnrCPAS.Write(this.txtQuantity4.Text);
bnrCPAS.Write(this.txtSubTotal4.Text);
bnrCPAS.Write(this.txtPartName5.Text);
bnrCPAS.Write(this.txtUnitPrice5.Text);
bnrCPAS.Write(this.txtQuantity5.Text);
bnrCPAS.Write(this.txtSubTotal5.Text);
bnrCPAS.Write(this.txtJobPerformed1.Text);
bnrCPAS.Write(this.txtJobPrice1.Text);
bnrCPAS.Write(this.txtJobPerformed2.Text);
bnrCPAS.Write(this.txtJobPrice2.Text);
bnrCPAS.Write(this.txtJobPerformed3.Text);
bnrCPAS.Write(this.txtJobPrice3.Text);
bnrCPAS.Write(this.txtJobPerformed4.Text);
bnrCPAS.Write(this.txtJobPrice4.Text);
bnrCPAS.Write(this.txtJobPerformed5.Text);
bnrCPAS.Write(this.txtJobPrice5.Text);
bnrCPAS.Write(this.txtTotalParts.Text);
bnrCPAS.Write(this.txtTotalLabor.Text);
bnrCPAS.Write(this.txtTaxRate.Text);
bnrCPAS.Write(this.txtTaxAmount.Text);
bnrCPAS.Write(this.txtRepairTotal.Text);
bnrCPAS.Write(this.txtRecommendations.Text);
this.btnResetOrder_Click(sender, e);
}
}
|
private void btnOpen_Click(object sender, System.EventArgs e)
{
this.openFileDialog1.InitialDirectory = "C:\\College Park Auto Shop";
if( this.openFileDialog1.ShowDialog() == DialogResult.OK )
{
FileStream fleCPAS = new FileStream(this.openFileDialog1.FileName,
FileMode.Open, FileAccess.Read, FileShare.Read);
BinaryReader bnrCPAS = new BinaryReader(fleCPAS);
this.dtpOrderDate.Value =
DateTime.Parse(bnrCPAS.ReadString(), new System.Globalization.CultureInfo("en-US", true));
this.dtpOrderTime.Value =
DateTime.Parse(bnrCPAS.ReadString(), new System.Globalization.CultureInfo("en-US", true));
this.txtOrderNumber.Text = bnrCPAS.ReadString();
this.txtCustomerName.Text = bnrCPAS.ReadString();
this.txtAddress.Text = bnrCPAS.ReadString();
this.txtCity.Text = bnrCPAS.ReadString();
this.txtState.Text = bnrCPAS.ReadString();
this.txtZIPCode.Text = bnrCPAS.ReadString();
this.txtMake.Text = bnrCPAS.ReadString();
this.txtModel.Text = bnrCPAS.ReadString();
this.txtCarYear.Text = bnrCPAS.ReadString();
this.txtProblem.Text = bnrCPAS.ReadString();
this.txtPartName1.Text = bnrCPAS.ReadString();
this.txtUnitPrice1.Text = bnrCPAS.ReadString();
this.txtQuantity1.Text = bnrCPAS.ReadString();
this.txtSubTotal1.Text = bnrCPAS.ReadString();
this.txtPartName2.Text = bnrCPAS.ReadString();
this.txtUnitPrice2.Text = bnrCPAS.ReadString();
this.txtQuantity2.Text = bnrCPAS.ReadString();
this.txtSubTotal2.Text = bnrCPAS.ReadString();
this.txtPartName3.Text = bnrCPAS.ReadString();
this.txtUnitPrice3.Text = bnrCPAS.ReadString();
this.txtQuantity3.Text = bnrCPAS.ReadString();
this.txtSubTotal3.Text = bnrCPAS.ReadString();
this.txtPartName4.Text = bnrCPAS.ReadString();
this.txtUnitPrice4.Text = bnrCPAS.ReadString();
this.txtQuantity4.Text = bnrCPAS.ReadString();
this.txtSubTotal4.Text = bnrCPAS.ReadString();
this.txtPartName5.Text = bnrCPAS.ReadString();
this.txtUnitPrice5.Text = bnrCPAS.ReadString();
this.txtQuantity5.Text = bnrCPAS.ReadString();
this.txtSubTotal5.Text = bnrCPAS.ReadString();
this.txtJobPerformed1.Text = bnrCPAS.ReadString();
this.txtJobPrice1.Text = bnrCPAS.ReadString();
this.txtJobPerformed2.Text = bnrCPAS.ReadString();
this.txtJobPrice2.Text = bnrCPAS.ReadString();
this.txtJobPerformed3.Text = bnrCPAS.ReadString();
this.txtJobPrice3.Text = bnrCPAS.ReadString();
this.txtJobPerformed4.Text = bnrCPAS.ReadString();
this.txtJobPrice4.Text = bnrCPAS.ReadString();
this.txtJobPerformed5.Text = bnrCPAS.ReadString();
this.txtJobPrice5.Text = bnrCPAS.ReadString();
this.txtTotalParts.Text = bnrCPAS.ReadString();
this.txtTotalLabor.Text = bnrCPAS.ReadString();
this.txtTaxRate.Text = bnrCPAS.ReadString();
this.txtTaxAmount.Text = bnrCPAS.ReadString();
this.txtRepairTotal.Text = bnrCPAS.ReadString();
this.txtRecommendations.Text = bnrCPAS.ReadString();
}
}
|
private void btnClose_Click(object sender, System.EventArgs e)
{
Close();
}
|


|
|
||
| Home | Copyright © 2005-2012 FunctionX | Next |
|
|
||