Adding a Print Function

When you created the child form template, you included a PrintDocument control to allow for printing functionality. Some simple code will activate this feature. You can learn more about the PrintDocument control and printing in general in Chapter 23, “Creating and Using Reports.”

Coding the Print Menu Command

Open the Code window for frmChild, and then open the Click event handler for the mnuFilePrint menu item. Add the code PrintDocument1.Print() to the procedure; this will cause the PrintDocument control to begin the printing process when mnuFilePrint is clicked. Your event handler should look like the following:

Private Sub mnuFilePrint_Click(ByVal sender As Object, _ 
    ByVal e As System.EventArgs) Handles mnuFilePrint.Click 
    PrintDocument1.Print() 
End Sub 

Coding the PrintDocument Control

A single lineof code in the PrintDocument control’s PrintPage event handler (which occurs when the control’s Print method is invoked) will take care of printing the contents of the txtMain text box to the printer. Enter the following PrintPage event handler for PrintDocument1:

Private Sub PrintDocument1_PrintPage(ByVal sender As Object, _ 
    ByVal e As System.Drawing.Printing.PrintPageEventArgs) _ 
    Handles PrintDocument1.PrintPage 
    e.Graphics.DrawString(txtMain.Text, New Font("Courier New", _ 
        10, FontStyle.Regular), Brushes.Black, 0, 0) 
End Sub 

..................Content has been hidden....................

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