Home

File-Based Applications:
College Park Auto-Shop

 

Printing

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. That is the case when using WordPad: its Standard toolbar is equipped with such a 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.

To provide the users with the ability to customize printing through the Print dialog box, you can add a PrintDialog object from 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 pointer to PrinterDialog. Here is an example:

private void btnPrint_Click(object sender, System.EventArgs e)
{
	PrintDialog dlgPrint = new PrintDialog();
}

To present the Print dialog box to the user, you can call its ShowDialog() method.

Practical LearningPractical Learning: Providing a Print Dialog Box

  • Display the form. From the Toolbox, click the PrintDialog button and click the form

 

The Document to Print

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 pointer to PrintDocument. Here is an example:

private void btnPrint_Click(object sender, System.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:

private void btnPrint_Click(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:

private void btnPrint_Click(object sender, System.EventArgs e)
{
	PrintDialog dlgPrint = new PrintDialog();
	PrintDocument docPrint = new PrintDocument();

	docPrint.DocumentName  = "Exercise";

	dlgPrint.Document = docPrint;
	dlgPrint.ShowDialog();
}

To actually print the document, you can call the PrintDocument.Print() method. Its syntax is:

public void Print();
 
 

Practical LearningPractical Learning: Printing

  1. From the Toolbox, click the PrintDocument button and click the form
  2. On the bar under the form, click printDialog1
  3. In the Properties window, set its Document to printDocument1
  4. On the bar under the form, double-click the printDocument1 button and implement its PrintPage event as follows:
     
    private void printDocument1_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-Shop";
    	System.Drawing.Font fntString = new System.Drawing.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, 2), 80, 220, 680, 220);
    
    	e.Graphics.DrawString("Order #:", fntString, Brushes.Black, 500, 200);
    	fntString = new System.Drawing.Font("Times New Roman", 12, FontStyle.Regular);
    	e.Graphics.DrawString(this.txtOrderNumber.Text, fntString, Brushes.Black, 575, 200);
    	 
    	fntString = new System.Drawing.Font("Times New Roman", 10, FontStyle.Bold);
    	string[] strDateTimeFormat = { "D", "T" };
    	e.Graphics.DrawString("Order Date: ", fntString, Brushes.Black, 100, 230);
    
    	fntString = new System.Drawing.Font("Times New Roman", 10, FontStyle.Regular);
    string strDate = this.dtpOrderDate.Value.ToString(strDateTimeFormat[0], DateTimeFormatInfo.InvariantInfo);
    	e.Graphics.DrawString(strDate, fntString, Brushes.Black, 200, 230);
    
    	fntString = new System.Drawing.Font("Times New Roman", 10, FontStyle.Bold);
    	e.Graphics.DrawString("Order Time:", fntString, Brushes.Black, 500, 230);
    	fntString = new System.Drawing.Font("Times New Roman", 10, FontStyle.Regular);
    string strTime = this.dtpOrderTime.Value.ToString(strDateTimeFormat[1], DateTimeFormatInfo.InvariantInfo);
    	e.Graphics.DrawString(strTime, fntString, Brushes.Black, 580, 230);
    
    	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(this.txtCustomerName.Text, fntString, Brushes.Black, 260, 260);;
    
    	e.Graphics.DrawLine(new Pen(Color.Black, 1), 100, 280, 640, 280);
    
    	fntString = new System.Drawing.Font("Times New Roman", 10, FontStyle.Bold);
    	e.Graphics.DrawString("Address:", fntString, Brushes.Black, 100, 290);
    	fntString = new System.Drawing.Font("Times New Roman", 10, FontStyle.Regular);
    	e.Graphics.DrawString(this.txtAddress.Text, fntString, Brushes.Black, 260, 290);;
    	 
    	e.Graphics.DrawLine(new Pen(Color.Black, 1), 100, 310, 640, 310);
    
    	fntString = new System.Drawing.Font("Times New Roman", 10, FontStyle.Regular);
    string strAddress = String.Concat(this.txtCity.Text, ", ", this.txtState.Text, " ", this.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 System.Drawing.Font("Times New Roman", 10, FontStyle.Bold);
    	e.Graphics.DrawString("Car:", fntString, Brushes.Black, 100, 350);
    	fntString = new System.Drawing.Font("Times New Roman", 10, FontStyle.Regular);
    	string strCar = String.Concat(this.txtMake.Text, ", ", this.txtModel.Text, ", ", this.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 System.Drawing.Font("Times New Roman", 10, FontStyle.Bold);
    	e.Graphics.DrawString("Problem Description:", fntString, Brushes.Black, 100, 380);
    	fntString = new System.Drawing.Font("Times New Roman", 10, FontStyle.Regular);
    e.Graphics.DrawString(this.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 System.Drawing.Font("Times New Roman", 10, FontStyle.Regular);
    	StringFormat fmtString = new StringFormat();
    	fmtString.Alignment = StringAlignment.Far;
    
    	e.Graphics.DrawString(this.txtPartName1.Text,  fntString, Brushes.Black,  100, 490);
    	e.Graphics.DrawString(this.txtUnitPrice1.Text, fntString, Brushes.Black, 480, 490, fmtString);
    	e.Graphics.DrawString(this.txtQuantity1.Text,  fntString, Brushes.Black, 540, 490, fmtString);
    	e.Graphics.DrawString(this.txtSubTotal1.Text,  fntString, Brushes.Black, 630, 490, fmtString);
    	e.Graphics.DrawLine(new Pen(Color.Black, 1), 100, 510, 640, 510);
    
    	e.Graphics.DrawString(this.txtPartName2.Text,  fntString, Brushes.Black,  100, 520);
    	e.Graphics.DrawString(this.txtUnitPrice2.Text, fntString, Brushes.Black, 480, 520, fmtString);
    	e.Graphics.DrawString(this.txtQuantity2.Text,  fntString, Brushes.Black, 540, 520, fmtString);
    	e.Graphics.DrawString(this.txtSubTotal2.Text,  fntString, Brushes.Black, 630, 520, fmtString);
    	e.Graphics.DrawLine(new Pen(Color.Black, 1), 100, 540, 640, 540);
    
    	e.Graphics.DrawString(this.txtPartName3.Text,  fntString, Brushes.Black,  100, 550);
    	e.Graphics.DrawString(this.txtUnitPrice3.Text, fntString, Brushes.Black, 480, 550, fmtString);
    	e.Graphics.DrawString(this.txtQuantity3.Text,  fntString, Brushes.Black, 540, 550, fmtString);
    	e.Graphics.DrawString(this.txtSubTotal3.Text,  fntString, Brushes.Black, 630, 550, fmtString);
    	e.Graphics.DrawLine(new Pen(Color.Black, 1), 100, 570, 640, 570);
    
    	e.Graphics.DrawString(this.txtPartName4.Text,  fntString, Brushes.Black,  100, 580);
    	e.Graphics.DrawString(this.txtUnitPrice4.Text, fntString, Brushes.Black, 480, 580, fmtString);
    	e.Graphics.DrawString(this.txtQuantity4.Text,  fntString, Brushes.Black, 540, 580, fmtString);
    	e.Graphics.DrawString(this.txtSubTotal4.Text,  fntString, Brushes.Black, 630, 580, fmtString);
    	e.Graphics.DrawLine(new Pen(Color.Black, 1), 100, 600, 640, 600);
    
    	e.Graphics.DrawString(this.txtPartName5.Text,  fntString, Brushes.Black,  100, 610);
    	e.Graphics.DrawString(this.txtUnitPrice5.Text, fntString, Brushes.Black, 480, 610, fmtString);
    	e.Graphics.DrawString(this.txtQuantity5.Text,  fntString, Brushes.Black, 540, 610, fmtString);
    	e.Graphics.DrawString(this.txtSubTotal5.Text,  fntString, Brushes.Black, 630, 610, fmtString);
    	e.Graphics.DrawLine(new Pen(Color.Black, 1), 100, 630, 640, 630);
    
    	fntString = new System.Drawing.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 System.Drawing.Font("Times New Roman", 10, FontStyle.Regular);
    	 
    	e.Graphics.DrawString(this.txtJobPerformed1.Text, fntString, Brushes.Black, 100, 710);
    	e.Graphics.DrawString(this.txtJobPrice1.Text, fntString, Brushes.Black, 600, 710, fmtString);
    	e.Graphics.DrawLine(new Pen(Color.Black, 1), 100, 730, 640, 730);
    
    	e.Graphics.DrawString(this.txtJobPerformed2.Text, fntString, Brushes.Black, 100, 740);
    	e.Graphics.DrawString(this.txtJobPrice2.Text, fntString, Brushes.Black, 600, 740, fmtString);
    	e.Graphics.DrawLine(new Pen(Color.Black, 1), 100, 760, 640, 760);
    
    	e.Graphics.DrawString(this.txtJobPerformed3.Text, fntString, Brushes.Black, 100, 770);
    	e.Graphics.DrawString(this.txtJobPrice3.Text, fntString, Brushes.Black, 600, 770, fmtString);
    	e.Graphics.DrawLine(new Pen(Color.Black, 1), 100, 790, 640, 790);
    
    	e.Graphics.DrawString(this.txtJobPerformed4.Text, fntString, Brushes.Black, 100, 800);
    	e.Graphics.DrawString(this.txtJobPrice4.Text, fntString, Brushes.Black, 600, 800, fmtString);
    	e.Graphics.DrawLine(new Pen(Color.Black, 1), 100, 820, 640, 820);
    
    	e.Graphics.DrawString(this.txtJobPerformed5.Text, fntString, Brushes.Black, 100, 830);
    	e.Graphics.DrawString(this.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(this.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(this.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(this.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(this.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(this.txtRepairTotal.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(this.txtRecommendations.Text, fntString,
    		Brushes.Black, new RectangleF(100, 920, 350, 280));
    }
  5. On the form, double-click the Print Repair Order button
  6. Implement its Click event as follows:
     
    private void btnPrint_Click(object sender, System.EventArgs e)
    {
    	if( this.printDialog1.ShowDialog() == DialogResult.OK )
    	{
    		this.printDocument1.Print();
    	}
    }
  7. Execute the application and create an order then click File . Print
  8. After printing the order, save it
  9. After using it, close the form and return to your programming environment

Print Preview

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:

To provide a Page Setup to your application, you can use the PageSetupDialog button from the Toolbox. The PageSetupDialog control is based on the PageSetupDialog class which derives from the CommonDialog class.

Microsoft Visual Studio .NET provides the ability to preview a document before printing it. To use it, you can use the appropriate control but you must "draw" what you are planning to print.

You can then add a PrintDocument and a PrintPreviewDialog controls to your form. When the user clicks the menu item or button you had provided for print preview, you can implement its event by drawing what will be printing, retrieving values from existing controls.

Practical LearningPractical Learning: Allowing Document Preview

  1. On the Toolbox, click PrintPreviewDialog and click the form
  2. In the Properties window, set its Document to printDocument1
  3. On the form, double-click the Print Preview button
  4. Implement its event as follows:
     
    private void btnPreview_Click(object sender, System.EventArgs e)
    {
    	this.printPreviewDialog1.ShowDialog();
    }
  5. Execute the application
  6. Open one of the previously saved orders and click the Print Preview button
     
  7. Click Close and close the form to return to your programming environment

 
 

Previous Copyright © 2005-2016, FunctionX