I have richtextbox and preview dialog.
when I want to make a preview I want to see all the pages of the richtextbox but now I can see only the fist page many times.
please help me
char[] param = { '\n' };
string [] lines = {};
if (pd.PrinterSettings.PrintRange == PrintRange.Selection)
{
lines = rtb.SelectedText.Split(param);
}
else
{
lines = rtb.Text.Split(param);
}
int i = 0;
char[] trimParam = { '\r' };
foreach (string s in lines)
{
lines[i++] = s.TrimEnd(trimParam);
}
int linesPrinted = 0;
int x = e.MarginBounds.Left;
int y = e.MarginBounds.Top;
Brush brush = new SolidBrush(rtb.ForeColor);
while (linesPrinted < lines.Length)
{
e.Graphics.DrawString(lines[linesPrinted++],
rtb.Font, brush, x, y);
y += 15;
if (y >= e.MarginBounds.Bottom)
{
e.HasMorePages = true;
return;
}
else
{
e.HasMorePages = false;
}
}
Because if you have this method in the print page even, every time that a new page is printed you read again the content of the RichTextBox :
And you start again from the beginning …
So you have to read the RichTextBox content only in the first page …
To resolve this problem you can for example declare a variable outside of the method :
And externalize from the method the variables :
The new code become (I haven’t tested it – is only a proof of concept) :