Exemplo n.º 1
0
        /// <summary>
        /// handles ok button click event.
        /// Updates external reference if question is valid
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOK_Click(object sender, EventArgs e)
        {
            //validate question
            if (txtQuestion.Text == "")
            {
                MessageBox.Show("Question does not have text!", "Invalid Question");
            }
            //validate answers
            else if ((txtAnswer1.Text == "") ||
                     (txtAnswer2.Text == "") ||
                     (txtAnswer3.Text == "") ||
                     (txtAnswer4.Text == ""))
            {
                MessageBox.Show("All answers must have text!", "Invalid Question");
            }
            else if ((rbtn1.Checked == false) &&
                     (rbtn2.Checked == false) &&
                     (rbtn3.Checked == false) &&
                     (rbtn4.Checked == false))
            {
                MessageBox.Show("No correct answer has been selected!", "Invalid Question");
            }
            else
            {
                //are we creating a new question or updating an old one?
                if (isNewQuestion) //new question
                {
                    externalQuestion.Text = txtQuestion.Text;
                    AnswerData answer;

                    answer         = new AnswerData(0);
                    answer.Text    = txtAnswer1.Text;
                    answer.Correct = rbtn1.Checked;
                    externalQuestion.AddAnswer(answer);

                    answer         = new AnswerData(0);
                    answer.Text    = txtAnswer2.Text;
                    answer.Correct = rbtn2.Checked;
                    externalQuestion.AddAnswer(answer);

                    answer         = new AnswerData(0);
                    answer.Text    = txtAnswer3.Text;
                    answer.Correct = rbtn3.Checked;
                    externalQuestion.AddAnswer(answer);

                    answer         = new AnswerData(0);
                    answer.Text    = txtAnswer4.Text;
                    answer.Correct = rbtn4.Checked;
                    externalQuestion.AddAnswer(answer);

                    this.Disposed -= frmQuestion_Disposed;
                    this.Dispose();
                }
                //change the old question
                else
                {
                    externalQuestion.Text               = txtQuestion.Text;
                    externalQuestion.Answers[0].Text    = txtAnswer1.Text;
                    externalQuestion.Answers[0].Correct = rbtn1.Checked;
                    externalQuestion.Answers[1].Text    = txtAnswer2.Text;
                    externalQuestion.Answers[1].Correct = rbtn2.Checked;
                    externalQuestion.Answers[2].Text    = txtAnswer3.Text;
                    externalQuestion.Answers[2].Correct = rbtn3.Checked;
                    externalQuestion.Answers[3].Text    = txtAnswer4.Text;
                    externalQuestion.Answers[3].Correct = rbtn4.Checked;

                    this.Disposed -= frmQuestion_Disposed;
                    this.Dispose();
                }
            }
        }