Question

I'm printing a grid containing several rows in Silverlight. Whenever the grid height is more than the PrintableArea I set HasMorePages to true and break, else HasMorePages is set to False. This code however causes an infinite loop, for the PrintPage event keeps being called again and again although the "HasMorePages" is false.

 private void PrintButton_Click(object sender, RoutedEventArgs e)
 {
        PrintDocument document = new PrintDocument(); 
        // create a copy of the MDivReq form by calling the constructor with the same parameters
        Common.MDivDegreeReq mymdiv = scrllvwr.Content as Common.MDivDegreeReq;


        int totalrows =mymdiv.LayoutRoot.RowDefinitions.Count;

       document.PrintPage +=(s,args) =>
           {

               Grid GridToBePrinted = new Grid();
               GridToBePrinted.Height = 0;

               for (int i = 0; i < mymdiv.LayoutRoot.RowDefinitions.Count; i++ )
               {
                   // if GridToBePrinted height + this rows height is less than the PrintableArea heigh
                   // then add this row to the gridtobeprinted.
                   if (GridToBePrinted.Height + mymdiv.LayoutRoot.RowDefinitions[i].ActualHeight + 20 < args.PrintableArea.Height)
                   {
                       RowDefinition myrow = new RowDefinition();
                       myrow = mymdiv.LayoutRoot.RowDefinitions[i];
                       //myrow = mymdiv.LayoutRoot.RowDefinitions[i].MemberwiseClone();
                       i--;
                       mymdiv.LayoutRoot.RowDefinitions.Remove(myrow);
                       GridToBePrinted.RowDefinitions.Add(myrow);
                       GridToBePrinted.Height += myrow.ActualHeight;

                       if (mymdiv.LayoutRoot.RowDefinitions.Count == 0)
                       {
                           break;
                       }
                       args.HasMorePages = false;

                   }
                   else
                   {
                       if (mymdiv.LayoutRoot.RowDefinitions[i].ActualHeight > args.PrintableArea.Height)
                       {
                           mymdiv.LayoutRoot.RowDefinitions.RemoveAt(i);
                           continue;
                       }
                       args.PageVisual = GridToBePrinted;
                       args.HasMorePages = true;
                       //i++;
                       break;
                   }
               }

           };
       document.Print(mystudent + " - MDiv Requirements");


    }

The Code is stuck as follows: At the end when all the mymdiv.LayoutRoot.RowDefinitions.Count=0 , PrintPage is called again and checks the forloop and finds that it doesn't fulfill the condition, then it goes to the beginning of PrintPage once again and continues to do that forever.

How can I stop this loop ?

Was it helpful?

Solution

Solution: The problem was that although "HasMorePages" was false, but the "PageVisual" had a null value, so it called the PrintPage again. Fixing the PageVisual value stopped the loop.

Improvement for my code: Used the suggestion by "DNKROZ" to set the HasMorePages to false one instead of on every iteration.

OTHER TIPS

Howcome args.HasMorePages = false; is being set on every iteration?

Should it not be set inside this if statement:

 if (mymdiv.LayoutRoot.RowDefinitions.Count == 0)
 {
    args.HasMorePages = false;
    break;
 }

So then it reads, if there are no rows left to print - set hasMorePages to false and exit the for loop, then print the page.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top