|
C# • Create and Dispose Listing 1. The PrintDocument object exposes several events. BeginPrint is fired before any pages are actually printed, giving you the opportunity to allocate objects or open files. EndPrint occurs after the last page has been printed. Don't forget to bind the events to your PrintDocument object. // At the class level
private Font bodyFont;
private Font headerFont;
private StreamReader data;
private PrintDocument doc;
private void MainForm_Load(object
sender, System.EventArgs e) {
doc = new PrintDocument();
// shows up in Print Manager
doc.DocumentName = "Contact List";
doc.BeginPrint += new
PrintEventHandler(doc_BeginPrint);
doc.EndPrint += new
PrintEventHandler(doc_EndPrint);
}
private void doc_BeginPrint(object
sender, PrintEventArgs pv) {
data = new
StreamReader("contacts.csv");
Font bodyFont = new Font("Arial",
12);
Font headerFont = new Font("Arial",
24);
}
private void doc_EndPrint(object
sender, PrintEventArgs pv) {
data.Close();
bodyFont.Dispose();
headerFont.Dispose();
}
|