Hi, i'm having a problem trying to print out a list of strings. i can get them to print out using rectangles successfully but when the bottom of the page is reached it wont go onto the next page, instead it just stops printing.

I've done this so far which just repeats the first page over and over rather than just printing the rest of the text on a new page:

private void document_PrintPage(object sender, PrintPageEventArgs e)
        {
            List<string> propNames = new List<string>();
            List<string> propValue = new List<string>();

            Font titleFont = new Font("Arial", 22, System.Drawing.FontStyle.Regular);
            string title = "Object State Default";

            RectangleF rect = new RectangleF();
            rect.X = 50.0F;
            rect.Y = 30.0F;
            rect.Width = 300.0F;
            rect.Height = 50.0F;
            SolidBrush brush = new SolidBrush(Color.Black);
            Pen blackpen = new Pen(Color.Transparent);            
            Font printFont = new Font("Arial", 11, System.Drawing.FontStyle.Regular);
            //Gets names of all properties
            propNames = printObject(objtyp);
            //Gets values of all properties
            propValue = getPrintValues();
            //Draws title of the page
            e.Graphics.DrawString(title, titleFont, brush, rect);
            //Sets position for next rectangle
            rect.Y = rect.Y + 75;

            for (int i = 0; i < propNames.Count && i < propValue.Count; i++)
            {
                e.Graphics.DrawString(propNames.ElementAt(i), printFont, brush, rect);
                rect.X = rect.X + 300;
                e.Graphics.DrawString(propValue.ElementAt(i), printFont, brush, rect);
                rect.Y = rect.Y + 30;
                   
                if (rect.Y >= 1100)
                {
                    e.HasMorePages = true;
                }
                else
                {
                    e.HasMorePages = false;
                }
            }            
        }

Any help with this would be great !

Thanks

Chris

1. Put the code that does the printing into a seperate method that returns a bool (True = there are more pages to print)
2. Change document_PrintPage to call it, setting e.HasMorePages to the result

private void document_PrintPage(object sender, PrintPageEventArgs e)
{
e.HasMorePages = DoPrint(e.Graphics);
}

        private bool DoPrint(Graphics g)
        {
            List<string> propNames = new List<string>();
            List<string> propValue = new List<string>();

            Font titleFont = new Font("Arial", 22, System.Drawing.FontStyle.Regular);
            string title = "Object State Default";

            RectangleF rect = new RectangleF();
            rect.X = 50.0F;
            rect.Y = 30.0F;
            rect.Width = 300.0F;
            rect.Height = 50.0F;
            SolidBrush brush = new SolidBrush(Color.Black);
            Pen blackpen = new Pen(Color.Transparent);
            Font printFont = new Font("Arial", 11, System.Drawing.FontStyle.Regular);
            //Gets names of all properties
            propNames = printObject(objtyp);
            //Gets values of all properties
            propValue = getPrintValues();
            //Draws title of the page
            g.DrawString(title, titleFont, brush, rect);
            //Sets position for next rectangle
            rect.Y = rect.Y + 75;
            int i = lastIndex;
            //i = lastIndex;
            while (i < propValue.Count)
            {

                //e.Graphics.DrawRectangle(blackpen, x, y, width, height);
                g.DrawString(propNames.ElementAt(i), printFont, brush, rect);
                rect.X = rect.X + 300;
                g.DrawString(propValue.ElementAt(i), printFont, brush, rect);
                rect.X = 50.0F;
                rect.Y = rect.Y + 30;
                //&& i < propNames.Count && i < propValue.Count
                if (rect.Y >= 1130)
                {
                    lastIndex = i;
                    return true;
                    //rect.Y = 40.0F;
                }
                //else
                //{
                //    return false;
                //}
                i++;
            }
            return false;
        }
commented: Extremely helpful just what i needed +1

Thank you this worked perfectly

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.