I am having alot of trouble printing a string with the printdocument
method.
I have a report of invalid entries in the form of a string. I build this string by concatenating entries to it while inside a forloop of the invalid entries. It looks like this
foreach(Error entry in ErrorEntries)
reportString += entry.ToString();
Now I access this string from the printdocumen
t method (it's a singleton). The trouble is that the string has about 300 entries so does not fit onto one page.
Let's say it prints the first 30 records. I am having trouble with the e.HasMorePages = true
command as from what I understand, it reruns the rpintdocument1 method. If that's the cas开发者_如何学JAVAe, then the method will just print the reportstring from top to bottom again stopping at the 30th record.
Is there a way to delete the line I just printed from the reportSummary string so the next time the printdocument method runs, it's not printing the same contents of the string (the beginning 30 records)?
What you can do is use a field in the class where your printdocument
method lives to store how many records you've managed to print so far:
EDIT: Updated example code to match your situation
class YourClass {
private int charactersPrinted = 0;
...
private void printdocument(object sender, PrintPageEventArgs ev) {
var charactersDoneThisPage =
this.PrintReportStringPart(reportString.Substring(charactersPrinted));
charactersPrinted += charactersDoneThisPage; // Number of characters you managed to print this page
if (charactersPrinted < reportString.Length) {
ev.HasMorePages = true;
} else {
ev.HasMorePages = false;
}
}
// Prints the string to the page
// Returns the number of characters it was actually able to print
private int PrintReportStringPart(string reportStringPart) {
// Print the reportStringPart however you're already doing it
// filling the page
return charactersDoneThisPage;
}
}
What about not concatenating a single string and instead pass the list of errors to your print method and keep an index of the first record you need to print from... that way the next time the print page method is called, you start from the point where your last printed (+1)
精彩评论