RichTextBox Printing problem?

  • Thread starter Thread starter Torben Philippsen
  • Start date Start date
T

Torben Philippsen

Hello,

I was reading the article "How To Print the Content of a RichTextBox Control
By Using Visual C# .NET" from
http://support.microsoft.com/default.aspx?scid=kb;en-us;812425 but I am
having some problems.

I am am able to create a method that prints the content of the RichTextBox.
When changing eg. the paper direction from portrait (the default) to
landscape, my text is still printed in portrait. It seems that the
printmethod isn't affected by the changes made in the PrintDialog.

What am I missing?
If you should have any other links on how to simple print content from a
RichTextBox, it would be great as well.

Below you will find my print method:

//prints the content of the richtextbox
public void print()
{
//create a new PrintDocument
PrintDocument printDocument = new PrintDocument();
printDocument.BeginPrint += new PrintEventHandler(printDocument_BeginPrint);
printDocument.PrintPage += new
PrintPageEventHandler(printDocument_PrintPage);
//if no printersettings was stored previously, create a new instance
if (storedPrinterSettings == null)
{
storedPrinterSettings = new PrinterSettings();
}
//create a new printdialog
PrintDialog printDialog = new PrintDialog();
printDialog.AllowSelection = true;
printDialog.AllowSomePages = true;
if (storedPrinterSettings != null)
{
printDialog.PrinterSettings = storedPrinterSettings ;
}
if (printDialog.ShowDialog() == DialogResult.OK)
{
printDialog.PrinterSettings = printDialog.PrinterSettings; //I think my
problem lies here, how do I read the changes?
printDocument.Print();
}
}

Thank you.
Torben
 
Hi!

Finally I got it right.

The missing thing was to set the printersettings of the PrintDocument to
those returned from the PrintDialog.

The final code is here:

//prints the content of the richtextbox
public void print()
{
//create a new PrintDocument
PrintDocument printDocument = new PrintDocument();
printDocument.BeginPrint += new
PrintEventHandler(printDocument_BeginPrint);
printDocument.PrintPage += new
PrintPageEventHandler(printDocument_PrintPage);
//if no printersettings was stored previously, create a new instance
if (storedPrinterSettings == null)
{
storedPrinterSettings = new PrinterSettings();
}
//create a new printdialog
PrintDialog printDialog = new PrintDialog();
printDialog.AllowSelection = true;
printDialog.AllowSomePages = true;
if (storedPrinterSettings != null)
{
printDialog.PrinterSettings = storedPrinterSettings ;
}
if (printDialog.ShowDialog() == DialogResult.OK)
{
printDocument.PrinterSettings = printDialog.PrinterSettings;
storedPrinterSettings = printDialog.PrinterSettings;
printDocument.Print();
}
}
 
Back
Top