예제 #1
0
        public AddForm(Form1 mainForm, int numOfAnswers, Questions q1 = null, Questions q2 = null)
        {
            InitializeComponent();
            this.mainForm = mainForm;

            question1Text.ReadOnly = true;
            question2Text.ReadOnly = true;

            if (numOfAnswers == 2)
            {
                question1Text.Text = q1.Question;
                question2Text.Text = q2.Question;
                addButton.Click   += (object sender, EventArgs e) =>
                {
                    if (answerText.Text != "")
                    {
                        try {
                            DataBaseUtil.SaveAnswer(new Answers(answerText.Text, q1.Id, q2.Id));
                            mainForm.restart();
                            mainForm.Show();
                            this.Close();
                        }
                        catch (MySql.Data.MySqlClient.MySqlException ex)
                        {
                            MessageBox.Show("Такой объект уже есть!\n" + ex.ToString());
                        }
                    }
                    else
                    {
                        MessageBox.Show("Заполните все поля!");
                    }
                };
            }
            else
            {
                if (numOfAnswers == 1)
                {
                    question1Text.Text     = q1.Question;
                    question2Text.ReadOnly = false;

                    addButton.Click += (object sender, EventArgs e) =>
                    {
                        if (answerText.Text != "" && question2Text.Text != "")
                        {
                            try {
                                Questions newQuestion = new Questions(DataBaseUtil.getQuestionMaxID() + 1, question2Text.Text);
                                DataBaseUtil.SaveQuestion(newQuestion);
                                DataBaseUtil.SaveAnswer(new Answers(answerText.Text, q1.Id, newQuestion.Id));
                                mainForm.restart();
                                mainForm.Show();
                                this.Close();
                            }
                            catch (MySql.Data.MySqlClient.MySqlException ex)
                            {
                                MessageBox.Show("Один из вводимых параметров уже есть в базе данных!\n" + ex.ToString());
                            }
                        }
                        else
                        {
                            MessageBox.Show("Заполните все поля!");
                        }
                    };
                }
                else
                {
                    question2Text.ReadOnly = false;
                    question1Text.ReadOnly = false;

                    addButton.Click += (object sender, EventArgs e) =>
                    {
                        if (answerText.Text != "" && question1Text.Text != "" && question2Text.Text != "")
                        {
                            try
                            {
                                Questions newQuestion1 = new Questions(DataBaseUtil.getQuestionMaxID() + 1, question1Text.Text);
                                DataBaseUtil.SaveQuestion(newQuestion1);
                                Questions newQuestion2 = new Questions(DataBaseUtil.getQuestionMaxID() + 1, question2Text.Text);
                                DataBaseUtil.SaveQuestion(newQuestion2);
                                DataBaseUtil.SaveAnswer(new Answers(answerText.Text, newQuestion1.Id, newQuestion2.Id));
                                mainForm.restart();
                                mainForm.Show();
                                this.Close();
                            }
                            catch (MySql.Data.MySqlClient.MySqlException ex)
                            {
                                MessageBox.Show("Один из вводимых параметров уже есть в базе!\n" + ex.ToString());
                            }
                        }
                        else
                        {
                            MessageBox.Show("Заполните все поля!");
                        }
                    };
                }
            }
        }