Reputation: 917
I am having an issue with the last part of a homework program where I have to make a multiple choice test and at the end give a printable version of the test. The teacher gave us code to be able to print multiple pages but it seems to only make copies of the first page. I tried to mess with the code to try and make it work, but it either make endless amounts of the first page or the program crashes cause my index counter goes beyond the array I made that stores the questions and answers. Here is where I stopped.
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;
}
}
}
Edit: I have changed the code above to what I have now. It goes to the next page and makes the tile, but the page count says 6 instead of 2 (only suppose to be 2 pages long) and the second page is blank of all the questions.
Upvotes: 1
Views: 725
Reputation: 942438
pageCount = 1;
Index = 0;
That code belongs in the BeginPrint event handler, the one that gets called when printing starts. Your PrintPage event handler gets called for each page, you should only draw what goes on each particular page.
Upvotes: 2
Reputation: 31203
This method is called for each page, so you have to keep the values for these variables for the next call so you know where you're going.
Seems like Index is defined outside the method, so if you set it to zero before you call print and don't zero it here, it probably will do what you want.
Upvotes: 0