 |
Introduction to Printing |
|
|
|
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
document 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
|
|
- Start Microsoft Visual C#
- Create a new
Windows Application named CollegeParkAutoRepair1
- In the Solution Explorer, right-click Form1.cs and click Rename
- Type Exercise.cs and press Enter
- From the Menus & Toolbars section of the Toolbox, click MenuStrip and
click the form
- Design the menu items as follows:
| MenuItem |
DropDownItems |
| Text |
Name |
Text |
Name |
Image |
Shortcut |
| &File |
mnuFile |
&New Repair Order |
mnuFileNew |
new.ico |
Ctrl+N |
| |
|
&Open Existing Order... |
mnuFileOpen |
open.ico |
Ctrl+O |
| |
|
&Save Current Order |
mnuFileSave |
save.ico |
Ctrl+S |
| |
|
Separator |
|
|
|
| |
|
&Print |
mnuFilePrint |
printer.ico |
Ctrl+P |
| |
|
Separator |
|
|
|
| |
|
E&xit |
mnuFileExit |
|
|
- Design the form as follows:
 |
| Control |
Name |
Text |
Other Properties |
| Group |
|
Order Identification |
|
| Label |
|
Customer Name: |
|
| TextBox |
txtCustomerName |
|
|
| Label |
|
Address |
|
| TextBox |
txtAddress |
|
|
| Label |
|
City: |
|
| TextBox |
txtCity |
|
|
| Label |
|
State: |
|
| TextBox |
txtState |
|
|
| Label |
|
ZIP Code: |
|
| TextBox |
txtZIPCode |
|
TextAlign: Right |
| Label |
|
Make / Model: |
|
| TextBox |
txtMake |
|
|
| TextBox |
txtModel |
|
|
| Label |
|
Year: |
|
| TextBox |
txtCarYear |
|
TextAlign: Right |
| Label |
|
Problem Description: |
|
| TextBox |
txtProblem |
|
|
| GroupBox |
|
Parts Used |
|
| Label |
|
Part Name |
|
| Label |
|
Unit Price |
|
| Label |
|
Qty |
|
| Label |
|
Sub Total |
|
| TextBox |
txtPartName1 |
|
|
| TextBox |
txtUnitPrice1 |
0.00 |
TextAlign: Right |
| TextBox |
txtQuantity1 |
0 |
TextAlign: Right |
| TextBox |
txtSubTotal1 |
0.00 |
TextAlign: Right
Enabled: False |
| TextBox |
txtPartName2 |
|
|
| TextBox |
txtUnitPrice2 |
0.00 |
TextAlign: Right |
| TextBox |
txtQuantity2 |
0 |
TextAlign: Right |
| TextBox |
txtSubTotal2 |
0.00 |
TextAlign: Right
Enabled: False |
| TextBox |
txtPartName3 |
|
|
| TextBox |
txtUnitPrice3 |
0.00 |
TextAlign: Right |
| TextBox |
txtQuantity3 |
0 |
TextAlign: Right |
| TextBox |
txtSubTotal3 |
0.00 |
TextAlign: Right
Enabled: False |
| TextBox |
txtPartName4 |
|
|
| TextBox |
txtUnitPrice4 |
0.00 |
TextAlign: Right |
| TextBox |
txtQuantity4 |
0 |
TextAlign: Right |
| TextBox |
txtSubTotal4 |
0.00 |
TextAlign: Right
Enabled: False |
| TextBox |
txtPartName5 |
|
|
| TextBox |
txtUnitPrice5 |
0.00 |
TextAlign: Right |
| TextBox |
txtQuantity5 |
0 |
TextAlign: Right |
| TextBox |
txtSubTotal5 |
0.00 |
TextAlign: Right
Enabled: False |
| GroupBox |
|
Jobs Performed |
|
| Label |
|
Price |
|
| TextBox |
txtJobPerformed1 |
|
|
| TextBox |
txtJobPrice1 |
0.00 |
TextAlign: Right |
| TextBox |
txtJobPerformed2 |
|
|
| TextBox |
txtJobPrice2 |
0.00 |
TextAlign: Right |
| TextBox |
txtJobPerformed3 |
|
|
| TextBox |
txtJobPrice3 |
0.00 |
TextAlign: Right |
| TextBox |
txtJobPerformed4 |
|
|
| TextBox |
txtJobPrice4 |
0.00 |
TextAlign: Right |
| TextBox |
txtJobPerformed5 |
|
|
| TextBox |
txtJobPrice5 |
0.00 |
TextAlign: Right |
| GroupBox |
|
Order Summary |
|
| Label |
|
Total Parts: |
|
| TextBox |
txtTotalParts |
0.00 |
TextAlign: Right |
| Label |
|
Total Labor: |
|
| TextBox |
txtTotalLabor |
0.00 |
TextAlign: Right |
| Label |
|
Tax Rate: |
|
| TextBox |
txtTaxRate |
7.75 |
TextAlign: Right |
| Label |
|
% |
|
| Label |
|
Tax Amount: |
|
| TextBox |
txtTaxAmount |
0.00 |
TextAlign: Right |
| Label |
|
Total Order: |
|
| TextBox |
txtTotalOrder |
0.00 |
TextAlign: Right |
| Label |
|
Recommendations |
|
| TextBox |
txtRecommendations |
|
Multiline: True
ScrollBars: Vertical |
|
- Right-click the form and click View Code
- Create a method named Calculate as follows:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace CollegeParkAutoRepair1
{
public partial class Exercise : Form
{
public Exercise()
{
InitializeComponent();
}
void Calculate()
{
double part1UnitPrice = 0.00, part2UnitPrice = 0.00,
part3UnitPrice = 0.00, part4UnitPrice = 0.00,
part5UnitPrice = 0.00;
double part1SubTotal, part2SubTotal, part3SubTotal,
part4SubTotal, 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 = 0.00, taxAmount, totalOrder;
// Don't charge a part unless it is clearly identified
if (txtPartName1.Text == "")
{
txtUnitPrice1.Text = "0.00";
txtQuantity1.Text = "0";
txtSubTotal1.Text = "0.00";
part1UnitPrice = 0.00;
}
else
{
try
{
part1UnitPrice = double.Parse(txtUnitPrice1.Text);
}
catch (FormatException)
{
MessageBox.Show("Invalid Unit Price");
txtUnitPrice1.Text = "0.00";
txtUnitPrice1.Focus();
}
try
{
part1Quantity = int.Parse(txtQuantity1.Text);
}
catch (FormatException)
{
MessageBox.Show("Invalid Quantity");
txtQuantity1.Text = "0";
txtQuantity1.Focus();
}
}
if (this.txtPartName2.Text == "" )
{
txtUnitPrice2.Text = "0.00";
txtQuantity2.Text = "0";
txtSubTotal2.Text = "0.00";
part2UnitPrice = 0.00;
}
else
{
try
{
part2UnitPrice = double.Parse(txtUnitPrice2.Text);
}
catch (FormatException)
{
MessageBox.Show("Invalid Unit Price");
this.txtUnitPrice2.Text = "0.00";
this.txtUnitPrice2.Focus();
}
try
{
part2Quantity = int.Parse(txtQuantity2.Text);
}
catch (FormatException)
{
MessageBox.Show("Invalid Quantity");
txtQuantity2.Text = "0";
txtQuantity2.Focus();
}
}
if (txtPartName3.Text == "" )
{
txtUnitPrice3.Text = "0.00";
txtQuantity3.Text = "0";
txtSubTotal3.Text = "0.00";
part3UnitPrice = 0.00;
}
else
{
try
{
part3UnitPrice = double.Parse(txtUnitPrice3.Text);
}
catch (FormatException)
{
MessageBox.Show("Invalid Unit Price");
txtUnitPrice3.Text = "0.00";
txtUnitPrice3.Focus();
}
try
{
part3Quantity = int.Parse(txtQuantity3.Text);
}
catch (FormatException)
{
MessageBox.Show("Invalid Quantity");
txtQuantity3.Text = "0";
txtQuantity3.Focus();
}
}
if (txtPartName4.Text == "" )
{
txtUnitPrice4.Text = "0.00";
txtQuantity4.Text = "0";
txtSubTotal4.Text = "0.00";
part4UnitPrice = 0.00;
}
else
{
try
{
part4UnitPrice = double.Parse(txtUnitPrice4.Text);
}
catch (FormatException)
{
MessageBox.Show("Invalid Unit Price");
txtUnitPrice4.Text = "0.00";
txtUnitPrice4.Focus();
}
try
{
part4Quantity = int.Parse(txtQuantity4.Text);
}
catch (FormatException)
{
MessageBox.Show("Invalid Quantity");
txtQuantity4.Text = "0";
txtQuantity4.Focus();
}
}
if (txtPartName5.Text == "" )
{
txtUnitPrice5.Text = "0.00";
txtQuantity5.Text = "0";
txtSubTotal5.Text = "0.00";
part5UnitPrice = 0.00;
}
else
{
try
{
part5UnitPrice = double.Parse(txtUnitPrice5.Text);
}
catch (FormatException)
{
MessageBox.Show("Invalid Unit Price");
txtUnitPrice5.Text = "0.00";
txtUnitPrice5.Focus();
}
try
{
part5Quantity = int.Parse(txtQuantity5.Text);
}
catch (FormatException)
{
MessageBox.Show("Invalid Quantity");
txtQuantity5.Text = "0";
txtQuantity5.Focus();
}
}
// Don't bill the customer for a job that is not specified
if (txtJobDescription1.Text == "")
{
txtJobPrice1.Text = "0.00";
job1Price = 0.00;
}
else
{
try
{
job1Price = double.Parse(txtJobPrice1.Text);
}
catch (FormatException)
{
MessageBox.Show("Invalid Job Price");
txtJobPrice1.Text = "0.00";
txtJobPrice1.Focus();
}
}
if (txtJobDescription2.Text == "")
{
txtJobPrice2.Text = "0.00";
job2Price = 0.00;
}
else
{
try
{
job2Price = double.Parse(txtJobPrice2.Text);
}
catch (FormatException)
{
MessageBox.Show("Invalid Job Price");
txtJobPrice2.Text = "0.00";
txtJobPrice2.Focus();
}
}
if (txtJobDescription3.Text == "")
{
txtJobPrice3.Text = "0.00";
job3Price = 0.00;
}
else
{
try
{
job3Price = double.Parse(txtJobPrice3.Text);
}
catch (FormatException)
{
MessageBox.Show("Invalid Job Price");
txtJobPrice3.Text = "0.00";
txtJobPrice3.Focus();
}
}
if (txtJobDescription4.Text == "")
{
txtJobPrice4.Text = "0.00";
job4Price = 0.00;
}
else
{
try
{
job4Price = double.Parse(txtJobPrice4.Text);
}
catch (FormatException)
{
MessageBox.Show("Invalid Job Price");
txtJobPrice4.Text = "0.00";
txtJobPrice4.Focus();
}
}
if (txtJobDescription5.Text == "")
{
txtJobPrice5.Text = "0.00";
job5Price = 0.00;
}
else
{
try
{
job5Price = double.Parse(txtJobPrice5.Text);
}
catch (FormatException)
{
MessageBox.Show("Invalid Job Price");
txtJobPrice5.Text = "0.00";
txtJobPrice5.Focus();
}
}
part1SubTotal = part1UnitPrice * part1Quantity;
part2SubTotal = part2UnitPrice * part2Quantity;
part3SubTotal = part3UnitPrice * part3Quantity;
part4SubTotal = part4UnitPrice * part4Quantity;
part5SubTotal = part5UnitPrice * part5Quantity;
txtSubTotal1.Text = part1SubTotal.ToString("F");
txtSubTotal2.Text = part2SubTotal.ToString("F");
txtSubTotal3.Text = part3SubTotal.ToString("F");
txtSubTotal4.Text = part4SubTotal.ToString("F");
txtSubTotal5.Text = part5SubTotal.ToString("F");
totalParts = part1SubTotal + part2SubTotal + part3SubTotal +
part4SubTotal + part5SubTotal;
totalLabor = job1Price + job2Price + job3Price +
job4Price + job5Price;
try
{
taxRate = double.Parse(txtTaxRate.Text);
}
catch (FormatException)
{
MessageBox.Show("Invalid Tax Rate");
txtTaxRate.Text = "7.75";
txtTaxRate.Focus();
}
double totalPartsAndLabor = totalParts + totalLabor;
taxAmount = totalPartsAndLabor * taxRate / 100;
totalOrder = totalPartsAndLabor + taxAmount;
txtTotalParts.Text = totalParts.ToString("F");
txtTotalLabor.Text = totalLabor.ToString("F");
txtTaxAmount.Text = taxAmount.ToString("F");
txtTotalOrder.Text = totalOrder.ToString("F");
}
}
}
|
- Return to the form
- On the form, click File and double-click New Repair Order
- Implement its Click event as
follows:
private void mnuFileNew_Click(object sender, 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.txtJobDescription1.Text = "";
this.txtJobPrice1.Text = "0.00";
this.txtJobDescription2.Text = "";
this.txtJobPrice2.Text = "0.00";
this.txtJobDescription3.Text = "";
this.txtJobPrice3.Text = "0.00";
this.txtJobDescription4.Text = "";
this.txtJobPrice4.Text = "0.00";
this.txtJobDescription5.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.txtTotalOrder.Text = "0.00";
this.txtRecommendations.Text = "";
this.txtCustomerName.Focus();
}
|
- Return to the form
- Click the first text box under Unit Price
- In the Properties window, click the Events button and double-click
Leave
- In the same way, generate the Leave event of the other unit prices,
the quantities, and the job prices text box
- Call the Calculate() method for each:
private void txtUnitPrice1_Leave(object sender, EventArgs e)
{
Calculate();
}
private void txtUnitPrice2_Leave(object sender, EventArgs e)
{
Calculate();
}
private void txtUnitPrice3_Leave(object sender, EventArgs e)
{
Calculate();
}
private void txtUnitPrice4_Leave(object sender, EventArgs e)
{
Calculate();
}
private void txtUnitPrice5_Leave(object sender, EventArgs e)
{
Calculate();
}
private void txtQuantity1_Leave(object sender, EventArgs e)
{
Calculate();
}
private void txtQuantity2_Leave(object sender, EventArgs e)
{
Calculate();
}
private void txtQuantity3_Leave(object sender, EventArgs e)
{
Calculate();
}
private void txtQuantity4_Leave(object sender, EventArgs e)
{
Calculate();
}
private void txtQuantity5_Leave(object sender, EventArgs e)
{
Calculate();
}
private void txtJobPrice1_Leave(object sender, EventArgs e)
{
Calculate();
}
private void txtJobPrice2_Leave(object sender, EventArgs e)
{
Calculate();
}
private void txtJobPrice3_Leave(object sender, EventArgs e)
{
Calculate();
}
private void txtJobPrice4_Leave(object sender, EventArgs e)
{
Calculate();
}
private void txtJobPrice5_Leave(object sender, EventArgs e)
{
Calculate();
}
|
- Return to the form
- From the Dialogs section of the Toolbox,
click OpenFileDialog and click the form
- Change its properties as follows:
Title: Open Existing Repair Order
DefaultExt: rpr
Filter: Repair Orders (*.rpr)|*.rpr|All Files|
Name: dlgOpen
- From the Dialogs section of the Toolbox,
click SaveFileDialog and click the form
- Change its properties as follows:
Title: Save Current Repair Order
DefaultExt: rpr
Filter: Repair Orders (*.rpr)|*.rpr|All Files|
Name: dlgSave
- On the form, click File and double-click Open Existing Order
- In the top section of the file, type the following:
using System;
using System.ComponentModel;
using System.Collections;
using System.Windows.Forms;
using System.Data;
using System.Drawing;
using System.Linq;
using System.IO;
|
- Implement its Click event as follows:
private void mnuFileOpen_Click(object sender, EventArgs e)
{
dlgOpen.InitialDirectory = @"C:\College Park Auto Repair";
if (dlgOpen.ShowDialog() == DialogResult.OK)
{
FileStream fleCPAR = new FileStream(this.dlgOpen.FileName,
FileMode.Open, FileAccess.Read, FileShare.Read);
BinaryReader bnrCPAR = new BinaryReader(fleCPAR);
this.txtCustomerName.Text = bnrCPAR.ReadString();
this.txtAddress.Text = bnrCPAR.ReadString();
this.txtCity.Text = bnrCPAR.ReadString();
this.txtState.Text = bnrCPAR.ReadString();
this.txtZIPCode.Text = bnrCPAR.ReadString();
this.txtMake.Text = bnrCPAR.ReadString();
this.txtModel.Text = bnrCPAR.ReadString();
this.txtCarYear.Text = bnrCPAR.ReadString();
this.txtProblem.Text = bnrCPAR.ReadString();
this.txtPartName1.Text = bnrCPAR .ReadString();
this.txtUnitPrice1.Text = bnrbnrCPAR.ReadString();
this.txtQuantity1.Text = bnrbnrCPAR.ReadString();
this.txtSubTotal1.Text = bnrbnrCPAR.ReadString();
this.txtPartName2.Text = bnrbnrCPAR.ReadString();
this.txtUnitPrice2.Text = bnrbnrCPAR.ReadString();
this.txtQuantity2.Text = bnrbnrCPAR.ReadString();
this.txtSubTotal2.Text = bnrbnrCPAR.ReadString();
this.txtPartName3.Text = bnrbnrCPAR.ReadString();
this.txtUnitPrice3.Text = bnrbnrCPAR.ReadString();
this.txtQuantity3.Text = bnrbnrCPAR.ReadString();
this.txtSubTotal3.Text = bnrbnrCPAR.ReadString();
this.txtPartName4.Text = bnrbnrCPAR.ReadString();
this.txtUnitPrice4.Text = bnrbnrCPAR.ReadString();
this.txtQuantity4.Text = bnrbnrCPAR.ReadString();
this.txtSubTotal4.Text = bnrbnrCPAR.ReadString();
this.txtPartName5.Text = bnrbnrCPAR.ReadString();
this.txtUnitPrice5.Text = bnrbnrCPAR.ReadString();
this.txtQuantity5.Text = bnrbnrCPAR.ReadString();
this.txtSubTotal5.Text = bnrbnrCPAR.ReadString();
this.txtJobDescription1.Text = bnrbnrCPAR.ReadString();
this.txtJobPrice1.Text = bnrbnrCPAR.ReadString();
this.txtJobDescription2.Text = bnrbnrCPAR.ReadString();
this.txtJobPrice2.Text = bnrbnrCPAR.ReadString();
this.txtJobDescription3.Text = bnrbnrCPAR.ReadString();
this.txtJobPrice3.Text = bnrbnrCPAR.ReadString();
this.txtJobDescription4.Text = bnrbnrCPAR.ReadString();
this.txtJobPrice4.Text = bnrbnrCPAR.ReadString();
this.txtJobDescription5.Text = bnrbnrCPAR.ReadString();
this.txtJobPrice5.Text = bnrbnrCPAR.ReadString();
this.txtTotalParts.Text = bnrbnrCPAR.ReadString();
this.txtTotalLabor.Text = bnrbnrCPAR.ReadString();
this.txtTaxRate.Text = bnrbnrCPAR.ReadString();
this.txtTaxAmount.Text = bnrbnrCPAR.ReadString();
this.txtTotalOrder.Text = bnrbnrCPAR.ReadString();
this.txtRecommendations.Text = bnrbnrCPAR.ReadString();
bnrCPAR.Close();
fleCPAR.Close();
}
}
|
- Return to the form
- On the form, click File and double-click Save Current Order
- Implement the
event as follows:
private void mnuFileSave_Click(object sender, EventArgs e)
{
// Just in case, calculate the order now
Calculate();
// 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
string strDirectory = @"C:\College Park Auto Repair";
DirectoryInfo dirInfo = Directory.CreateDirectory(strDirectory);
// 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
dlgSave.FileName = incremental.ToString() + ".rpr";
}
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
dlgSave.FileName = incremental.ToString() + ".rpr";
}
// For convenience, display the Save dialog box in the directory
// created above
dlgSave.InitialDirectory = dirInfo.FullName;
// Find out if the user clicked OK after displaying the Save dialog box
if (dlgSave.ShowDialog() == DialogResult.OK)
{
// Create a new file using the name of the Save dialog box
FileStream fleCPAR = new FileStream(dlgSave.FileName,
FileMode.Create, FileAccess.Write, FileShare.Write);
BinaryWriter bnrCPAR = new BinaryWriter(fleCPAR);
// Write each value in the file
bnrCPAR.Write(this.txtCustomerName.Text);
bnrCPAR.Write(this.txtAddress.Text);
bnrCPAR.Write(this.txtCity.Text);
bnrCPAR.Write(this.txtState.Text);
bnrCPAR.Write(this.txtZIPCode.Text);
bnrCPAR.Write(this.txtMake.Text);
bnrCPAR.Write(this.txtModel.Text);
bnrCPAR.Write(this.txtCarYear.Text);
bnrCPAR.Write(this.txtProblem.Text);
bnrCPAR.Write(this.txtPartName1.Text);
bnrCPAR.Write(this.txtUnitPrice1.Text);
bnrCPAR.Write(this.txtQuantity1.Text);
bnrCPAR.Write(this.txtSubTotal1.Text);
bnrCPAR.Write(this.txtPartName2.Text);
bnrCPAR.Write(this.txtUnitPrice2.Text);
bnrCPAR.Write(this.txtQuantity2.Text);
bnrCPAR.Write(this.txtSubTotal2.Text);
bnrCPAR.Write(this.txtPartName3.Text);
bnrCPAR.Write(this.txtUnitPrice3.Text);
bnrCPAR.Write(this.txtQuantity3.Text);
bnrCPAR.Write(this.txtSubTotal3.Text);
bnrCPAR.Write(this.txtPartName4.Text);
bnrCPAR.Write(this.txtUnitPrice4.Text);
bnrCPAR.Write(this.txtQuantity4.Text);
bnrCPAR.Write(this.txtSubTotal4.Text);
bnrCPAR.Write(this.txtPartName5.Text);
bnrCPAR.Write(this.txtUnitPrice5.Text);
bnrCPAR.Write(this.txtQuantity5.Text);
bnrCPAR.Write(this.txtSubTotal5.Text);
bnrCPAR.Write(this.txtJobDescription1.Text);
bnrCPAR.Write(this.txtJobPrice1.Text);
bnrCPAR.Write(this.txtJobDescription2.Text);
bnrCPAR.Write(this.txtJobPrice2.Text);
bnrCPAR.Write(this.txtJobDescription3.Text);
bnrCPAR.Write(this.txtJobPrice3.Text);
bnrCPAR.Write(this.txtJobDescription4.Text);
bnrCPAR.Write(this.txtJobPrice4.Text);
bnrCPAR.Write(this.txtJobDescription5.Text);
bnrCPAR.Write(this.txtJobPrice5.Text);
bnrCPAR.Write(this.txtTotalParts.Text);
bnrCPAR.Write(this.txtTotalLabor.Text);
bnrCPAR.Write(this.txtTaxRate.Text);
bnrCPAR.Write(this.txtTaxAmount.Text);
bnrCPAR.Write(this.txtTotalOrder.Text);
bnrCPAR.Write(this.txtRecommendations.Text);
bnrCPAR.Close();
fleCPAR.Close();
mnuFileNew_Click(sender, e);
}
}
|
- Execute the application to test it
- Create a new record. Here is an example:

- On the main menu of the form, click File -> Save Current Order...
- Accept the suggested name of the file and click Save
- Create another order, calculate it and save it
- Try opening a previously saved order
- Close the form and return to your programming environment
|
Introduction to the Print Dialog Box |
|
|
One of the ways users print consists of sending the document to the printer. To directly send a document to the printer, you need to make sure that the control, whose value needs to be printed, supports printing. To accommodate the users
of your application, you can provide a menu item or a button they would click. An example of such a button would be
. To print, the user can click this button. With this type of printing, when the user decides to print, the whole document would be printed "as is", in color if the document is colored and if the printer supports colors. If there
is more than one printer, the computer would use what is known as the default printer.
If you want users to be able to configure or customize the printing process, Microsoft Windows provides a common dialog box called
Print. Here is an example:
|
|

The Print dialog box allows a user to select a printer if more than one is available. The user can decide either to print the whole document, to print a range of pages, or to print a portion of the document that was previously selected. The user can also decide on the number of copies to print from the document, the range specified, or the selected portion. Furthermore, the user can access the particular characteristics of the selected printer and specify how the printer should perform the job. For example, if the selected printer can print in color and the document is in color but the user wants to print in black and white,
he or she can specify this using the Properties button.
To provide the users with the ability to customize printing through the Print
dialog box, you can add a
PrintDialog object
from the Dialogs section of the Toolbox to your form. The PrintDialog control is implemented through
the PrintDialog class of the System.Windows.Forms namespace. To
programmatically create a PrintDialog object, you can declare a variable of type
PrinterDialog. Here is an example:
using System;
using System.Drawing;
using System.Windows.Forms;
public class Exercise : Form
{
Button btnPrint;
public Exercise()
{
InitializeComponent();
}
void InitializeComponent()
{
btnPrint = new Button ();
btnPrint.Location = new Point(12, 12);
btnPrint.Text = "&Print...";
btnPrint.Click += new EventHandler(btnPrintDocument);
Controls.Add(btnPrint);
}
void btnPrintDocument(object sender, EventArgs e)
{
PrintDialog dlgPrint = new PrintDialog();
}
}
public class Program
{
static int Main()
{
System.Windows.Forms.Application.Run(new Exercise());
return 0;
}
}
To present the Print dialog box to the user, you can call its ShowDialog() method.
|
Practical
Learning: Providing a Print Dialog Box
|
|
- From the Printing section of the Toolbox, click the PrintDialog button
and click the form
- While the print control is still selected, in the Properties window,
change its Name to dlgPrint
In order to print, the Print dialog box must be given a document to print. This
means that you must first prepare a document prior to printing. To support this,
the .NET Framework provides the PrintDocument class that is defined in the System.Drawing.Printing namespace. This class is
represented in the Toolbox by the PrintDocument button . Based on this, to
prepare a document for printing, you can either add a PrintDocument object to
your project or declare a variable of type PrintDocument. Here is an example:
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Printing;
public class Exercise : Form
{
Button btnPrint;
public Exercise()
{
InitializeComponent();
}
void InitializeComponent()
{
btnPrint = new Button ();
btnPrint.Location = new Point(12, 12);
btnPrint.Text = "&Print...";
btnPrint.Click += new EventHandler(btnPrintDocument);
Controls.Add(btnPrint);
}
void btnPrintDocument(object sender, EventArgs e)
{
PrintDialog dlgPrint = new PrintDialog();
PrintDocument docPrint = new PrintDocument();
}
}
After creating the document to print through a PrintDocument object, you can
associate it with a PrintDialog. To support this, the PrintDialog class
is equipped with the Document property. To specify the object that would
carry the printing, you can assign the PrintDocument object to the PrintDialog.Document
property. Here is an example:
System.Void btnPrint_Click(System.Object sender, System.EventArgs e)
{
PrintDialog dlgPrint = new PrintDialog;
PrintDocument docPrint = new PrintDocument;
dlgPrint.Document = docPrint;
dlgPrint.ShowDialog();
}
A document to print can be made of only one or many pages. Each page has a
number of characteristics. The characteristics of a page are controlled by the PrintDocument.PageSettings
property which itself is based on the PageSettings class. The PageSettings
class is defined in the System.Drawing.Printing namespace. This class
holds the dimensions of the page, the values of the margins applied on the page,
the tray that would supply the paper (since some printers have many trays), the
printer resolution, whether the page would be printed in color or black and
white, whether the page would be printed in Portrait or Landscape orientation,
etc. If you don't want to specify these characteristics, you can set the PrintDocument.PageSettings
property to DefaultPageSettings.
If you know the name of the document to be printed, you can assign it to the PrintDocument.DocumentName
property. Here is an example:
void btnPrintDocument(object sender, EventArgs e)
{
PrintDialog dlgPrint = new PrintDialog();
PrintDocument docPrint = new PrintDocument();
dlgPrint.Document = docPrint;
dlgPrint.ShowDialog();
}
To actually print the document, you can call the PrintDocument.Print()
method. Its syntax is:
public void Print();
|
Practical
Learning: Providing the Document to Print
|
|
- From the Printing section of the Toolbox, click the PrintDocument button
and click the form
- While the print document control is still selected, in the Properties
window, change its name to docPrint
- Under the form, click dlgPrint
- In the Properties window, click Document and select docPrint
|
Events Related to Printing |
|
When the PrintDocument.Print()
method is called, the printing process would start by firing the BeginPrint
event but this event occurs before the first page is printed. The BeginPrint
event is of type PrintEventArgs which does not hold any particular
information, especially for the BeginPrint event. This event allows you
to take some early actions, if necessary, before the printer receives the job.
Once the printer is ready, the application would then need to know what needs to
be printed on the paper. At this time, the PrintPage event is fired. The PrintPage
event is of type PrintPageEventArgs. The PrintPageEventArgs class
allows you to fully customize the page of the document to be printed. For
example, it is equipped with a Graphics property that allows you to
"draw"
anything you want on the paper. The PrintPageEventArgs class also allows
you to get the location and dimensions of the area to be printed. This is
represented by the PageBounds property, which produces a Rectangle
object.
Once the PrintDocument object is ready, you must pass it to the Print dialog
box. To do that, assign the name of the PrintDocument variable to the PrintDialog.Document
property.
|
Practical
Learning: Printing a Document
|
|
- On the bar under the form, double-click docPrint to generate its default
event
- Implement the PrintPage event as follows:
private void docPrint_PrintPage(object sender,
System.Drawing.Printing.PrintPageEventArgs e)
{
e.Graphics.DrawLine(new Pen(Color.Black, 2), 60, 90, 680, 90);
e.Graphics.DrawLine(new Pen(Color.Black, 1), 60, 93, 680, 93);
string strDisplay = "College Park Auto Repair";
System.Drawing.Font fntString = new Font("Times New Roman", 28,
FontStyle.Bold);
e.Graphics.DrawString(strDisplay, fntString,
Brushes.Black, 160, 100);
strDisplay = "Customer Car Repair Order";
fntString = new System.Drawing.Font("Times New Roman", 18,
FontStyle.Bold);
e.Graphics.DrawString(strDisplay, fntString,
Brushes.Black, 220, 150);
e.Graphics.DrawLine(new Pen(Color.Black, 1), 60, 184, 680, 184);
e.Graphics.DrawLine(new Pen(Color.Black, 2), 60, 187, 680, 187);
fntString = new System.Drawing.Font("Times New Roman", 12,
FontStyle.Bold);
e.Graphics.DrawString("Order Identification", fntString,
Brushes.Black, 80, 200);
e.Graphics.DrawLine(new Pen(Color.Black, 1), 100, 250, 640, 250);
fntString = new System.Drawing.Font("Times New Roman", 10,
FontStyle.Bold);
e.Graphics.DrawString("Customer Name:", fntString,
Brushes.Black, 100, 260);
fntString = new System.Drawing.Font("Times New Roman", 10,
FontStyle.Regular);
e.Graphics.DrawString(txtCustomerName.Text, fntString,
Brushes.Black, 260, 260); ;
e.Graphics.DrawLine(new Pen(Color.Black, 1), 100, 280, 640, 280);
fntString = new Font("Times New Roman", 10, FontStyle.Bold);
e.Graphics.DrawString("Address:", fntString,
Brushes.Black, 100, 290);
fntString = new Font("Times New Roman", 10, FontStyle.Regular);
e.Graphics.DrawString(txtAddress.Text, fntString,
Brushes.Black, 260, 290); ;
e.Graphics.DrawLine(new Pen(Color.Black, 1), 100, 310, 640, 310);
fntString = new Font("Times New Roman", 10, FontStyle.Regular);
string strAddress = txtCity.Text.ToString() + ", " +
txtState.Text + " " + txtZIPCode.Text;
e.Graphics.DrawString(strAddress, fntString, Brushes.Black, 260, 320);
e.Graphics.DrawLine(new Pen(Color.Black, 1), 100, 340, 640, 340);
fntString = new Font("Times New Roman", 10, FontStyle.Bold);
e.Graphics.DrawString("Car:", fntString, Brushes.Black, 100, 350);
fntString = new Font("Times New Roman", 10, FontStyle.Regular);
string strCar = txtMake.Text + ", " + txtModel.Text +
", " + txtCarYear.Text;
e.Graphics.DrawString(strCar, fntString, Brushes.Black, 260, 350);
e.Graphics.DrawLine(new Pen(Color.Black, 1), 100, 370, 640, 370);
fntString = new Font("Times New Roman", 10, FontStyle.Bold);
e.Graphics.DrawString("Problem Description:", fntString,
Brushes.Black, 100, 380);
fntString = new Font("Times New Roman", 10, FontStyle.Regular);
e.Graphics.DrawString(txtProblem.Text, fntString,
Brushes.Black,
new RectangleF(260, 380, 420, 380));
e.Graphics.DrawLine(new Pen(Color.Black, 1), 100, 400, 640, 400);
fntString = new System.Drawing.Font("Times New Roman", 12, FontStyle.Bold);
e.Graphics.DrawString("Parts Used", fntString,
Brushes.Black, 80, 430);
e.Graphics.DrawLine(new Pen(Color.Black, 2), 80, 450, 680, 450);
e.Graphics.DrawString("Parts Name", fntString,
Brushes.Black, 100, 460);
e.Graphics.DrawString("Unit Price", fntString,
Brushes.Black, 420, 460);
e.Graphics.DrawString("Qty", fntString,
Brushes.Black, 520, 460);
e.Graphics.DrawString("Sub-Total", fntString,
Brushes.Black, 562, 460);
e.Graphics.DrawLine(new Pen(Color.Black, 1), 100, 480, 640, 480);
fntString = new Font("Times New Roman", 10, FontStyle.Regular);
StringFormat fmtString = new StringFormat();
fmtString.Alignment = StringAlignment.Far;
e.Graphics.DrawString(txtPartName1.Text, fntString,
Brushes.Black, 100, 490);
e.Graphics.DrawString(txtUnitPrice1.Text, fntString,
Brushes.Black, 480, 490, fmtString);
e.Graphics.DrawString(txtQuantity1.Text, fntString,
Brushes.Black, 540, 490, fmtString);
e.Graphics.DrawString(txtSubTotal1.Text, fntString,
Brushes.Black, 630, 490, fmtString);
e.Graphics.DrawLine(new Pen(Color.Black, 1),
100, 510, 640, 510);
e.Graphics.DrawString(txtPartName2.Text, fntString,
Brushes.Black, 100, 520);
e.Graphics.DrawString(txtUnitPrice2.Text, fntString,
Brushes.Black, 480, 520, fmtString);
e.Graphics.DrawString(txtQuantity2.Text, fntString,
Brushes.Black, 540, 520, fmtString);
e.Graphics.DrawString(txtSubTotal2.Text, fntString,
Brushes.Black, 630, 520, fmtString);
e.Graphics.DrawLine(new Pen(Color.Black, 1), 100, 540, 640, 540);
e.Graphics.DrawString(txtPartName3.Text, fntString,
Brushes.Black, 100, 550);
e.Graphics.DrawString(txtUnitPrice3.Text, fntString,
Brushes.Black, 480, 550, fmtString);
e.Graphics.DrawString(txtQuantity3.Text, fntString,
Brushes.Black, 540, 550, fmtString);
e.Graphics.DrawString(txtSubTotal3.Text, fntString,
Brushes.Black, 630, 550, fmtString);
e.Graphics.DrawLine(new Pen(Color.Black, 1),
100, 570, 640, 570);
e.Graphics.DrawString(txtPartName4.Text, fntString,
Brushes.Black, 100, 580);
e.Graphics.DrawString(txtUnitPrice4.Text, fntString,
Brushes.Black, 480, 580, fmtString);
e.Graphics.DrawString(txtQuantity4.Text, fntString,
Brushes.Black, 540, 580, fmtString);
e.Graphics.DrawString(txtSubTotal4.Text, fntString,
Brushes.Black, 630, 580, fmtString);
e.Graphics.DrawLine(new Pen(Color.Black, 1), 100, 600, 640, 600);
e.Graphics.DrawString(txtPartName5.Text, fntString,
Brushes.Black, 100, 610);
e.Graphics.DrawString(txtUnitPrice5.Text, fntString,
Brushes.Black, 480, 610, fmtString);
e.Graphics.DrawString(txtQuantity5.Text, fntString,
Brushes.Black, 540, 610, fmtString);
e.Graphics.DrawString(txtSubTotal5.Text, fntString,
Brushes.Black, 630, 610, fmtString);
e.Graphics.DrawLine(new Pen(Color.Black, 1), 100, 630, 640, 630);
fntString = new Font("Times New Roman", 12, FontStyle.Bold);
e.Graphics.DrawString("Jobs Performed", fntString,
Brushes.Black, 80, 650);
e.Graphics.DrawLine(new Pen(Color.Black, 2), 80, 670, 680, 670);
e.Graphics.DrawString("Job Name", fntString, Brushes.Black, 100, 680);
e.Graphics.DrawString("Price", fntString, Brushes.Black, 562, 680);
e.Graphics.DrawLine(new Pen(Color.Black, 1), 100, 700, 640, 700);
fntString = new Font("Times New Roman", 10, FontStyle.Regular);
e.Graphics.DrawString(txtJobDescription1.Text, fntString,
Brushes.Black, 100, 710);
e.Graphics.DrawString(txtJobPrice1.Text, fntString,
Brushes.Black, 600, 710, fmtString);
e.Graphics.DrawLine(new Pen(Color.Black, 1), 100, 730, 640, 730);
e.Graphics.DrawString(txtJobDescription2.Text, fntString,
Brushes.Black, 100, 740);
e.Graphics.DrawString(txtJobPrice2.Text, fntString,
Brushes.Black, 600, 740, fmtString);
e.Graphics.DrawLine(new Pen(Color.Black, 1), 100, 760, 640, 760);
e.Graphics.DrawString(txtJobDescription3.Text, fntString,
Brushes.Black, 100, 770);
e.Graphics.DrawString(txtJobPrice3.Text, fntString,
Brushes.Black, 600, 770, fmtString);
e.Graphics.DrawLine(new Pen(Color.Black, 1), 100, 790, 640, 790);
e.Graphics.DrawString(txtJobDescription4.Text, fntString,
Brushes.Black, 100, 800);
e.Graphics.DrawString(txtJobPrice4.Text, fntString,
Brushes.Black, 600, 800, fmtString);
e.Graphics.DrawLine(new Pen(Color.Black, 1), 100, 820, 640, 820);
e.Graphics.DrawString(txtJobDescription5.Text, fntString,
Brushes.Black, 100, 830);
e.Graphics.DrawString(txtJobPrice5.Text, fntString,
Brushes.Black, 600, 830, fmtString);
e.Graphics.DrawLine(new Pen(Color.Black, 1), 100, 850, 640, 850);
fntString = new System.Drawing.Font("Times New Roman", 12, FontStyle.Bold);
e.Graphics.DrawString("Order Summary", fntString,
Brushes.Black, 80, 870);
e.Graphics.DrawLine(new Pen(Color.Black, 2), 80, 890, 680, 890);
fntString = new System.Drawing.Font("Times New Roman", 10, FontStyle.Bold);
e.Graphics.DrawString("Total Parts:", fntString,
Brushes.Black, 500, 900);
fntString = new System.Drawing.Font("Times New Roman", 10, FontStyle.Regular);
e.Graphics.DrawString(txtTotalParts.Text, fntString,
Brushes.Black, 640, 900, fmtString);
fntString = new System.Drawing.Font("Times New Roman", 10, FontStyle.Bold);
e.Graphics.DrawString("Total Labor:", fntString,
Brushes.Black, 500, 920);
fntString = new System.Drawing.Font("Times New Roman", 10, FontStyle.Regular);
e.Graphics.DrawString(txtTotalLabor.Text, fntString,
Brushes.Black, 640, 920, fmtString);
fntString = new System.Drawing.Font("Times New Roman", 10, FontStyle.Bold);
e.Graphics.DrawString("Tax Rate:", fntString,
Brushes.Black, 500, 940);
fntString = new System.Drawing.Font("Times New Roman", 10, FontStyle.Regular);
e.Graphics.DrawString(txtTaxRate.Text, fntString,
Brushes.Black, 640, 940, fmtString);
fntString = new System.Drawing.Font("Times New Roman", 10, FontStyle.Bold);
e.Graphics.DrawString("Tax Amount:", fntString,
Brushes.Black, 500, 960);
fntString = new System.Drawing.Font("Times New Roman", 10, FontStyle.Regular);
e.Graphics.DrawString(txtTaxAmount.Text, fntString,
Brushes.Black, 640, 960, fmtString);
fntString = new System.Drawing.Font("Times New Roman", 10, FontStyle.Bold);
e.Graphics.DrawString("Repair Total:", fntString,
Brushes.Black, 500, 980);
fntString = new System.Drawing.Font("Times New Roman", 10, FontStyle.Regular);
e.Graphics.DrawString(txtTotalOrder.Text, fntString,
Brushes.Black, 640, 980, fmtString);
fntString = new System.Drawing.Font("Times New Roman", 10, FontStyle.Bold);
e.Graphics.DrawString("Recommendations:", fntString,
Brushes.Black, 100, 900);
fntString = new System.Drawing.Font("Times New Roman", 10, FontStyle.Regular);
e.Graphics.DrawString(txtRecommendations.Text, fntString,
Brushes.Black, new RectangleF(100, 920, 350, 280));
}
|
- Return to the form
- On the form, click File and double-click Print
- Implement its Click event as follows:
private void mnuFilePrint_Click(object sender, EventArgs e)
{
if ( dlgPrint.ShowDialog() == DialogResult.OK)
{
docPrint.Print();
}
}
|
- Execute the application and open one of the previously saved orders
- On the main menu of the form, click File -> Print and click OK
- After using it, close the form and return to your programming environment
In the above example, we saw a somewhat simplistic way of making the Print
dialog box available to the user. This dialog box offers many options defined as
the Printer Settings. To support the various options of a Print dialog box, the
PrintDialog class is equipped with a property called PrinterSettings, which
itself is defined from the PrinterSettings class, which holds all possible
characteristics of a printer.
The first option presented to the user is the name of the printer to be used.
Because there can be many printers available to the user, the printers are
presented as a combo box:
The available printers may also be presented as a list view:
The Name combo box in the Printer section or the Select Printer list view allows the user to select the printer
that will handle the job. If you are writing a universal application and cannot
predict what printer(s) the user would have, you would not be concerned with this characteristic. If you are writing an application for a special company
or you are creating a particular application and
you know for sure what printer should be used to print the current document,
then you can specify the printer to use. To do this, assign the (exact) name of
the printer to the PrinterSettings.PrinterName property. On the other
hand, if for some reason you want to know what printer the user selected to
print the document, you can get the value of this PrinterName property.
Under the Name combo box, the labels provide the status of the selected printer
(whether it is ready or not),
the type of printer (such as its manufacturer), its location (such as where in
the building the printer is located; the person who installed the printer or may
have provided this printer), and an optional comment (this information is
created by the person who installed the printer or it can be changed in the
printer's properties).
After selecting the printer, the user can access the properties of that
particular printer. Different printers support different options. To configure
the printed paper based on the selected printer, the user can click either
Properties or Preferences. This opens the Document Properties or the Printing
Preferences dialog box. The content of this dialog box (highly) depends on the
printer that was selected but some characteristics are shared among printers.
On the lower-right side of the Printer section or of the Select Printer
section, there is a check box labeled Print To File. When this check box is
checked, the document is transformed into a file rather than being printed. In
this case, if the user clicks OK or Print, a dialog box would come up, asking the user to
specify the path and a name for the new file that will be created. The
appearance of that dialog box depends. Here is an example:
If you want the Print To File check box to be checked, set the PrinterSettings.PrintFoFile
Boolean property to true. Its default value is false, which lets the user decide
whether to check it or not.
After selecting the printer and deciding whether to physically print or to only
create a printed file, the user can click OK.
If the document is made of only
one page, it would be printed. If the document contains more than one page, the
user may want to print only one page, a range of pages, or all pages. The user
can also select a section in the document and print only that section. This
decision is made using the Page Range section. This section provides four radio
buttons. The first radio button labeled All is selected by default and allows
the user to print the whole document. By default, the second radio button is
disabled. If the user had selected a section of the document to print, then the
second radio button, labeled Selection would be enabled:
This allows the user to still specify whether to print the whole document or only the
section that was selected. If the document contains more than one page, the user
can navigate to a particular page and decide to print only that page using the
Current Page radio button. Again, if the document contains more than one page,
the user can specify a range of pages to print.
All these options are usually left up to the user. On the other hand, if you
want to specify the range of pages to print, you can use the PrinterSettings.FromPage
and the ToPage properties. If you want to specify the limits of ranges allowed
to the user, use the MinimumPage and the MaximumPage properties.
On the right side of the Page Range section, a spin button allows the user to
specify the number of copies to make when printing. If you want to specify this
number by default, assign the desired value to the Copies property. If the user
(or you) set this number to a value higher than 1, then the printed papers
can be collated or not. This is specified using the Collate check box. If you
want to programmatically collate the pages or not, change the Boolean value of
the Collate property.
|
|