Lab Create a Print Document

In this lab, you will expand on the solution you created in Lesson 1, "Managing the Print Process by Using Print Dialogs," and create an application that allows the user to open a text file and print the contents.

► Exercise 1: Creating a Print Document

1. From the Toolbox, drag an OpenFileDialog box onto the form.

2. In the Properties window, set the Filter property of OpenFileDialog1 to Text Files | *.txt.

3. Beneath the FileMenuItem, add a menu item named Open File.

4. Double-click OpenFileToolStripMenuItem to open the code editor to the OpenFileToolStripMenuItem_Click event handler.

5. Outside of the OpenFileToolStripMenuItem_Click event handler, add the following code:

Dim s As String

Dim strings As String()

Dim ArrayCounter As Integer = 0

string s; string[] strings; int ArrayCounter = 0;

6. Inside the OpenFileToolStripMenuItem_Click event handler, add the following code:

Dim aResult As Windows.Forms.DialogResult aResult = OpenFileDialog1.ShowDialog If aResult = Windows.Forms.DialogResult.OK Then

Dim aReader As New System.IO.StreamReader(OpenFileDialog1.FileName)

s = aReader.ReadToEnd aReader.Close()

strings = s.Split(ControlChars.CrLf) End If

System.Windows.Forms.DialogResult aResult; aResult = openFileDialog1.ShowDialog();

if (aResult == System.Windows.Forms.DialogResult.OK) {

System.IO.StreamReader aReader = new

System.IO.StreamReader(openFileDialog1.FileName); s = aReader.ReadToEnd(); aReader.Close(); strings = s.Split('\n');

7. In the PrintDocument1_PrintPage event handler, replace the existing code with the following code:

Dim LeftMargin As Single = e.MarginBounds.Left

Dim TopMargin As Single = e.MarginBounds.Top

Dim MyLines As Single = 0

Dim YPosition As Single = 0

Dim Counter As Integer = 0

Dim CurrentLine As String

MyLines = e.MarginBounds.Height / _


While Counter < MyLines And ArrayCounter <= strings.Length - 1 CurrentLine = strings(ArrayCounter)

YPosition = TopMargin + Counter * Me.Font.GetHeight(e.Graphics) e.Graphics.DrawString(CurrentLine, Me.Font, Brushes.Black, _

LeftMargin, YPosition, New StringFormat()) Counter += 1 ArrayCounter += 1 End While

If Not (ArrayCounter >= strings.Length - 1) Then e.HasMorePages = True Else e.HasMorePages = False End If

float LeftMargin = e.MarginBounds.Left; float TopMargin = e.MarginBounds.Top; float MyLines = 0; float YPosition = 0; int Counter = 0; string CurrentLine; MyLines = e.MarginBounds.Height / this.Font.GetHeight(e.Graphics);

while (Counter < MyLines && ArrayCounter <= strings.Length - 1) {

CurrentLine = strings[ArrayCounter];

YPosition = TopMargin + Counter * this.Font.GetHeight(e.Graphics); e.Graphics.DrawString(CurrentLine, this.Font, Brushes.Black,

LeftMargin, YPosition, new StringFormatO); Counter++; ArrayCounter++;

if (!(ArrayCounter >= strings.GetLength(0) - 1)) e.HasMorePages = true;

else e.HasMorePages = false;

8. Press F5 to run the application. Choose Open File from the file menu and open a text file on your computer. Choose Print Preview from the File menu to view the file in the Print Preview dialog box. Print the file. Experiment with other page and print settings.

Was this article helpful?

0 0

Post a comment