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