gerrysb (3) [Avatar] Offline
#1
I've developed code that loops through checked nodes in a treeview to determine which images the user would like to print. This loop calls the PrintPage event handler for each image and successfully prints each image.

private void btnPrint_Click(object sender, System.EventArgs e)
{
using (PrintDialog dlg = new PrintDialog())
{
dlg.Document = this.printDoc;

if (dlg.ShowDialog() == DialogResult.OK)
{
// Iterate through all nodes
foreach (TreeNode node in treeViewDocs.Nodes)
{
for (int i = 0; i < node.Nodes.Count; i++)
{
if (node.Nodes[i].Tag != null)
{
if (node.Nodes[i].Checked)
{
// Assign node to document name
docName = node.Nodes[i].Text + ".jpg";
tabName = node.Text;

// Assign node and draw document
AssignDoc(node.Nodes[i]);

// Print document
printDoc.DocumentName = node.Nodes[i].Text;
printDoc.Print();
}
}
}
}
}
}
}

However, I'm struggling to figure out how to send all the selected images to be printed to the Print Preview Dialog (looping through the treeview in the Print Preview click event only shows the last image selected in the Print Preview Dialog).

How can I send multiple image files to the Print Preview Dialog?

Thanks.
eebrown (89) [Avatar] Offline
#2
Re: Print previewing multiple images
Gerry,

What your doing is correct, but perhaps not the best way to approach this. By performing all of the printing in your button's Click handler, you lose out on the other features, such as pages....

Look up the PrintPage event for the PrintDocument class. This event receives a PrintPageEventArgs parameter that allows, among other things, the ability to have multiple pages. Its in section 18.1.2 on page 607 of my book.

In this handler, print a single image and set the HasMorePages property to true. The event will be called for each page, so you can print the whole set of images to a printer or a preview dialog.

Enjoy,

Erik
gerrysb (3) [Avatar] Offline
#3
Re: Print previewing multiple images
Erik,

Figured out how to add multiple images to the print preview dialog.

1. Added each image name to an arraylist
2. In the print preview dialog printpage event handler
- Drew the first image to the print preview dialog
- Removed the first image from the arraylist
- Checked for arraylist count - if > 0, e.HasMorePages = true else e.HasMorePages = false

However, now I have another problem. When the Print button is clicked in the print preview dialog, it tries to access the same printpage event handler above and bombs out since the images have been removed from the arraylist.

Is there a way to set up another printpage event handler that would only be accessed from the Print button in the print preview dialog (without starting from scratch using the Print Preview Control)?

Thanks.
gerrysb (3) [Avatar] Offline
#4
Re: Print previewing multiple images
Erik,

Figured it out. I defined a count variable outside of the printpage event handler equal to the arraylist count. Instead of removing each image from the arraylist while drawing to the print preview dialog in the printpage event handler, I decremented the count variable.

When the count variable gets to 0, e.HasMorePages = false in the printpage event handler, and the count variable is reset to the arraylist count.

Works great now!

Thanks for your help.
eebrown (89) [Avatar] Offline
#5
Re: Print previewing multiple images
Gerry,

Great! Glad you figured it out. Another approach would be to handle the BeginPrint or EndPrint events, which allow you to do something at the beginning or end of printing the document. You could set/reset your counter their as well.

Enjoy,

Erik