我在家庭作业程序的最后一部分遇到问题,我必须进行多项选择测试,最后给出一个可打印的测试版本。老师给了我们代码,以便能够打印多个页面,但似乎只能复制第一页。我试图弄乱代码试图让它工作,但它要么无穷无尽的第一页或程序崩溃导致我的索引计数器超出了我存储问题和答案的数组。这是我停下来的地方。
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页长),第二页是所有问题的空白。
答案 0 :(得分:2)
pageCount = 1;
Index = 0;
该代码属于BeginPrint事件处理程序,即打印开始时调用的处理程序。为每个页面调用PrintPage事件处理程序,您应该只绘制每个特定页面上的内容。
答案 1 :(得分:0)
为每个页面调用此方法,因此您必须为下次调用保留这些变量的值,以便知道您要去哪里。
似乎索引是在方法之外定义的,所以如果你在调用print之前将它设置为零而不在这里将其归零,它可能会做你想要的。