//___________________Adds a user from input from user, tests user ID and passes userObj to InsertToDb method_________ private void addUserButt_Click(object sender, EventArgs e) { if (Housekeeping.CheckAllFields(this)) { if (int.TryParse(userIDTxt.Text, out int ret)) //only accept int type for user ID { if (!test.UserObjDictionary.ContainsKey(int.Parse(userIDTxt.Text))) //Check user ID doesnt already exist { switch (accessGrpCbox.SelectedIndex) { case 0: { user = new AdminObj(); break; } case 1: { user = new LecturerObj(); break; } case 2: { user = new StudentObj(); break; } default: { break; } } user.UserID = int.Parse(userIDTxt.Text); user.Name = nameTxt.Text; user.Surname = surnameTxt.Text; user.Password = passwordTxt.Text; DatabaseUtilities.InsertToDB("Users", user); Housekeeping.ResetForm(this); //invoke delegate OnUserAdded(this, null); } else { MessageBox.Show("User ID already exists!", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } } else { MessageBox.Show("User ID can only contain numbers!", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } } else { MessageBox.Show("ALL FIELDS ARE REQUIRED!", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } }
//_______________________on click method to save the test calls on close method__________________________________________ private void saveAndExitButt_Click(object sender, EventArgs e) { if (Housekeeping.CheckAllFields(this)) { nextQuesButt_Click(sender, e); this.Close(); } else { this.Close(); } }
//______________________on click method to save the current question and move to next___________________________________ private void nextQuesButt_Click(object sender, EventArgs e) { string correctAns = ""; if (Housekeeping.CheckAllFields(this)) { if (ansARB.Checked) { correctAns = "A"; } else if (ansBRB.Checked) { correctAns = "B"; } else if (ansCRB.Checked) { correctAns = "C"; } else if (ansDRB.Checked) { correctAns = "D"; } test.QuestionID++; test.addQuestion(test.QuestionID, questionTxt.Text, test.NewTestID, ansATxt.Text, ansBTxt.Text, ansCTxt.Text, ansDTxt.Text, correctAns); questionCounter++; questionLbl.Text = "Question " + questionCounter + ":"; Housekeeping.ResetForm(this); } else { MessageBox.Show("ALL FIELDS ARE REQUIRED!", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } }
//__________________On click method populates next question and saves previous answer___________________________________ private void nextQuestButt_Click(object sender, EventArgs e) { //if list of marks from selected test does NOT contain Current UserID the user has not already taken the test if (!test.MarksDictionary.Values.Where(n => n.TestID == ((KeyValuePair <int, String>)testsCbox.SelectedItem).Key).Any(n => n.UserID == User.UserID)) { //if combobox for test selection is still visible test has not started yet. if (testsCbox.Visible) { test.ChosenTestID = ((KeyValuePair <int, String>)testsCbox.SelectedItem).Key; test.TestName = test.TestsDictionary[test.ChosenTestID]; test.PopulateTestQuestions(); questionCount = test.MinValue - 1; PopulateNextQuest(sender); this.Text = test.TestName; testsCbox.Visible = false; questionLbl.Visible = true; radButtPnl.Visible = true; nextQuestButt.Text = "Next Question"; } else { //if answer not selected if (Housekeeping.CheckAllFields(radButtPnl)) { //if final question if (questionCount == test.MaxValue) { setLearnerAns(); test.addToMemo(questionCount, learnerAns, learnerAnsString); test.addToMarks(); Thread thread = new Thread(new ThreadStart(test.saveAnswers)); thread.Start(); memoCount++; questionCount++; if (test.TestMark.MarkPercent >= 50) { MessageBox.Show("Congratulations you got " + test.TestMark.MarkInt + "/" + test.NumberOfQuestions + " answers correct giving you " + test.TestMark.MarkPercent + "%", "Pass", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { MessageBox.Show("Unfortunately you only got " + test.TestMark.MarkInt + "/" + test.NumberOfQuestions + " answers correct giving you " + test.TestMark.MarkPercent + "%", "Fail", MessageBoxButtons.OK, MessageBoxIcon.Information); } } else { //if less than final question if (questionCount < test.MaxValue) { if (questionCount == test.MaxValue - 1) { nextQuestButt.Text = "Save and Submit"; } setLearnerAns(); test.addToMemo(questionCount, learnerAns, learnerAnsString); PopulateNextQuest(sender); Housekeeping.ResetForm(radButtPnl); } } } else { MessageBox.Show("Please select an answer!", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error); } } //if test complete but memo not if (memoCount > 0 && memoCount <= test.NumberOfQuestions) { //if first memo question if (memoCount == 1) { //if user selects yes to view memo if (MessageBox.Show("Would you like to view the Memo?", "View Memo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { ansARb.Visible = false; ansBRb.Visible = false; ansCRb.Visible = false; ansDRb.Visible = false; correctAnsLbl.Visible = true; youAnsLbl.Visible = true; testAnsLbl.Visible = true; learnerAnsLbl.Visible = true; nextQuestButt.Text = "Next Question"; } else { this.Close(); } } //if memo not complete but not first question else { //if second last memo question if (memoCount == test.NumberOfQuestions) { nextQuestButt.Text = "Exit"; } else { //if memo final question if (memoCount > test.NumberOfQuestions) { this.Close(); } } } PopulateNextMemo(); } else { //if memo final question if (memoCount > test.NumberOfQuestions) { this.Close(); } } } else { MessageBox.Show("You have already taken this test.", "Nope", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } }