views:

489

answers:

1

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:

  1. 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.

  2. 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;
        }
    }
}
A: 

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:

int myLocation = 0;

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;  
        while(myLocation < _factorsView.Length)  
        {  
            e.Graphics.DrawString(_factorsView[myLocation], _detailFont, _reportBrush, 1.0F, i);  
            myLocation++;


            if (i >= 9.75)  
            {  
                i += 1.25F;  
                e.HasMorePages = true;  
                return;  
            }  
            else  
                i += .25F;  
        }  
    }

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.

KrisTrip