C# e.Hasmorepage is in Eternal Loop

Asked

Viewed 362 times

1

I am creating a system for printing labels as follows the code:

private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{
    Image newImage;
    Point ulCorner;
    int x = 0;
    int xn = 0;
    int y = 0;
    int yn = 0;
    bool xativo = false;

    int n = 14;
    int n1 = 0;
    int yPage = 0;

    for (int i = 0; i < lstAdesivos.Items.Count; i++)
    {
        newImage = Image.FromFile(@path + "\\Modelos\\" + lstAdesivos.Items[i].Text + ".png");
        if (xativo == true)
        {
            x = 400;
            y = 0;
            xativo = false;
        }

        ulCorner = new Point(x, y);
        yPage += newImage.Size.Height;

        if (yPage > e.PageBounds.Height)
        {
            e.HasMorePages = true;
            yPage = 0;
        }

        e.Graphics.DrawImage(newImage, ulCorner);

        if (yn == nudCartelasColuna.Value - 1)
        {
            yn = 0;
            xativo = true;
        }
        else
        {
            y += 200;
            yn += 1;
        }
    }

    Font fonte = new Font("Verdana", 12);
    e.Graphics.DrawString(tbDescricao.Text, fonte, Brushes.Black, 400, 1050);
}

What happens is that instead of just creating a new page it keeps creating many endlessly, I’d like to know what I’m doing wrong?

  • 1

    Where is the "e" variable declared? Is this chunk of code inside an event? would that be windows Forms, wpf, Asp.net, Asp.net MVC? be more specific please. Also take a look at this page [Ask]

  • Win Form, inside the printDocument1_PrintPage(), I changed the code on top.

4 answers

1

It’s a logic problem that, I think you don’t quite understand how the printing process works with Printdocument.

Like you said, he goes back in PrintPage, and this is what is expected.

This event, PrintPage will be called once for each page if when it finishes executing the value of e.HasMorePages is equal to true he will be called again.

What you need is to know which images have already been printed each time the event is called.

Basically you print what fits on the page, the time that no longer fits only that there are still more things to be printed e.HasMorePages = true and leaves the function, then it will be called again and you continue printing what is missing on the new page from where you left off, repeating the process if necessary.

How to do this there are several ways, you will probably need some value in the class scope to mark where you left off and know where to continue.

0

Try it on there

    if (yPage > e.PageBounds.Height)
    {
        e.HasMorePages = true;
        yPage = 0;
    }else{
        e.HasMorePages = true;

    }

0

My "kick" is that with each iteration of the block you are increasing the lstAdesivo.items.Count, would it be possible? I didn’t see the definition of it in the code and risk that if you are in an infinite loop, with each iteration Count goes up a unit. You can test and report?

Looking up, your code doesn’t seem wrong to me, so it’s the only thing I can think of.

0

So the for is working normally, but one thing I realized is that after finishing the for it starts printDocument1_PrintPage again. So get in the for again.

I’m calling it that way:

printPreviewDialog1.Document = this.printDocument1;

printPreviewDialog1.Showdialog();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.