"john sutor" <john_sutor@NOSPAM> wrote in message
news:eg**************@TK2MSFTNGP09.phx.gbl...
I am trying to print a simple document (.txt) from my drive. I tried
using the following code to no avail. This cannot be that hard. In VB this was
easy, Why does C# make it so hard? Can anyone help me solve this. I ran
the code from the clikc event of a button
PrintDocument prnDc = new PrintDocument();
prnDc.DocumentName="c:\\Test1.txt";
prnDc.Print();
OK, you need all that, but you're not done yet. You need to lay out the text
on the page yourself. To do that, you need to handle the PrintPage event of
the document, so
prnDoc = new PrintDocument();
prnDoc.PrintPage += new
System.Drawing.Printing.PrintPageEventHandler(prnD oc_PrintPage);
Then you have
private void prnDoc_PrintPage(object sender,
System.Drawing.Printing.PrintPageEventArgs e)
{
//Here you open your text file yourself, and draw the text, or
whatever you want
//to the graphics object which is a property of the PrintPageEventArgs
e argument.
}
When you call prnDoc.Print() .NET creates a graphics object based on the
selected printer, margins, and all that, and then calls your
prnDoc_PrintPage method, giving you what you need to layout your stuff. Then
at the end of the method, sends the output to the printer.
The name of the document is merely what shows in the printers dialog box..
It does NOT open a file and print anything. Actually, it's not so hard, just
quite different than how it was done in VB6.
HTH,
Eric