Matthew Somers
Matthew Somers

Reputation: 27

Trying to create a new page when horizontal pos. of i extends past right margin

I am trying add a page when horizontal or the x position is greater than a counter in order to keep a right side margin. When I run the code I end up in an infinate loop of hundreds of pages all displaying the same first page graphics. Thinking it might have to do with my lack of understanding HasMorePages. I could use some help. Thanks.

    public static class PrintWave
{
    public static void PrintPreWave()
    {

        PrintDocument pd = new PrintDocument();

        if (WaveTools.MySettings == null)
        {
            pd.DefaultPageSettings.Landscape = true;
        }
        else
        {
            pd.DefaultPageSettings = WaveTools.MySettings;
        }
        pd.OriginAtMargins = true;

        pd.PrintPage += new PrintPageEventHandler(OnPrintPage);
        PrintDialog dlg = new PrintDialog();
        PrintPreviewDialog printPreviewDlg = new PrintPreviewDialog();
        printPreviewDlg.Document = pd;

        Form p = (Form)printPreviewDlg;
        p.WindowState = FormWindowState.Maximized;
        printPreviewDlg.ShowDialog();


    }

    private static void OnPrintPage(object sender, PrintPageEventArgs e)
    {
        string MyTag = string.Empty;
        MyTag = WaveActions.ActiveId;
        Wave MyWave = WaveHolder.FindWave(MyTag);
        int MyCount = 0;

        int xOffset = e.MarginBounds.Location.X;

        int yOffset = e.MarginBounds.Location.Y;


        if (MyWave != null)
        {
            Graphics g = e.Graphics;
            g.SetClip(e.PageBounds);

            Pen MyPen = new Pen(WaveTools.WaveColor, WaveTools.PenWidth);

            float dx = (float)e.PageBounds.Width / MyWave.NumSamples;
            float dy = (float)e.PageBounds.Height / 255;


            if (MyWave.Normal == false)
             {
               g.ScaleTransform(dx, dy);

            }



            for (int i = 0; i < MyWave.NumSamples - 1; i++)
            {





                g.DrawLine(MyPen, i, MyWave.Data[i], i + 1, MyWave.Data[i + 1]);
                MyCount = MyCount + 1;

                if (MyCount > e.MarginBounds.Width)
                {

                    e.HasMorePages = true;
                    MyCount = 0; 
                    return;
                }
                else
                {

                    e.HasMorePages = false;
                    return;

                }


            }


        }


    }

}

}

Upvotes: 0

Views: 266

Answers (1)

Hans Passant
Hans Passant

Reputation: 941455

       for (int i = 0; i < MyWave.NumSamples - 1; i++)

That's the core problem statement, you start at 0 every time PrintPage gets called. You need to resume where you left off on the previous page. Make the i variable a field of your class instead of a local variable. Implement the BeginPrint event to set it to zero.

The else clause inside the loop need to be deleted.

Upvotes: 1

Related Questions