//method: AddOrModifyQuestion //description: modifies questions in the database //params: Question question - new question with answers to add to database // Question oldQuestion - old question containing answers to replace in database //returns: void public void AddOrModifyQuestion(Question question, Question oldQuestion) { if(stopped) return; Task.Factory.StartNew(() => { try { lock (socketLock) { //tell service to add or modify a question writer.WriteLine("ADD_QUESTION"); writer.WriteLine("" + question.questionNumber); writer.WriteLine("" + question.questionText); for (int i = 0; i<oldQuestion.answers.Count(); i++) { writer.WriteLine("OLDANSWER:" + oldQuestion.answers[i].answerText); } for (int i = 0; i<question.answers.Count(); i++){ writer.WriteLine("ANSWER:" + question.answers[i].answerText); writer.WriteLine("ISCORRECT:" + question.answers[i].isCorrect); } writer.Flush(); //todo: delete any questions in the db whos QuestionNumber is the same as question.questionNumber //todo: add new question to the db with the data from question } } catch(Exception) { stopped = true; socket.Close(); owner.Invoke((MethodInvoker)(() => { ConnectionLost(this, new EventArgs()); })); } }); }
//method: SaveButton_Click //description: SaveButton click event //params: object sender - sender // EventArgs e - event args //returns: void private void SaveButton_Click(object sender, EventArgs e) { SaveButton.Enabled = false; EditButton.Enabled = true; if (questionNum != 10) { NextButton.Enabled = true; } if (questionNum != 1) { PreviousButton.Enabled = true; } QuestionTextbox.Enabled = false; TextboxA.Enabled = false; TextboxB.Enabled = false; TextboxC.Enabled = false; TextboxD.Enabled = false; AnswerGroupbox.Enabled = false; //Get A answerA = new Answer(); answerA.answerText = TextboxA.Text; answerA.isCorrect = RadioButtonA.Checked; //Get B answerB = new Answer(); answerB.answerText = TextboxB.Text; answerB.isCorrect = RadioButtonB.Checked; //Get C answerC = new Answer(); answerC.answerText = TextboxC.Text; answerC.isCorrect = RadioButtonC.Checked; //Get D answerD = new Answer(); answerD.answerText = TextboxD.Text; answerD.isCorrect = RadioButtonD.Checked; //create question question = new Question(); question.answers = new List<Answer>(); question.questionNumber = questionNum; question.questionText = QuestionTextbox.Text; question.answers.Add(answerA); question.answers.Add(answerB); question.answers.Add(answerC); question.answers.Add(answerD); oldQuestion = new Question(); oldQuestion.answers = new List<Answer>(); oldQuestion.answers.Add(oldAnswerA); oldQuestion.answers.Add(oldAnswerB); oldQuestion.answers.Add(oldAnswerC); oldQuestion.answers.Add(oldAnswerD); connection.AddOrModifyQuestion(question, oldQuestion); }
//method: GetQuestion //description: retrieves question information from the database //params: int questionNumber - questionNumber //returns: void public void GetQuestion(int questionNumber) { if(stopped) return; Task.Factory.StartNew(() => { try { lock (socketLock) { //tell service to get questions writer.WriteLine("GET_QUESTION"); writer.WriteLine("" + questionNumber); writer.Flush(); Question question = new Question(); question.questionNumber = questionNumber; //get question string input = reader.ReadLine(); if (input.StartsWith("QUESTION:")) { question.questionText = input.Substring("QUESTION:".Length); //get question answers question.answers = new List<Answer>(); while (reader.PeekLine().StartsWith("ANSWER:")) { Answer a = new Answer(); a.answerText = reader.ReadLine().Substring("ANSWER:".Length); if (reader.PeekLine().StartsWith("ISCORRECT:")){ a.isCorrect = Convert.ToBoolean(reader.ReadLine().Substring("ISCORRECT:".Length)); question.answers.Add(a); } } string writerDump = reader.ReadLine(); //dump for "DONE" string from server owner.Invoke((MethodInvoker)( () => { QuestionReceived(this, question); } )); } } } catch(Exception) { stopped = true; socket.Close(); owner.Invoke((MethodInvoker)(() => { ConnectionLost(this, new EventArgs()); })); } }); }
//method: QuestionReceivedHandler //description: question received event handler //params: object sender - sender // question q - question //returns: void private void QuestionReceivedHandler(object sender, Question q) { //put question and answers in textboxes QuestionTextbox.Text = q.questionText; TextboxA.Text = q.answers[0].answerText; TextboxB.Text = q.answers[1].answerText; TextboxC.Text = q.answers[2].answerText; TextboxD.Text = q.answers[3].answerText; //set correct answer if (q.answers[0].isCorrect == true) { RadioButtonA.Checked = true; ; } else if (q.answers[1].isCorrect == true) { RadioButtonB.Checked = true; } else if (q.answers[2].isCorrect == true) { RadioButtonC.Checked = true; } else if (q.answers[3].isCorrect == true) { RadioButtonD.Checked = true; } }