364
LESSON 31 Printing
TRY IT
In this Try It, you build a program that prints and displays a preview of the table shown in
Figure 31-2. You build an array of
Student objects and then loop through them displaying
their values as shown in the figure.
FIGURE 312
You can download the code and resources for this Try It from the book’s web
page at
www.wrox.com or www.CSharpHelper.com/24hour.html. You can find
them in the Lesson31 folder of the download.
Lesson Requirements
Start a new project and create the program’s main form. Add
PrintDocument and
PrintPreviewDialog components to do the printing and previewing.
Add appropriate event handlers to the Preview and Print buttons.
Add a
Student class with FirstName and LastName properties. Also give it a TestScores
property that is an array of integers.
Create the
PrintPage event handler.
Create an array of
Student objects. Initialize them using array and object initializers.
Loop through the
Student objects printing them using code similar to the code used
by the PrintText example program described earlier.
Draw a rectangle around the table.
596906c31.indd 364 4/7/10 12:34:31 PM
Try It
365
Hints
Don’t forget to set the
PrintPreviewDialog’s Document property to the PrintDocument
component.
This example doesn’t do anything fancy with properties so they can be auto-implemented.
It might help to define variables
x0, x1, and so on to keep track of where each column should
begin.
Step-by-Step
Start a new project and create the program’s main form. Add
PrintDocument and
PrintPreviewDialog components to do the printing and previewing.
1. This is reasonably straightforward.
Add appropriate event handlers to the Preview and Print buttons.
1. Use code similar to the following:
// Display a print preview.
private void previewButton_Click(object sender, EventArgs e)
{
textPrintPreviewDialog.ShowDialog();
}
// Print.
private void printButton_Click(object sender, EventArgs e)
{
textPrintDocument.Print();
}
Add a
Student class with FirstName and LastName properties. Also give it a TestScores
property that is an array of integers.
1. Use code similar to the following:
class Student
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int[]TestScores { get; set; }
}
Create the
PrintPage event handler.
Create an array of
Student objects. Initialize them using array and object initializers.
Loop through the Student objects, printing them using code similar to the code used
by the PrintText example program described earlier.
Draw a rectangle around the table.
596906c31.indd 365 4/7/10 12:34:31 PM
366
LESSON 31 Printing
1. Use code similar to the following:
// Print the table.
private void textPrintDocument_PrintPage(object sender,
System.Drawing.Printing.PrintPageEventArgs e)
{
// Make some data.
Student[] students =
{
new Student() {FirstName=”Ann”, LastName=”Archer”,
TestScores=new int[] {91, 92, 93, 94}},
new Student() {FirstName=”Bob”, LastName=”Blarth”,
TestScores=new int[] {81, 82, 83, 84}},
new Student() {FirstName=”Cyd”, LastName=”Carter”,
TestScores=new int[] {71, 72, 73, 74}},
new Student() {FirstName=”Dan”, LastName=”Deever”,
TestScores=new int[] {61, 62, 63, 64}},
};
// Get the coordinates for the rst row and the columns.
int y = e.MarginBounds.Top;
int x0 = e.MarginBounds.Left;
int x1 = x0 + 200;
int x2 = x1 + 100;
int x3 = x2 + 100;
int x4 = x3 + 100;
// Make a font to use.
using (Font font = new Font(“Times New Roman”, 20))
{
// Draw column headers.
e.Graphics.DrawString(“Name”, font, Brushes.Black, x0, y);
e.Graphics.DrawString(“Test 1”, font, Brushes.Black, x1, y);
e.Graphics.DrawString(“Test 2”, font, Brushes.Black, x2, y);
e.Graphics.DrawString(“Test 3”, font, Brushes.Black, x3, y);
e.Graphics.DrawString(“Test 4”, font, Brushes.Black, x4, y);
// Move Y down for the rst row.
y += 30;
// Loop through the Students displaying their data.
foreach (Student student in students)
{
// Display the Student’s values.
e.Graphics.DrawString(student.FirstName + “ “ +
student.LastName, font, Brushes.Black, x0, y);
e.Graphics.DrawString(student.TestScores[0].ToString(),
font, Brushes.Black, x1, y);
e.Graphics.DrawString(student.TestScores[1].ToString(),
font, Brushes.Black, x2, y);
e.Graphics.DrawString(student.TestScores[2].ToString(),
font, Brushes.Black, x3, y);
e.Graphics.DrawString(student.TestScores[3].ToString(),
font, Brushes.Black, x4, y);
// Move Y down for the next row.
y += 30;
}
596906c31.indd 366 4/7/10 12:34:31 PM
Exercises
367
}
// Draw a box around it all.
e.Graphics.DrawRectangle(Pens.Black,
x0, e.MarginBounds.Top,
x4 - x0 + 100,
y - e.MarginBounds.Top);
// We’re only printing one page.
e.HasMorePages = false;
}
Please select Lesson 31 on the DVD to view the video that accompanies this lesson.
EXERCISES
1. Copy the program you built in this lesson’s Try It and add additional drawing code to pro-
duce the result shown in Figure 31-3.
FIGURE 313
2. Make a program that prints a bar chart similar to the one shown in Figure 31-4.
FIGURE 314
596906c31.indd 367 4/7/10 12:34:32 PM
368
LESSON 31 Printing
3. Build the program shown in Figure 31-5. The main form (on the left) contains a DataGridView
control where the user can enter values. The File menu contains Preview and Print commands.
FIGURE 315
Add PrintDocument and PrintPreviewDialog controls as usual. The PrintPage event
handler should:
1. Call the grid’s EndEdit method to commit any current edit.
2. Loop through the grid’s Columns collection displaying the column headers. Add each
column’s
Width value to the X coordinate for the next column.
3. Loop through the grid’s Rows collection. For each row, loop through the row’s Cells
collection, displaying the cells’
FormattedValue property.
4. For extra credit, draw lines between the values.
You can download the solutions to these exercises from the book’s web page at
www.wrox.com or www.CSharpHelper.com/24hour.html. You can find those
solutions in the Lesson31 folder.
596906c31.indd 368 4/7/10 12:34:32 PM
Click here to Play
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.12.249