【问题标题】:Printing over several pages打印多页
【发布时间】:2013-02-10 04:34:09
【问题描述】:

我在作业程序的最后一部分遇到问题,我必须进行多项选择测试,最后给出测试的可打印版本。老师给了我们能够打印多页的代码,但它似乎只复制第一页。我试图弄乱代码以使其工作,但它要么使第一页无休止,要么程序崩溃导致我的索引计数器超出了我创建的存储问题和答案的数组。这是我停下来的地方。

private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{

    Font titleFont = new Font("Brush Script Std", 25);
    Font typeFont = new Font("Times New Roman", 15);

    int questionCount = 1;
    int xcoordinate = 20, ycoordinate = 140;
    String IndexQuestion, IndexAnswerA, IndexAnswerB, IndexAnswerC, IndexAnswerD, IndexCorrectAnswer;

    if (testTaken == "yes")
    {
        e.Graphics.DrawString("Visual Basic Assessment Questions",
                titleFont, Brushes.Black, 100, 20);

        e.Graphics.DrawString("Page" + pageCount,
                typeFont, Brushes.Black, 100, 90);

        while (Index < 10)
        {
            IndexQuestion = DataTier.allTestQuestions[Index].Question.ToString();
            IndexAnswerA = DataTier.allTestQuestions[Index].AnswerA.ToString();
            IndexAnswerB = DataTier.allTestQuestions[Index].AnswerB.ToString();
            IndexAnswerC = DataTier.allTestQuestions[Index].AnswerC.ToString();
            IndexAnswerD = DataTier.allTestQuestions[Index].AnswerD.ToString();
            IndexCorrectAnswer = DataTier.allTestQuestions[Index].CorrectAnswer.ToString();
            e.Graphics.DrawString(questionCount + "." + DataTier.allTestQuestions[Index].Question, typeFont, Brushes.Black, xcoordinate, ycoordinate);
            ycoordinate += 20;
            e.Graphics.DrawString(IndexAnswerA, typeFont, Brushes.Black, xcoordinate, ycoordinate);
            ycoordinate += 20;
            e.Graphics.DrawString(IndexAnswerB, typeFont, Brushes.Black, xcoordinate, ycoordinate);
            ycoordinate += 20;
            e.Graphics.DrawString(IndexAnswerC, typeFont, Brushes.Black, xcoordinate, ycoordinate);
            ycoordinate += 20;
            e.Graphics.DrawString(IndexAnswerD, typeFont, Brushes.Black, xcoordinate, ycoordinate);
            ycoordinate += 20;
            e.Graphics.DrawString("Correct Answer is: " + IndexCorrectAnswer, typeFont, Brushes.Red, xcoordinate, ycoordinate);

            ycoordinate += 60;
            questionCount += 1;
            Index += 1;
            if (ycoordinate >= e.MarginBounds.Bottom)
            {
               pageCount++; 
               e.HasMorePages = true;
            }
    }
}

编辑:我已将上面的代码更改为现在的代码。它转到下一页并制作瓷砖,但页数显示为 6 而不是 2(仅假设为 2 页长),第二页是所有问题的空白。

【问题讨论】:

    标签: c# printing


    【解决方案1】:
        pageCount = 1;
        Index = 0;
    

    该代码属于 BeginPrint 事件处理程序,即在打印开始时调用的事件处理程序。每个页面都会调用您的 PrintPage 事件处理程序,您应该只绘制每个特定页面上的内容。

    【讨论】:

    • 好的,我已经改变了它,它已经停止了无休止的打印。但它仍然不会将信息传递到下一页,只会停在第一页或只是崩溃。
    【解决方案2】:

    每个页面都会调用此方法,因此您必须为下一次调用保留这些变量的值,以便知道您要去哪里。

    似乎 Index 是在方法之外定义的,所以如果你在调用 print 之前将其设置为零并且不在这里将其归零,它可能会做你想做的事情。

    【讨论】:

      猜你喜欢
      • 2018-07-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-21
      • 2015-08-07
      • 2013-12-07
      相关资源
      最近更新 更多