在 C# 中打印多页 ListView 时出现问题。想不通

发布于 2024-08-11 00:49:33 字数 1958 浏览 7 评论 0原文

我在打印 listView 项目时遇到问题。 我认为问题出在 private void PrintPageDetail(PrintPageEventArgs e) 这就是正在发生的事情:

  1. 它会一遍又一遍地打印一页上适合的任何内容。 (数据比打印的数据多)尽管页脚和页眉打印正确,页码正确。而且我每次都很难让它清除内存以将新数据映射到下一页上。

  2. 打印预览和打印不会停止,除非我点击取消(可能是由于无限循环“return;”)。

感谢您的帮助

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;
        }
    }
}

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;
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

稳稳的幸福 2024-08-18 00:49:33

问题是,每次进入 PrintPageDetail(PrintPageEventArgs e) 时,都会从 _factorsView 的开头开始,因为您正在使用 foreach 循环。您将需要跟踪您在该方法之外的位置。像这样的事情:

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;  
        }  
    }  

我不知道 _factorsView 是什么类型,因此索引可能会根据它的类型而改变,但这应该给您一个想法。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文