when i print the items in my listboxes it just keeps going and when it gets to the end of the page, it justs cuts off the rest. how do i get it to go to the next page. This is the code i have

private void docPrint_PrintPage(object sender, PrintPageEventArgs e)
         {
             float linesPerPage = 0;
             float yPos = 0;
             float xPos = 0;
             int count = 0;
             float leftMargin = e.MarginBounds.Left;
             float topMargin = e.MarginBounds.Top;
             string line = null;
             string line2 = null;
             m_objFont = new Font("Arial", 12);


             // Calculate the number of lines per page.
             linesPerPage = e.MarginBounds.Height /
             m_objFont.GetHeight(e.Graphics);

             // Print each line of the file.
             foreach (Control objControl in this.Controls)
             {
                 if (objControl.GetType().Name != "Button" && objControl.GetType().Name != "MenuStrip" && objControl.Name != "lblItemName" && objControl.Name != "lblItemValue" )
                 {
                     line = objControl.Text;

                     switch (objControl.Name)
                     {
                         case "lblSum":
                             e.Graphics.DrawRectangle(Pens.Black, lblSum.Location.X + leftMargin, lblSum.Location.Y + topMargin - 4, lblSum.Width, lblSum.Height);
                             m_objFont = objControl.Font;
                             break;

                         default:
                             m_objFont = objControl.Font;
                             m_objFont = new Font("Arial", 12);
                             break;

                     }
                     xPos = leftMargin + objControl.Location.X;
                     yPos = topMargin + objControl.Location.Y;
                    
                     e.Graphics.DrawString(line, m_objFont, Brushes.Black, xPos, yPos);
                 }
             }

             

             while (count < linesPerPage && nextItem < lstResult.Items.Count && nextItem < lstResult.Items.Count)
             {
                 m_objFont = new Font("Arial", 12, FontStyle.Underline);
                 yPos = topMargin + 210 + (count *
                 m_objFont.GetHeight(e.Graphics));
                 line = lstResult.Items[nextItem].ToString();
                 line2 = lstResult2.Items[nextItem].ToString();

                 e.Graphics.DrawString(line, m_objFont, Brushes.Black,
                 leftMargin, yPos, new StringFormat());
                 
                 e.Graphics.DrawString(line2, m_objFont, Brushes.Black, leftMargin + 500, yPos, new StringFormat());
                 count++;
                 nextItem++;
             }
                    
                     // If more lines exist, print another page.
                     if (nextItem < lstResult.Items.Count - 1 && nextItem < lstResult2.Items.Count - 1)
                         e.HasMorePages = true;
                     else
                     {
                         e.HasMorePages = false;
                         nextItem = 0;
                     }
                 }
         
         
     

                         
                     
                 



             
             
         
         private void mnuFilePrint_Click(object sender, EventArgs e)
         {
             PrintDocument docPrint = new PrintDocument();
             docPrint.PrintPage += new PrintPageEventHandler(docPrint_PrintPage);

             if (PrinterSettings.InstalledPrinters.Count == 0)
             {
                 ErrorMessage();
                 return;
             }
             docPrint.Print();
         }

         private void mnuFilePrintPreview_Click(object sender, EventArgs e)
         {
             PrintDocument docPrint = new PrintDocument();

             docPrint.PrintPage += new PrintPageEventHandler(docPrint_PrintPage);
             if (PrinterSettings.InstalledPrinters.Count == 0)
             {
                 ErrorMessage();
                 return;
             }
             objPreview.Document = docPrint;
             objPreview.ShowDialog();
         }
         void ErrorMessage()
         {
             MessageBox.Show("No printers istalled. You must" + "have a printer installed to preview or print" + "the document.", "Print Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
         }
        

       

       
     
       
    }
}

I looked at the tutorial but i still dont know why it doesnt go to the next page. my code is in the first post

i looked at that thread that you posted but its still unclear to me whats wrong with my code.

in the if/else statement

//If more lines exist, print another page.                    
 if (nextItem < lstResult.Items.Count - 1 && nextItem < lstResult2.Items.Count - 1) 
    e.HasMorePages = true;       
 else                    
 {                        
   e.HasMorePages = false;                    
     nextItem = 0;     
 }

when i use this i dont get the next page. when i used what it said in the other thread, i get an infinate number of pages. i dont know what to put to make it so that it doesnt go on for an infinate number of pages.

Edited 5 Years Ago by steven8579: n/a

i tried changing the code to what was on the other thread but it just results in an infinate number of pages

does anyone know whats wrong with this code? i dont want it to run on endlessly without going to the next page.

1. Do not reset nextItem variable.

2. Want set top margin then set height of document.

float topMargin = e.MarginBounds.Top + 200;

linesPerPage = (e.MarginBounds.Height - 200) / m_objFont.GetHeight(e.Graphics);

 while (count < linesPerPage && linesprinted < totallines)
          {
         yPos = topMargin  + (count * m_objFont.GetHeight(e.Graphics));
         ....
          }

Have a look at sample app.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Printing;
namespace printmulti
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //Adding 200 lines of text into listbox.
            for (int i = 1; i <= 200; i++)
            {
                listBox1.Items.Add("Item #" + i);
            }
        }


        Font m_objFont;
        int totallines = 0;    //Total lines to be print
        int linesprinted = 0;  //printed lines count

        //Print - Button
        private void button1_Click(object sender, EventArgs e)
        {
            PrintDocument pd = new PrintDocument();
            totallines = listBox1.Items.Count;
            linesprinted = 0;
            m_objFont = new Font("Arial", 12);

            pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
            pd.Print();
        }

        void pd_PrintPage(object sender, PrintPageEventArgs e)
        {
            float linesPerPage = 0;
            float yPos = 0;
            int count = 0;
            string line = "";

            float leftMargin = e.MarginBounds.Left;
            float topMargin = e.MarginBounds.Top + 200;


            linesPerPage = (e.MarginBounds.Height - 200) / m_objFont.GetHeight(e.Graphics);


            while (count < linesPerPage && linesprinted < totallines)
            {
                yPos = topMargin + (count * m_objFont.GetHeight(e.Graphics));

                line = listBox1.Items[linesprinted].ToString();

                e.Graphics.DrawString(line, m_objFont, Brushes.Black,
                leftMargin, yPos, new StringFormat());

                count++;
                linesprinted++;
            }

            if (linesprinted < totallines)
                e.HasMorePages = true;
            else
                e.HasMorePages = false;

        }

        //Preview - Button
        private void button2_Click(object sender, EventArgs e)
        {
            PrintDocument pd = new PrintDocument();
            totallines = listBox1.Items.Count;
            linesprinted = 0;
            m_objFont = new Font("Arial", 12);
            PrintPreviewDialog preview = new PrintPreviewDialog();


            pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
            preview.Document = pd;
            preview.ShowDialog();
        }
    }
}
This question has already been answered. Start a new discussion instead.