المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : PrintDoc PrintPreview problem with random numbers



C# Programming
09-13-2009, 11:17 PM
I have an application which involves creating a set of randomly generated numbers that are included in the document to be printed.

Prior to printing I am displaying the document(s) in a PrintPreview form constructed with a PrintPreviewControl.
There is a button on the form which calls up a printer dialog and the document is then printed on ok click on the printer dialog after pages to print selection etc.

The problem I am having is that, to display the document in the PrintPreview form the random numbers are generated in the calling form and the printDoc_PrintPage function then formats the document using the random numbers that have been put in an array.

This is fine.

But when I click to print the document, the printDoc_PrintPage function is called again to generate the document for the printer but the array of random numbers is now cleared and so an error is thrown.

I could put the generation of the random numbers within the the printDoc_PrintPage function, but the output would then be different between the printPreview display and the printed paper document.

I don't want to have to pass the array back and forth between the forms.
The printPreview form is used for assorted documents in various places in the application and should be kept generic and should only need a printDocument.

Does anyone have any suggestions as to how I might get around this problem please?

I would think many people would like to have a 'completed' document to pass around but it seems that the only option is to create the document on the fly for each call to print.

I can't see how you can achieve this without persisting the document to a file.

I might be missing something simple here or misunderstanding how to use these components but I can't find any examples of similar use.

Thanks