namespace Microsoft.Samples.WinForms.Cs.PrintingExample3 { using System; using System.ComponentModel; using System.Windows.Forms; using System.Drawing; using System.Drawing.Printing; using System.IO; public class PrintForm : System.Windows.Forms.Form { /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.Container components; protected internal System.Windows.Forms.Button printButton; public PrintForm() { // // Required for Windows Form Designer support // InitializeComponent(); // Wire up event handler for print button printButton.Click += new System.EventHandler(printButton_Click); } /// <summary> /// Clean up any resources being used. /// </summary> protected override void Dispose(bool disposing) { if (disposing) { if (components != null) { components.Dispose(); } } base.Dispose(disposing); } //Event fired when the user presses the print button private void printButton_Click(object sender, EventArgs e) { try { StreamReader streamToPrint = new StreamReader ("PrintMe.Txt"); try { TextFilePrintDocument pd = new TextFilePrintDocument(streamToPrint); //Assumes the default printer PrintDialog dlg = new PrintDialog() ; dlg.Document = pd; DialogResult result = dlg.ShowDialog(); if (result == DialogResult.OK) { pd.Print(); } } finally { streamToPrint.Close() ; } } catch(Exception ex) { MessageBox.Show("An error occurred printing the file - " + ex.Message); } } /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.components = new System.ComponentModel.Container (); this.printButton = new System.Windows.Forms.Button (); this.Text = "Print Example 3"; this.AutoScaleBaseSize = new System.Drawing.Size (5, 13); this.ClientSize = new System.Drawing.Size (504, 381); printButton.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft; printButton.FlatStyle = System.Windows.Forms.FlatStyle.Flat; printButton.Size = new System.Drawing.Size (136, 40); printButton.TabIndex = 0; printButton.Location = new System.Drawing.Point (32, 112); printButton.Text = "Print the file"; this.Controls.Add (this.printButton); } /// <summary> /// The main entry point for the application. /// </summary> [STAThread] public static void Main(string[] args) { Application.Run(new PrintForm()); } } } Mohamed Mahfouz MEA Developer Support Center ITworx on behalf of Microsoft EMEA GTSC
Don't see what you're looking for? Try a search.
|