Example #1
0
        //ok button
        private void okButton_Click(object sender, EventArgs e)
        {
            //error0 = less than 2 questions
            //error1 = empty answer
            //error2 = empty title
            bool[] errors = { false, false, false, false };

            this.clearErrors();

            ConfigForm conf = new ConfigForm("Είστε σίγουρος ότι θέλετε να επιβεβαιώσετε τις αλλαγές σας;\nΟι αλλαγές θα είναι οριστικές!", "Ακύρωση", "Επιβεβαίωση", Color.LimeGreen, "Επιβεβαίωση Επεξεργασίας Ερώτησης");

            if (conf.ShowDialog(this) == DialogResult.OK)
            {
                int check = 0;  //status check from db

                //get number of new answers
                int numberCount = this.editQuestionsDataGridView.Rows.Count;
                int delCount    = 0;

                //check if answers are >2
                for (int i = 0; i < numberCount - 1; i++)
                {
                    //get answer go to delete
                    bool flag = false;
                    try
                    {
                        flag = (bool)this.editQuestionsDataGridView.Rows[i].Cells[1].Value;
                    }
                    catch   //it was null
                    { }

                    if (flag)
                    {
                        delCount++;
                    }
                }

                if (this.editQuestionsDataGridView.Rows.Count - 1 - delCount < 2)
                {
                    errors[0] = true;
                }

                //check if there is empty answer
                for (int i = 0; i < this.editQuestionsDataGridView.Rows.Count - 1; i++)
                {
                    String temptxt = this.editQuestionsDataGridView.Rows[i].Cells[0].Value.ToString();
                    if (String.IsNullOrEmpty(temptxt) || String.IsNullOrWhiteSpace(temptxt))
                    {
                        errors[1] = true;
                    }
                }

                //check if there is empty title question
                if (String.IsNullOrEmpty(this.questionTextbox.Text) || String.IsNullOrWhiteSpace(this.questionTextbox.Text))
                {
                    errors[2] = true;
                }

                //checkif there are errors
                bool errorFlag = false;

                foreach (bool f in errors)
                {
                    if (f)
                    {
                        errorFlag = true;
                    }
                }

                if (!errorFlag)
                {
                    List <string> newAnswers      = new List <string>();
                    int           newAnswersCount = this.editQuestionsDataGridView.Rows.Count;

                    //get answers

                    for (int i = 0; i < this.editQuestionsDataGridView.Rows.Count - 1; i++)
                    {
                        bool tempFlag = Convert.ToBoolean(this.editQuestionsDataGridView.Rows[i].Cells[1].Value);
                        if (!(tempFlag))
                        {
                            newAnswers.Add(this.editQuestionsDataGridView.Rows[i].Cells[0].Value.ToString());
                        }
                    }



                    //check if answers are same
                    foreach (String obj in newAnswers)
                    {
                        foreach (String obj2 in newAnswers)
                        {
                            if (obj.Equals(obj2) && !obj.Equals("") && obj != obj2)
                            {
                                errors[3] = true;
                            }
                        }
                    }

                    if (errors[3])  //there are errors
                    {
                        this.errorsLabel.Text        += "Δε μπορείτε να καταχορήσετε ίδιες ερωτήσεις.";
                        this.errorsLabel.Visible      = true;
                        this.errorTittleLabel.Visible = true;
                    }
                    else    //everythinkg okey go to database
                    {
                        int diffLevel;
                        //take diff
                        if (this.diffRadioButton1.Checked)
                        {
                            diffLevel = 1;
                        }
                        else if (this.diffRadioButton2.Checked)
                        {
                            diffLevel = 2;
                        }
                        else
                        {
                            diffLevel = 3;
                        }

                        //new count of answer
                        if (newAnswers.Count < this.answers.Count)
                        {
                            int x = this.answers.Count - newAnswers.Count;
                            for (int i = 0; i < x; i++)
                            {
                                db.dAnswer(this.user, this.lesson, this.unit, this.question, this.answers.ElementAt(0));
                                this.answers.RemoveAt(0);
                            }
                        }
                        else if (newAnswers.Count > this.answers.Count)
                        {
                            int x = newAnswers.Count - this.answers.Count;
                            for (int i = 0; i < x; i++)
                            {
                                this.answers.Add("new temp answer" + i);
                                db.iAnswer(this.user, this.lesson, this.unit, this.question, "new temp answer" + i);
                            }
                        }


                        if (!this.question.Equals(this.questionTextbox.Text))
                        {
                            //check question rename
                            check = db.uQuestion(user, lesson, this.unitsComboBox.Text, this.unit, this.questionTextbox.Text, this.question, diffLevel, newAnswers, this.answers);
                        }
                        else
                        {
                            check = db.uQuestionWithoutName(user, lesson, this.unitsComboBox.Text, this.unit, this.question, diffLevel, newAnswers, this.answers);
                        }

                        if (check == 1)
                        {
                            father.loadQuestions();
                            MessageBox.Show("Η επεξεργασία ερώτησης ήταν επιτυχής");
                            this.Close();
                            this.Dispose();
                        }
                        else
                        {
                            this.errorsLabel.Text        += "Κάτι πήγε στραβά στην επεξεργασία ερώτησης, δοκιμάστε ξανά";
                            this.errorsLabel.Visible      = true;
                            this.errorTittleLabel.Visible = true;
                        }
                    }
                }
                else
                {
                    if (errors[0])
                    {
                        this.errorsLabel.Text += "Δε μπορείτε να καταχορήσετε λιγότερες από 2 ερωτήσεις";
                    }
                    if (errors[1])
                    {
                        this.errorsLabel.Text += "Δε μπορείτε να καταχορήσετε κενή απάντηση";
                    }
                    if (errors[2])
                    {
                        this.errorsLabel.Text += "Δε μπορείτε να καταχορήσετε κενό τίτλο ερώτησης.";
                    }

                    this.errorsLabel.Visible      = true;
                    this.errorTittleLabel.Visible = true;
                }
            }
            conf.Dispose();
        }