private void bwCreateChoices_DoWork(object sender, DoWorkEventArgs e) { if (question.Choices == null || question.Choices.Count == 0) { question.Choices = new List <Choice>(new Choice[4]); for (int i = 0; i < 4; i++) //make 4 blank choices { question.Choices[i] = new Choice(); question.Choices[i].CreateBlankChoice((int)question.Id, i); question.Choices[i].Id = DB_Insert.InsertChoice(question.Choices[i]); } } }
private void bwImportCategory_DoWork(object sender, DoWorkEventArgs e) { if (importCategoryForm.SelectedCategory.Questions.Count > 0) //only import questions if there are any { //only imports questions to the max size of the grid and the max size of the other game grid for (int i = 0; i < category.Questions.Count && i < importCategoryForm.SelectedCategory.Questions.Count; i++) { //if going from multiple choice to another type, delete the choices if (category.Questions[i].Type == "mc" && importCategoryForm.SelectedCategory.Questions[i].Type != "mc") { for (int j = 0; j < 4 && j < category.Questions[i].Choices.Count; j++) { DB_Delete.DeleteChoice(category.Questions[i].Choices[j].Id); } } // going from other type of question to multiple choice, make the choices else if (category.Questions[i].Type != "mc" && importCategoryForm.SelectedCategory.Questions[i].Type == "mc") { category.Questions[i].Choices = new List <Choice>(new Choice[4]); for (int j = 0; j < 4 && j < category.Questions[i].Choices.Count; j++) { category.Questions[i].Choices[j] = new Choice(); category.Questions[i].Choices[j].QuestionId = (int)category.Questions[i].Id; category.Questions[i].Choices[j].Index = j; category.Questions[i].Choices[j].Text = importCategoryForm.SelectedCategory.Questions[i].Choices[j].Text; category.Questions[i].Choices[j].Id = DB_Insert.InsertChoice(category.Questions[i].Choices[j]); } } //if both are multiple choice, just do a simple update else if (category.Questions[i].Type == "mc" && importCategoryForm.SelectedCategory.Questions[i].Type == "mc") { for (int j = 0; j < 4; j++) { category.Questions[i].Choices[j].Text = importCategoryForm.SelectedCategory.Questions[i].Choices[j].Text; DB_Update.UpdateChoice(category.Questions[i].Choices[j]); } } //set the other new properties of the importing questions. This needs to go after the previous code category.Questions[i].Type = importCategoryForm.SelectedCategory.Questions[i].Type; category.Questions[i].QuestionText = importCategoryForm.SelectedCategory.Questions[i].QuestionText; category.Questions[i].Answer = importCategoryForm.SelectedCategory.Questions[i].Answer; //update the question (import the question) DB_Update.UpdateQuestion(category.Questions[i]); } } }
private void bwAddQuestions_DoWork(object sender, DoWorkEventArgs e) { for (int i = 0; i < game.NumCategories; i++) { Question newQuestion = new Question(); newQuestion.CategoryId = (int)game.Categories[i].Id; newQuestion.ResetQuestionToDefaults(); //default data newQuestion.Weight = (game.NumQuestionsPerCategory) * 100; newQuestion.Id = DB_Insert.InsertQuestion(newQuestion); if (newQuestion.Id != null && newQuestion.Id > 0) //if the insert into db worked { game.Categories[i].Questions.Add(newQuestion); //add it to the current game object too } } }
private void btnImport_Click(object sender, EventArgs e) { frmImportQuestion importQuestionForm = new frmImportQuestion(); DialogResult dialogResult = importQuestionForm.ShowDialog(); if (dialogResult == DialogResult.OK) { //if going from multiple choice to another type, delete the choices if (question.Type == "mc" && importQuestionForm.selectedQuestion.Type != "mc") { for (int j = 0; j < 4 && j < question.Choices.Count; j++) { DB_Delete.DeleteChoice(question.Choices[j].Id); } } // going from other type of question to multiple choice, make the choices else if (question.Type != "mc" && importQuestionForm.selectedQuestion.Type == "mc") { question.Choices = new List <Choice>(new Choice[4]); for (int j = 0; j < 4 && j < question.Choices.Count; j++) { question.Choices[j] = new Choice(); question.Choices[j].QuestionId = (int)question.Id; question.Choices[j].Index = j; question.Choices[j].Text = importQuestionForm.selectedQuestion.Choices[j].Text; question.Choices[j].Id = DB_Insert.InsertChoice(question.Choices[j]); } } //if both are multiple choice, just do a simple update else if (question.Type == "mc" && importQuestionForm.selectedQuestion.Type == "mc") { for (int j = 0; j < 4; j++) { question.Choices[j].Text = importQuestionForm.selectedQuestion.Choices[j].Text; DB_Update.UpdateChoice(question.Choices[j]); } } //get info from the selected question (not a complete clone because the IDs have to be different) question.Type = importQuestionForm.selectedQuestion.Type; question.QuestionText = importQuestionForm.selectedQuestion.QuestionText; question.Answer = importQuestionForm.selectedQuestion.Answer; frmEditQuestion_Load(sender, e); //reload this form with the info } }
private void bwAddCategory_DoWork(object sender, DoWorkEventArgs e) { Category newCategory = new Category(); newCategory.GameId = (int)game.Id; newCategory.Index = game.Categories.Count; newCategory.ResetCategoryToDefaults(); //other default data newCategory.Id = DB_Insert.InsertCategory(newCategory); newCategory.Questions = new List <Question>(new Question[game.NumQuestionsPerCategory]); for (int i = 0; i < game.NumQuestionsPerCategory; i++) { newCategory.Questions[i] = new Question(); newCategory.Questions[i].CategoryId = (int)newCategory.Id; newCategory.Questions[i].ResetQuestionToDefaults(); //other default data newCategory.Questions[i].Weight = (i + 1) * 100; newCategory.Questions[i].Id = DB_Insert.InsertQuestion(newCategory.Questions[i]); } game.Categories.Add(newCategory); }
public static void importXML(String path, string fileName) { try { XmlSerializer xs = new XmlSerializer(typeof(Game)); using (var sr = new StreamReader(path)) { Game importedGame = (Game)xs.Deserialize(sr); importedGame.Id = null; importedGame.GameName = fileName; int?id = DB_Insert.InsertGame(importedGame); } } catch (Exception ex) { MessageBox.Show("This file is not in the correct format for this game. The game cannot be imported", "Import Error", MessageBoxButtons.OK, MessageBoxIcon.Error); Console.WriteLine(ex.ToString()); } }
//creating the new blank game in the db can take a few seconds, so do it in a background thread private void bwInsertGame_DoWork(object sender, DoWorkEventArgs e) { DB_Insert.InsertGame(newGame); }