private void btAddQuestion_Click(object sender, EventArgs e) { var item = new Question(); if (fmEditQuestion.Execute(item)) Item.Add(item); UpdateInterface(); }
/// <summary> /// Читает вопросы из файла /// </summary> /// <returns> Расположение файла </returns> public static List<Question> ReadQuestions(string address) { try { // Список вопросов var list = new List<Question>(); // Читаем весь файл var file = File.ReadAllText(address); // Бьем его на массив вопросов var questions = file.Split('@'); // Перебираем каждый вопрос, кроме последнего for (var i = 0; i < questions.Length - 1; i++) { var questionElements = questions[i].Split('|'); var question = new Question { QuestionString = questionElements[0], TrueAnswer = questionElements[1], Answers = questionElements[2].Split('$').ToList() }; list.Add(question); } return list; } catch (Exception ex) { throw new Exception("Ошибка чтения вопросов из файла", ex); } }
public static bool Execute(Question item) { using (var fm = new fmEditQuestion(item)) { fm.ShowDialog(); if (fm.DialogResult == DialogResult.OK) return true; return false; } }
private void LoadForm(Question item) { if (item.QuestionString == null) { this.Text = "Новый Вопрос"; Item = item; } else { this.Text = "Редактирование вопроса"; Item = item; tbQuestion.Text = Item.QuestionString; tbAnswerOne.Text = Item.Answers[0]; tbAnswerTwo.Text = Item.Answers[1]; tbAnswerThree.Text = Item.Answers[2]; tbAnswerFour.Text = Item.Answers[3]; int trueAnswer = -1; for (var i = 0; i <= 3; i++) { if (Item.Answers[i] == Item.TrueAnswer) { trueAnswer = i; break; } } if (trueAnswer == -1) { MessageBox.Show("Вопрос --" + item.QuestionString + " -- не имеет правильного варианта ответа.", "Внимание", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { switch (trueAnswer) { case 0: rbOne.Checked = true; break; case 1: rbTwo.Checked = true; break; case 2: rbThree.Checked = true; break; case 3: rbFour.Checked = true; break; } } } }
public fmEditQuestion(Question item) { InitializeComponent(); LoadForm(item); }