Hello,
I'm having newby difficulties printing from a richtextbox to a printer.
I have built a small application (Like part of notepad) with file load and print.
I can load file or write to the textbox, but when printing (using the PrintDialog to select and set the printer) I always get an empty page from the default printer.
Can someone show me how to make this printing and print previw work? (See attched code).
Why do I always get my default printer, even when selecting another network printer?
Sam
using System; using System.Windows.Forms; using System.IO; using System.Drawing.Printing; namespace PrintTextBox { public partial class Form1 : Form { internal string MyFileName; internal bool newfile; internal bool open; public Form1() { InitializeComponent(); this.Show(); } private void richTextBox1_TextChanged(object sender, EventArgs e) { } private void newToolStripMenuItem_Click(object sender, EventArgs e) { this.richTextBox1.Clear(); newfile = true; Text = "PrintTextBox"; } private void opentoolStripMenu_Click(object sender, EventArgs e) { try { OpenFileDialog dlgOpen = new OpenFileDialog(); dlgOpen.DefaultExt = "Text files |*.txt"; dlgOpen.Filter = "Text files |*.txt|RTF Files (*.rtf)|*.rtf|All files |*.*"; dlgOpen.Multiselect = true; if (dlgOpen.ShowDialog() == DialogResult.OK) { this.MyFileName = Text = dlgOpen.FileName; //Display and remember the open file name for saving StreamReader streamReader = new StreamReader(dlgOpen.FileName); this.richTextBox1.Text = streamReader.ReadToEnd(); streamReader.Close(); open = true; newfile = false; } } catch (ArgumentException) { } catch (IOException) { } catch (FormatException) { } finally { } } private void PrintoolStripMenut_Click(object sender, EventArgs e) { PrintDialog dlgPrint = new PrintDialog(); if (dlgPrint.ShowDialog() == DialogResult.OK) { //Print a document or print the textbox
PrintDocument printdoc = new PrintDocument(); printdoc.DocumentName = "MyDocument.txt"; printdoc.Print(); //Prints an empty page!!!! // // or print the richTextBox ???? // } } private void PreviewtoolStripMenu_Click(object sender, EventArgs e) { PrintPreviewDialog dlgPreview = new PrintPreviewDialog(); if (dlgPreview.ShowDialog() == DialogResult.OK) { //same problem as print. } } private void ExittoolStripMenu_Click(object sender, EventArgs e) { Application.Exit(); } } }
|