//This button click event decrements the current question number value and populates the input fields with data from the previously saved question stored in the CurrentTestQuestions List <T> of the type MultipleChoice private void BtPrevQuest_Click(object sender, RoutedEventArgs e) { if (CurrentTestQuestions.Count < Convert.ToInt32(lbQuestionNumber.Content)) { MessageBox.Show("Please save the current question before viewing other questions", "Error"); } else { MultipleChoice ques = new MultipleChoice(); ques.Question = new TextRange(rtQuestion.Document.ContentStart, rtQuestion.Document.ContentEnd).Text; btNextQuest.IsEnabled = true; lbQuestionNumber.Content = test.PreviousQuestion().ToString(); test.QuestionNumber = Convert.ToInt32(lbQuestionNumber.Content); tbA.Text = CurrentTestQuestions[test.QuestionNumber - 1].OptionA; tbB.Text = CurrentTestQuestions[test.QuestionNumber - 1].OptionB; tbC.Text = CurrentTestQuestions[test.QuestionNumber - 1].OptionC; tbD.Text = CurrentTestQuestions[test.QuestionNumber - 1].OptionD; rtQuestion.Document.Blocks.Clear(); rtQuestion.Document.Blocks.Add(new Paragraph(new Run(CurrentTestQuestions[test.QuestionNumber - 1].Question))); switch (CurrentTestQuestions[test.QuestionNumber - 1].CorrectAnswer) { case "A": combCorrectAns.SelectedIndex = 1; break; case "B": combCorrectAns.SelectedIndex = 2; break; case "C": combCorrectAns.SelectedIndex = 3; break; case "D": combCorrectAns.SelectedIndex = 4; break; } } if (Convert.ToInt32(lbQuestionNumber.Content) != QuestionCounter) { btNewQuestion.IsEnabled = false; } if (lbQuestionNumber.Content.ToString().Equals("1")) { btPrevQuest.IsEnabled = false; btNextQuest.IsEnabled = true; } }
//This button click event adds or overwrites new question data from the current input fields using a List <T> of the type MultipleChoice private void BtSaveQuestion_Click(object sender, RoutedEventArgs e) { MultipleChoice ques = new MultipleChoice(); ques.Question = new TextRange(rtQuestion.Document.ContentStart, rtQuestion.Document.ContentEnd).Text; if (ques.Question.Equals("") || tbA.Text.Equals("") || tbB.Text.Equals("") || tbC.Text.Equals("") || tbD.Text.Equals("") || combCorrectAns.SelectedIndex == 0) { MessageBox.Show("Please ensure all question fields are filled before saving a question"); } else { ques.Name = test.Name; ques.OptionA = tbA.Text; ques.OptionB = tbB.Text; ques.OptionC = tbC.Text; ques.OptionD = tbD.Text; ques.CorrectAnswer = combCorrectAns.Text; if (CurrentTestQuestions.Count >= QuestionCounter) { CurrentTestQuestions.RemoveAt(Convert.ToInt32(lbQuestionNumber.Content) - 1); } try { CurrentTestQuestions.Insert(Convert.ToInt32(lbQuestionNumber.Content) - 1, ques); } catch (Exception) { CurrentTestQuestions.Add(ques); } MessageBox.Show("Question " + test.QuestionNumber + " Saved Successfully!\nClick [Add New Question] to add another question, or [Submit Test] if you are finished", "Success!"); if (Convert.ToInt32(lbQuestionNumber.Content) == QuestionCounter) { btNewQuestion.IsEnabled = true; } } }