简体   繁体   English

使用 C# PrintDocument 和 PrintPage 事件打印嵌套循环

[英]Printing nested loops using C# PrintDocument and the PrintPage event

I am writing a C# WinForms app, and part of it includes generating inventory reports.我正在编写一个 C# WinForms 应用程序,其中一部分包括生成库存报告。 I can't print the inventory one item per line, because each inventory item may be located in more than one location.我无法每行打印一个库存项目,因为每个库存项目可能位于多个位置。 So, the goal is to print the name of the product (and a few other details) one one line, and below it, print a list of locations where the item is located and how many should be in each location, basically like this:因此,我们的目标是一行打印产品名称(以及一些其他详细信息),然后在其下方打印一份物品所在位置的列表以及每个位置应该有多少个,基本上是这样的:

WIDGET (ACME, INC)           PRODUCT #123435       TOTAL ON HAND: 30
    Aisle: 10              Count: 15
    Aisle: 4               Count: 6
    Aisle: 15              Count: 9

SNIPE (ACE SNIPE CO)         PRODUCT #67890        TOTAL ON HAND: 11
    Aisle: 1               Count: 4
    Aisle: 3               Count: 7

and so on...等等...

The problem I'm running into is, to print this requires a loop within a loop - the outer loop is the list of products, and the inner loop is the list of locations and counts for each product.我遇到的问题是,打印这需要一个循环中的一个循环——外循环是产品列表,内循环是每个产品的位置和计数列表。 What I can't figure out is how to cause a page break when I am inside the inner loop and hit the bottom of the page.我想不通的是当我在内循环中并到达页面底部时如何导致分页。 What needs to happen is for the printing routine to print a new page header but then pick up where it left off in the list of locations for the product it was printing.打印例程需要做的是打印一个新页面 header,然后在它正在打印的产品的位置列表中从它停止的地方开始。

The printing code I've written works well except I can't figure out how to catch hitting the bottom of the page to start a new page while inside the inner loop[, so the printing runs off the bottom of the page.我写的打印代码运行良好,除了我无法弄清楚如何在内部循环中抓住页面底部以开始新页面[,所以打印从页面底部开始。

So the question is, when you have a loop inside another loop while printing , how do you check page position and start a new page that continues where you left off?所以问题是,当您在打印时在另一个循环中有一个循环时,您如何检查页面 position 并从您中断的地方开始一个新页面? I'd be glad to provide the code I have for this so far, but I thought the explanation of the problem might be easier for people to understand and provide some guidance on.我很乐意提供到目前为止我拥有的代码,但我认为对问题的解释可能更容易让人们理解并提供一些指导。 I have been banging my head against the wall on this for a couple days, and nothing I've found anywhere on the web addresses this.几天来,我一直在用头撞墙,但我在 web 上的任何地方都找不到解决这个问题的方法。

Thanks for any help that is given.感谢您提供的任何帮助。

The focus is on the PrintPage event, which has a HasMorePages property.重点是 PrintPage 事件,该事件具有 HasMorePages 属性。 If this property is true, he will open a new page and execute the custom event bound to PrintPage again.如果这个属性为true,他会打开一个新的页面,并再次执行绑定到PrintPage的自定义事件。 So it is equivalent to a recursion, you need to return this method at the right time.所以相当于一个递归,需要在合适的时候返回这个方法。 Finally, set HasMorePages to false so it won't recycle.最后,将 HasMorePages 设置为 false,这样它就不会被回收。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM