// carries out the deletion of the question // deletes from list of question objects first, then updates text file accordingly by rewriting all remaining // question objects private void deleteQuestionFromFile() { QuestionBank QtoDelete = null; if (QuestionTextList.SelectedItem != null) { foreach (QuestionBank aQuestion in BoardAdmin.questions) { if (aQuestion.questiontext.ToString() == QuestionTextList.SelectedItem.ToString()) { QtoDelete = aQuestion; BoardAdmin.questions.Remove(QtoDelete); break; } } // writer for writing to file StreamWriter aSW = new StreamWriter(File.Open("QuestionBank 2.csv", FileMode.Truncate)); // rewrites each question to the file - OVERWRITES foreach (QuestionBank aQuestion in BoardAdmin.questions) { aSW.WriteLine(aQuestion.questiontext + "," + aQuestion.answer + "," + aQuestion.answer2 + "," + aQuestion.difficulty); } aSW.Close(); // close stream // re-populate list box with the new contents of the question object list and hence the text file QuestionTextList.Items.Clear(); populateListBox(); } }
//This procedure carries out the task of adding the question to the file stored on the server. //It checks that the input is valid and then it appends a new line containing the new question, //in the correct format private void addQuestionToFile() { if (QuestionTextBox.Text == "") { MessageBox.Show("Please enter the question text", "error"); } if (Answer1Box.Text == "") { MessageBox.Show("Please enter the question Answer", "error"); } if (DifficultyBox.Text == "") { MessageBox.Show("Please enter the Difficulty", "error"); } int num; // stores the result of the conversion bool isint = int.TryParse(DifficultyBox.Text, out num); // this checks that the difficulty is an integer if (isint == true) { if (Convert.ToInt16(DifficultyBox.Text) > 3) // the difficulty level cannot be greater than 3 { MessageBox.Show("the difficulty cannot exceed 3", "error"); } else { if (QuestionTextBox.Text != "" && Answer1Box.Text != "") { QuestionBank newQuestion = new QuestionBank(QuestionTextBox.Text, Answer1Box.Text, Answer2Box.Text, Convert.ToInt16(DifficultyBox.Text)); BoardAdmin.questions.Add(newQuestion); File.AppendAllText("QuestionBank 2.csv", Environment.NewLine + newQuestion.questiontext + "," + newQuestion.answer + "," + newQuestion.answer2 + "," + newQuestion.difficulty); MessageBox.Show("Question added successfully!", "Successful!"); } } } }
// when the edit button is clicked private void EditQuestionsButton_Click(object sender, EventArgs e) { //checks that there is a selection if (QuestionTextList.SelectedItem != null) { // validates to ensure all appropiate data is available if (QuestionTextBox.Text == "" || Answer1Box.Text == "") { MessageBox.Show("fields are empty", "error"); } else { int num; // stores result of conversion bool isint = int.TryParse(DifficultyBox.Text, out num); // checks for integer values if (isint == true) { if (num > 3) { MessageBox.Show("the difficulty cannot exceed 3", "error"); } else { QuestionBank QtoEdit = null; foreach (QuestionBank aQuestion in BoardAdmin.questions) { QuestionTextList.SelectedItem = aQuestion; if (aQuestion.questiontext.ToString() == QuestionTextList.SelectedItem.ToString()) { QtoEdit = aQuestion; BoardAdmin.questions.Remove(QtoEdit); break; } } QtoEdit.questiontext = QuestionTextBox.Text; QtoEdit.answer = Answer1Box.Text; QtoEdit.answer2 = Answer2Box.Text; QtoEdit.difficulty = Convert.ToInt16(DifficultyBox.Text); BoardAdmin.questions.Add(QtoEdit); File.WriteAllText("QuestionBank 2.csv", string.Empty); StreamWriter aSW = new StreamWriter(File.Open("QuestionBank 2.csv", FileMode.Open)); foreach (QuestionBank aQuestion in BoardAdmin.questions) { // re writes each question as a line in the text file aSW.WriteLine(aQuestion.questiontext + "," + aQuestion.answer + "," + aQuestion.answer2 + "," + aQuestion.difficulty); } aSW.Close(); // closes the stream // the following takes the text from the file and removes all blank lines after the content to ensure no errors occur // when reading in from the file on another process string temptext = File.ReadAllText("QuestionBank 2.csv"); temptext = temptext.Trim(); File.WriteAllText("QuestionBank 2.csv", temptext); // overwrites QuestionTextList.Items.Clear(); populateListBox(); MessageBox.Show("Question edited successfully!", "Successful!"); } } else { MessageBox.Show("The difficulty must be a number", "error"); } } } }