// Http://dotnet.chinaitlab.com/List_233.html
Private void button4_click (Object sender, eventargs E)
{
// For the preview function, you need to add a control: theprintdocument
String strtext = richtextbox1.text;
Stringreader myreader = new stringreader (strtext );
Printpreviewdialog printpreviewdialog1 = new printpreviewdialog ();
Printpreviewdialog1.document = theprintdocument;
Printpreviewdialog1.formborderstyle = formborderstyle. fixed3d;
Printpreviewdialog1.showdialog ();
}
Private void button5_click (Object sender, eventargs E)
{
// Print function. You need to add a control: printdialog.
Printdialog1.document = theprintdocument;
String strtext = richtextbox1.text;
Stringreader myreader = new stringreader (strtext );
If (printdialog1.showdialog () = dialogresult. OK)
{
// This method calls the theprintdocument_printpage event to complete the print task.
Theprintdocument. Print ();
}
}
Private void theprintdocument_printpage (Object sender, printpageeventargs eV)
{
// Implement the True Print function
String strtext = richtextbox1.text;
Stringreader myreader = new stringreader (strtext );
Float linesperpage = 0;
Float yposition = 0;
Int COUNT = 0;
Float leftmargin = eV. marginbounds. Left;
Float topmargin = eV. marginbounds. Top;
String line = NULL;
Font printfont = richtextbox1.font;
Solidbrush mybrush = new solidbrush (color. Black );
// Calculate the number of rows printed per page
Linesperpage = eV. marginbounds. Height/printfont. getheight (EV. Graphics );
// Repeat the stringreader object to print all the content in richtextbox1
While (count <linesperpage & (line = myreader. Readline ())! = NULL ))
{
// Calculate the location of the page on which the next row is to be printed
Yposition = topmargin + (count * printfont. getheight (EV. Graphics ));
// Print the content of the next line in richtextbox1
Ev. Graphics. drawstring (line, printfont, mybrush, leftmargin, yposition, new stringformat ());
Count ++;
}
// Determine whether to print the next page.
If (line! = NULL)
Ev. hasmorepages = true;
Else
Ev. hasmorepages = false;
Mybrush. Dispose ();
}