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 |
|
System::Void btnCalculateOrder_Click(System::Object * sender, System::EventArgs * e) { double part1UnitPrice, part1SubTotal, part2UnitPrice, part2SubTotal, part3UnitPrice, part3SubTotal, part4UnitPrice, part4SubTotal, part5UnitPrice, part5SubTotal, totalParts; int part1Quantity = 0, part2Quantity = 0, part3Quantity = 0, part4Quantity = 0, part5Quantity = 0; double job1Price = 0.00, job2Price = 0.00, job3Price = 0.00, job4Price = 0.00, job5Price = 0.00; double totalLabor; double taxRate, taxAmount, totalOrder; // Don't charge a part unless it is clearly identified if( this->txtPartName1->Text->Equals(S"") ) { this->txtUnitPrice1->Text = S"0.00"; this->txtQuantity1->Text = S"0"; this->txtSubTotal1->Text = S"0.00"; part1UnitPrice = 0.00; } else { try { part1UnitPrice = this->txtUnitPrice1->Text->ToDouble(0); } catch(FormatException *) { MessageBox::Show(S"Invalid Unit Price"); this->txtUnitPrice1->Text = S"0.00"; this->txtUnitPrice1->Focus(); } try { part1Quantity = this->txtQuantity1->Text->ToInt16(0); } catch(FormatException *) { MessageBox::Show(S"Invalid Quantity"); this->txtQuantity1->Text = S"0"; this->txtQuantity1->Focus(); } } if( this->txtPartName2->Text->Equals(S"") ) { this->txtUnitPrice2->Text = S"0.00"; this->txtQuantity2->Text = S"0"; this->txtSubTotal2->Text = S"0.00"; part2UnitPrice = 0.00; } else { try { part2UnitPrice = this->txtUnitPrice2->Text->ToDouble(0); } catch(FormatException *) { MessageBox::Show(S"Invalid Unit Price"); this->txtUnitPrice2->Text = S"0.00"; this->txtUnitPrice2->Focus(); } try { part2Quantity = this->txtQuantity2->Text->ToInt16(0); } catch(FormatException *) { MessageBox::Show(S"Invalid Quantity"); this->txtQuantity2->Text = S"0"; this->txtQuantity2->Focus(); } } if( this->txtPartName3->Text->Equals(S"") ) { this->txtUnitPrice3->Text = S"0.00"; this->txtQuantity3->Text = S"0"; this->txtSubTotal3->Text = S"0.00"; part3UnitPrice = 0.00; } else { try { part3UnitPrice = this->txtUnitPrice3->Text->ToDouble(0); } catch(FormatException *) { MessageBox::Show(S"Invalid Unit Price"); this->txtUnitPrice3->Text = S"0.00"; this->txtUnitPrice3->Focus(); } try { part3Quantity = this->txtQuantity3->Text->ToInt16(0); } catch(FormatException *) { MessageBox::Show(S"Invalid Quantity"); this->txtQuantity3->Text = S"0"; this->txtQuantity3->Focus(); } } if( this->txtPartName4->Text->Equals(S"") ) { this->txtUnitPrice4->Text = S"0.00"; this->txtQuantity4->Text = S"0"; this->txtSubTotal4->Text = S"0.00"; part4UnitPrice = 0.00; } else { try { part4UnitPrice = this->txtUnitPrice4->Text->ToDouble(0); } catch(FormatException *) { MessageBox::Show(S"Invalid Unit Price"); this->txtUnitPrice4->Text = S"0.00"; this->txtUnitPrice4->Focus(); } try { part4Quantity = this->txtQuantity4->Text->ToInt16(0); } catch(FormatException *) { MessageBox::Show(S"Invalid Quantity"); this->txtQuantity4->Text = S"0"; this->txtQuantity4->Focus(); } } if( this->txtPartName5->Text->Equals(S"") ) { this->txtUnitPrice5->Text = S"0.00"; this->txtQuantity5->Text = S"0"; this->txtSubTotal5->Text = S"0.00"; part5UnitPrice = 0.00; } else { try { part5UnitPrice = this->txtUnitPrice5->Text->ToDouble(0); } catch(FormatException *) { MessageBox::Show(S"Invalid Unit Price"); this->txtUnitPrice5->Text = S"0.00"; this->txtUnitPrice5->Focus(); } try { part5Quantity = this->txtQuantity5->Text->ToInt16(0); } catch(FormatException *) { MessageBox::Show(S"Invalid Quantity"); this->txtQuantity5->Text = S"0"; this->txtQuantity5->Focus(); } } // Don't bill the customer for a job that is not specified if( this->txtJobPerformed1->Text->Equals(S"") ) { this->txtJobPrice1->Text = S"0.00"; job1Price = 0.00; } else { try { job1Price = this->txtJobPrice1->Text->ToDouble(0); } catch(FormatException *) { MessageBox::Show(S"Invalid Job Price"); this->txtJobPrice1->Text = S"0.00"; this->txtJobPrice1->Focus(); } } if( this->txtJobPerformed2->Text->Equals(S"") ) { this->txtJobPrice2->Text = S"0.00"; job2Price = 0.00; } else { try { job2Price = this->txtJobPrice2->Text->ToDouble(0); } catch(FormatException *) { MessageBox::Show(S"Invalid Job Price"); this->txtJobPrice2->Text = S"0.00"; this->txtJobPrice2->Focus(); } } if( this->txtJobPerformed3->Text->Equals(S"") ) { this->txtJobPrice3->Text = S"0.00"; job3Price = 0.00; } else { try { job3Price = this->txtJobPrice3->Text->ToDouble(0); } catch(FormatException *) { MessageBox::Show(S"Invalid Job Price"); this->txtJobPrice3->Text = S"0.00"; this->txtJobPrice3->Focus(); } } if( this->txtJobPerformed4->Text->Equals(S"") ) { this->txtJobPrice4->Text = S"0.00"; job4Price = 0.00; } else { try { job4Price = this->txtJobPrice4->Text->ToDouble(0); } catch(FormatException *) { MessageBox::Show(S"Invalid Job Price"); this->txtJobPrice4->Text = S"0.00"; this->txtJobPrice4->Focus(); } } if( this->txtJobPerformed5->Text->Equals(S"") ) { this->txtJobPrice5->Text = S"0.00"; job5Price = 0.00; } else { try { job5Price = this->txtJobPrice5->Text->ToDouble(0); } catch(FormatException *) { MessageBox::Show(S"Invalid Job Price"); this->txtJobPrice5->Text = S"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(S"F"); this->txtSubTotal2->Text = part2SubTotal.ToString(S"F"); this->txtSubTotal3->Text = part3SubTotal.ToString(S"F"); this->txtSubTotal4->Text = part4SubTotal.ToString(S"F"); this->txtSubTotal5->Text = part5SubTotal.ToString(S"F"); totalParts = part1SubTotal + part2SubTotal + part3SubTotal + part4SubTotal + part5SubTotal; totalLabor = job1Price + job2Price + job3Price + job4Price + job5Price; try { taxRate = this->txtTaxRate->Text->ToDouble(0); } catch(FormatException *) { MessageBox::Show(S"Invalid Tax Rate"); this->txtTaxRate->Text = S"7.75"; this->txtTaxRate->Focus(); } double totalPartsAndLabor = totalParts + totalLabor; taxAmount = totalPartsAndLabor * taxRate / 100; totalOrder = totalPartsAndLabor + taxAmount; this->txtTotalParts->Text = totalParts.ToString(S"F"); this->txtTotalLabor->Text = totalLabor.ToString(S"F"); this->txtTaxAmount->Text = taxAmount.ToString(S"F"); this->txtRepairTotal->Text = totalOrder.ToString(S"F"); } |
private: System::Void btnResetOrder_Click(System::Object * sender, System::EventArgs * e) { this->txtCustomerName->Text = S""; this->txtAddress->Text = S""; this->txtCity->Text = S""; this->txtState->Text = S""; this->txtZIPCode->Text = S""; this->txtMake->Text = S""; this->txtModel->Text = S""; this->txtCarYear->Text = S""; this->txtProblem->Text = S""; this->txtPartName1->Text = S""; this->txtUnitPrice1->Text = S"0.00"; this->txtQuantity1->Text = S"0"; this->txtSubTotal1->Text = S"0.00"; this->txtPartName2->Text = S""; this->txtUnitPrice2->Text = S"0.00"; this->txtQuantity2->Text = S"0"; this->txtSubTotal2->Text = S"0.00"; this->txtPartName3->Text = S""; this->txtUnitPrice3->Text = S"0.00"; this->txtQuantity3->Text = S"0"; this->txtSubTotal3->Text = S"0.00"; this->txtPartName4->Text = S""; this->txtUnitPrice4->Text = S"0.00"; this->txtQuantity4->Text = S"0"; this->txtSubTotal4->Text = S"0.00"; this->txtPartName5->Text = S""; this->txtUnitPrice5->Text = S"0.00"; this->txtQuantity5->Text = S"0"; this->txtSubTotal5->Text = S"0.00"; this->txtJobPerformed1->Text = S""; this->txtJobPrice1->Text = S"0.00"; this->txtJobPerformed2->Text = S""; this->txtJobPrice2->Text = S"0.00"; this->txtJobPerformed3->Text = S""; this->txtJobPrice3->Text = S"0.00"; this->txtJobPerformed4->Text = S""; this->txtJobPrice4->Text = S"0.00"; this->txtJobPerformed5->Text = S""; this->txtJobPrice5->Text = S"0.00"; this->txtTotalParts->Text = S"0.00"; this->txtTotalLabor->Text = S"0.00"; this->txtTaxRate->Text = S"7.75"; this->txtTaxAmount->Text = S"0.00"; this->txtRepairTotal->Text = S"0.00"; this->txtRecommendations->Text = S""; this->txtCustomerName->Focus(); } |
using namespace System; using namespace System::ComponentModel; using namespace System::Collections; using namespace System::Windows::Forms; using namespace System::Data; using namespace System::Drawing; using namespace System::IO; using namespace System::Globalization; |
System::Void btnSave_Click(System::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[] = { S"January", S"February", S"March", S"April", S"May", S"June", S"July", S"August", S"September", S"October", S"November", S"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(S"C:\\College Park Auto Shop\\", strMonth[selMonth], S" ", 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 = fwe->ToInt32(0) + 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: System::Void btnOpen_Click(System::Object * sender, System::EventArgs * e) { this->openFileDialog1->InitialDirectory = S"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 = bnrCPAS->ReadString()->ToDateTime(new System::Globalization::CultureInfo(S"en-US", true)); this->dtpOrderTime->Value = bnrCPAS->ReadString()->ToDateTime(new System::Globalization::CultureInfo(S"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: System::Void btnClose_Click(System::Object * sender, System::EventArgs * e) { Close(); } |
|
||
Home | Copyright © 2005-2016, FunctionX | Next |
|