在 C# 中打印多页 ListView 时出现问题。想不通
我在打印 listView 项目时遇到问题。 我认为问题出在 private void PrintPageDetail(PrintPageEventArgs e)
这就是正在发生的事情:
它会一遍又一遍地打印一页上适合的任何内容。 (数据比打印的数据多)尽管页脚和页眉打印正确,页码正确。而且我每次都很难让它清除内存以将新数据映射到下一页上。
打印预览和打印不会停止,除非我点击取消(可能是由于无限循环“return;”)。
感谢您的帮助
private void PrintDocument(object sender, PrintPageEventArgs e)
{
// Use inches for all measurements.
e.Graphics.PageUnit = GraphicsUnit.Inch;
PrintPageHeader(e);
PrintPageDetail(e);
PrintPageFooter(e);
}
// Print the page header/footer/details.
private void PrintPageHeader(PrintPageEventArgs e)
{
e.Graphics.DrawString("Factors List Report",
_headerFont, _reportBrush, 3.0F, 0.75F);
e.Graphics.DrawLine(_reportPen, 0.5F, 1.15F, 7.5F, 1.15F);
}
private void PrintPageFooter(PrintPageEventArgs e)
{
e.Graphics.DrawLine(_reportPen, 1.0F, 10.0F, 7.5F, 10.0F);
e.Graphics.DrawString("Printed on " + DateTime.Now.ToLongDateString(),
_footerFont, _reportBrush, 1F, 10.2F);
_pageNumber++;
e.Graphics.DrawString(string.Format("Page: {0}", _pageNumber),
_footerFont, _reportBrush, 7F, 10.2F);
}
private void PrintPageDetail(PrintPageEventArgs e)
{
// Create variables to hold position on page.
float leftMargin = e.MarginBounds.Left;
float topMargin = e.MarginBounds.Top;
float i = 1.25F;
e.HasMorePages = false;
foreach (string printerLine in _factorsView)
{
e.Graphics.DrawString(printerLine, _detailFont, _reportBrush, 1.0F, i);
if (i >= 9.75)
{
i += 1.25F;
e.HasMorePages = true;
return;
}
else
i += .25F;
}
}
}
I am having an issue printing listView items.
the problem I think is at private void PrintPageDetail(PrintPageEventArgs e)
So this is what's going on:
it prints whatever fits on one page over and over. (There is more data than what is printed) although the page footer and header print OK with the right page numbers. and I am having hard time getting it to clear the memory everytime to map the new data on the next page.
the print preview and printing won't stop, unless I hit cancel (probably due to the indefinite loop "return;").
Thank you for any help
private void PrintDocument(object sender, PrintPageEventArgs e)
{
// Use inches for all measurements.
e.Graphics.PageUnit = GraphicsUnit.Inch;
PrintPageHeader(e);
PrintPageDetail(e);
PrintPageFooter(e);
}
// Print the page header/footer/details.
private void PrintPageHeader(PrintPageEventArgs e)
{
e.Graphics.DrawString("Factors List Report",
_headerFont, _reportBrush, 3.0F, 0.75F);
e.Graphics.DrawLine(_reportPen, 0.5F, 1.15F, 7.5F, 1.15F);
}
private void PrintPageFooter(PrintPageEventArgs e)
{
e.Graphics.DrawLine(_reportPen, 1.0F, 10.0F, 7.5F, 10.0F);
e.Graphics.DrawString("Printed on " + DateTime.Now.ToLongDateString(),
_footerFont, _reportBrush, 1F, 10.2F);
_pageNumber++;
e.Graphics.DrawString(string.Format("Page: {0}", _pageNumber),
_footerFont, _reportBrush, 7F, 10.2F);
}
private void PrintPageDetail(PrintPageEventArgs e)
{
// Create variables to hold position on page.
float leftMargin = e.MarginBounds.Left;
float topMargin = e.MarginBounds.Top;
float i = 1.25F;
e.HasMorePages = false;
foreach (string printerLine in _factorsView)
{
e.Graphics.DrawString(printerLine, _detailFont, _reportBrush, 1.0F, i);
if (i >= 9.75)
{
i += 1.25F;
e.HasMorePages = true;
return;
}
else
i += .25F;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是,每次进入 PrintPageDetail(PrintPageEventArgs e) 时,都会从 _factorsView 的开头开始,因为您正在使用 foreach 循环。您将需要跟踪您在该方法之外的位置。像这样的事情:
我不知道 _factorsView 是什么类型,因此索引可能会根据它的类型而改变,但这应该给您一个想法。
The problem is the fact that each time you enter into PrintPageDetail(PrintPageEventArgs e), you start at the beginning of _factorsView because you are using a foreach loop. You will need to instead keep track of where you are outside of this method. Something like this:
I don't know what type _factorsView is so the indexing may change depending on what type it is, but that should give you an idea.