Printing 2

Objectives


Discussion

Printing a List Down a Page

Using e.graphics

Back to top


Demonstration

Open the project that you created in the last lesson.  We will modify the form. 

1.  Add a listbox (lstPrint).  Add several items to the collection of items in the Properties Window.  You can add any items that you want.  You are going to print these items later.  Add a FontDialog box (dgFont). Also add a menu item ("Font") with a name mnuFont .  We will use the font selected in the FontDialog box to print.

2.  In the click event for mnuFont add the following code:

dgFont.ShowDialog()

3.  Now comment out the code in the PrintPage event for PrintDocument1 and add the following code . This code will print the items from the listbox down the page.

Dim i As Int16
For i = 0 To lstPrint.Items.Count - 1
   e.Graphics.DrawString( _ 
   lstPrint.Items(i), _ 
   dgFont.Font, Brushes.Black, _  
   e.MarginBounds.Left, _ 
   e.MarginBounds.Top + i * dgFont.Font.Height) 
Next

4.  Now study the code.  First notice that dgFont is being used to provide the font.  Second notice that the loop iterates through each item in the listbox.  Finally notice that the y-location of each item is based on the loop counter i.  As i increases, the location of y increases.  Run the code and the items in the listbox should be printed on the page.

5.  Now we will add a line to the form just to demonstrate e.graphics.  We will draw the line from the top-left margin to the bottom-right corner of the page.   Add the following line of code to the PrintPage event.  The code can go anywhere in the event but if you put it in the for-loop then it will be processed multiple times which is not necessary.

e.Graphics.DrawLine(New Pen(Color.Aqua), _
e.MarginBounds.Left, _
e.MarginBounds.Top, _
e.PageBounds.Right, _
e.PageBounds.Bottom)

6. Study the code and run the program.  A line should be drawn.

7.  Experiment with the above line of code.  Change some of the locations to numbers and change the color. 

8.  Finally add the following line of code to the PrintPage event:

e.Graphics.FillRectangle(Brushes.Black, 22, 33, 44, 55)

9.  Run your program.  There should be a little rectangle on the page.

10.  Experiment with the code for the rectangle.  It is important to understand what the code is doing so that you can create your own graphics.

Back to top
Exercises

1,  Create a form with the following features:

  1. It has a  menu item that displays the PrintPreviewDialog control when clicked.
  2. It has a TextBox and a NumericUpDown box.
  3. When the user selects PrintPreview:
Back to top

Links & Help
Back to top