//Обработка двойного клика по дереву. Редактирование названий предметов
        private void treeView1_AfterLabelEdit(object sender, NodeLabelEditEventArgs e)
        {
            if (e.Label != null)
            {
                using (TestAppLibrary.TestAppContext db = new TestAppLibrary.TestAppContext())
                {
                    if (e.Node.Level == 0)
                    {
                        var sub = db.Subjects.Where(i => i.Id == ((TestAppLibrary.Subject)e.Node.Tag).Id).FirstOrDefault();
                        if (sub != null)
                        {
                            if (e.Label != null)
                            {
                                sub.Name = e.Label;
                            }
                        }
                    }
                    else if (e.Node.Level == 1)
                    {
                        var ex = db.Exams.Where(i => i.Id == ((TestAppLibrary.Exam)e.Node.Tag).Id).FirstOrDefault();
                        if (ex != null)
                        {
                            if (e.Label != null)
                            {
                                ex.Name = e.Label;
                            }
                        }
                    }

                    db.SaveChanges();
                }
                e.Node.EndEdit(false);
                TreeViewFill();
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == String.Empty)
            {
                MessageBox.Show("Необходимо ввести название вопроса");
                return;
            }
            TestAppLibrary.Question q = new TestAppLibrary.Question();
            using (TestAppLibrary.TestAppContext db = new TestAppLibrary.TestAppContext())
            {
                q.Challenge     = textBox1.Text;
                q.AnswersNumber = (int)numericUpDown1.Value;
                q.MultiChoice   = radioButton1.Checked;
                q.Exam          = db.Exams.Where(i => i.Id == id).First();
                db.Questions.Add(q);
                db.SaveChanges();


                FormAddAnswers fa     = new FormAddAnswers(form, tree, textBox1, (int)numericUpDown1.Value, radioButton1.Checked, q.Id);
                DialogResult   result = fa.ShowDialog();
                if (result == DialogResult.Cancel)
                {
                    db.Questions.Remove(q);
                    db.SaveChanges();
                }
            }
            this.Close();
        }
예제 #3
0
 //Если пользователя с данным логином нет в базе - предлагается его создать
 // Если данный логин есть в базе, но под другим паролем, выводится сообщение об ошибке
 private void button1_Click(object sender, EventArgs e)
 {
     using (TestAppLibrary.TestAppContext db = new TestAppLibrary.TestAppContext())
     {
         var users = db.Users.Where(i => i.Name == textBox1.Text && i.Password == textBox2.Text).FirstOrDefault();
         if (users != null)
         {
             form.UserId = users.Id;
         }
         else
         {
             var users2 = db.Users.Where(i => i.Name == textBox1.Text && i.Password != textBox2.Text).FirstOrDefault();
             if (users2 != null)
             {
                 MessageBox.Show("Пароль пользователя введен неверно");
                 return;
             }
             else
             {
                 DialogResult result = MessageBox.Show("Вы хотите создать нового пользователя?", "Создание нового пользователя", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                 if (result == DialogResult.Yes)
                 {
                     TestAppLibrary.User user = new TestAppLibrary.User {
                         Name = textBox1.Text, Password = textBox2.Text
                     };
                     db.Users.Add(user);
                     db.SaveChanges();
                 }
                 return;
             }
         }
     }
     this.DialogResult = DialogResult.OK;
     this.Close();
 }
예제 #4
0
 //При загрузке формы, создается коллекция вопросов, связанная с данным экзаменом и выводится первый вопрос на форму
 private void FormUserAnswer_Load(object sender, EventArgs e)
 {
     try
     {
         using (TestAppLibrary.TestAppContext db = new TestAppLibrary.TestAppContext())
         {
             q    = db.Questions.Where(i => i.Exam.Id == idExam).ToList();
             ex   = db.Exams.Where(i => i.Id == idExam).FirstOrDefault();
             user = db.Users.Where(i => i.Id == idUser).FirstOrDefault();
             TestAppLibrary.Question quest = q[count++];
             textBox1.Text = quest.Challenge;
             Control ctrl;
             textBox1.ReadOnly = true;
             table             = new TableLayoutPanel();
             table.Parent      = splitContainer2.Panel1;
             table.Dock        = DockStyle.Fill;
             table.ColumnCount = 2;
             table.Padding     = new Padding(20, 10, 10, 20);
             table.AutoScroll  = true;
             int k = 0;
             ans = db.Answers.Where(i => i.Question.Id == quest.Id).ToList();
             for (int j = 0; j < quest.AnswersNumber && j < ans.Count; j++)
             {
                 if (quest.MultiChoice == false)
                 {
                     ctrl = new RadioButton();
                 }
                 else
                 {
                     ctrl = new CheckBox();
                 }
                 ctrl.Parent = table;
                 TextBox t = new TextBox();
                 t.Parent    = table;
                 t.Multiline = true;
                 t.WordWrap  = true;
                 t.Size      = new Size {
                     Width = 250, Height = 50
                 };
                 t.Text = ans[k++].Challenge;
             }
             table.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 20));
             //Инициализация и запуск таймера
             t          = new Timer();
             t.Interval = 1000;
             t.Tick    += OnTimer;
             time       = new TimeSpan(ex.TimeToPass / 60, ex.TimeToPass % 60, 0);
             t.Start();
         }
     }
     catch (Exception e1)
     {
         MessageBox.Show(e1.Message);
     }
 }
예제 #5
0
 private void FormTestPass_Load(object sender, EventArgs e)
 {
     using (TestAppLibrary.TestAppContext db = new TestAppLibrary.TestAppContext())
     {
         q    = db.Questions.Where(i => i.Exam.Id == idExam).ToList();
         ex   = db.Exams.Where(i => i.Id == idExam).FirstOrDefault();
         user = db.Users.Where(i => i.Id == idUser).FirstOrDefault();
     }
     textBox1.Text = ex.Name;
     textBox2.Text = ex.QuestionNumber.ToString();
     textBox3.Text = new TimeSpan(ex.TimeToPass / 60, ex.TimeToPass % 60, 0).ToString();
 }
예제 #6
0
 private void FormAddExam_Load(object sender, EventArgs e)
 {
     using (TestAppLibrary.TestAppContext db = new TestAppLibrary.TestAppContext())
     {
         var subjs = db.Subjects.ToList();
         foreach (var i in subjs)
         {
             comboBox1.Items.Add(i.Name);
         }
         comboBox1.SelectedItem = ((TestAppLibrary.Subject)tree.SelectedNode.Tag).Name;
     }
 }
예제 #7
0
 private void FormRedactExam_Load(object sender, EventArgs e)
 {
     using (TestAppLibrary.TestAppContext db = new TestAppLibrary.TestAppContext())
     {
         var exam = db.Exams.Where(i => i.Id == id).First();
         db.Entry(exam).Reference(i => i.Subject).Load();
         numericUpDown1.Value  = exam.QuestionNumber;
         numericUpDown2.Value  = exam.QuestionToPass;
         textBox1.Text         = exam.Subject.Name;
         textBox2.Text         = exam.Name;
         dateTimePicker1.Value = DateTime.Parse((int)exam.TimeToPass / 60 + ":" + (int)exam.TimeToPass % 60);
     }
 }
예제 #8
0
        private void button1_Click(object sender, EventArgs e)
        {
            foreach (var c in table.Controls)
            {
                if (c.GetType().Name == "TextBox")
                {
                    if (((TextBox)c).Text == String.Empty)
                    {
                        MessageBox.Show("Заполните все поля");
                        return;
                    }
                }
            }

            var radios = table.Controls.OfType <RadioButton>().ToList();
            var checks = table.Controls.OfType <CheckBox>().ToList();
            var texts  = table.Controls.OfType <TextBox>().ToList();

            using (TestAppLibrary.TestAppContext db = new TestAppLibrary.TestAppContext())
            {
                if (multiChoice == true)
                {
                    for (int i = 0; i < texts.Count; i++)
                    {
                        TestAppLibrary.Answer ans = new TestAppLibrary.Answer {
                            Challenge = texts[i].Text, IsRight = checks[i].Checked, Question = db.Questions.Where(k => k.Id == id).First()
                        };
                        db.Answers.Add(ans);
                    }
                }
                else if (multiChoice == false)
                {
                    for (int i = 0; i < texts.Count; i++)
                    {
                        TestAppLibrary.Answer ans = new TestAppLibrary.Answer {
                            Challenge = texts[i].Text, IsRight = radios[i].Checked, Question = db.Questions.Where(k => k.Id == id).First()
                        };
                        db.Answers.Add(ans);
                    }
                }

                db.SaveChanges();

                form.TreeViewFill();
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }
예제 #9
0
 private void FormQuestResult_Load(object sender, EventArgs e)
 {
     using (TestAppLibrary.TestAppContext db = new TestAppLibrary.TestAppContext())
     {
         textBox1.Text = db.Questions.Where(i => i.Id == questId).FirstOrDefault().Challenge;
         var answers = db.Answers.Where(i => i.Question.Id == questId).ToList();
         foreach (var a in answers)
         {
             Control ctrl;
             TestAppLibrary.UserAnswer ua = new TestAppLibrary.UserAnswer();
             ua = db.UserAnswers.Where(i => i.Answer.Id == a.Id && i.User.Id == userId).FirstOrDefault();
             if (db.Questions.Where(i => i.Id == questId).FirstOrDefault().MultiChoice == false)
             {
                 ctrl = new RadioButton();
                 if (ua.IsPicked == true)
                 {
                     ((RadioButton)ctrl).Checked = true;
                 }
             }
             else
             {
                 ctrl = new CheckBox();
                 if (ua.IsPicked == true)
                 {
                     ((CheckBox)ctrl).Checked = true;
                 }
             }
             ctrl.Parent = table;
             TextBox t = new TextBox();
             t.Parent    = table;
             t.Multiline = true;
             t.WordWrap  = true;
             t.Size      = new Size {
                 Width = 250, Height = 50
             };
             t.Text = a.Challenge;
             if (a.IsRight == true)
             {
                 t.ForeColor = Color.Green;
             }
             else
             {
                 t.ForeColor = Color.Red;
             }
         }
     }
 }
 public FormRedactSubject(FormMain f, int id, bool regime)
 {
     InitializeComponent();
     this.id = id;
     form    = f;
     Text    = "Редактитрование предмета";
     if (regime == false)
     {
         textBox1.ReadOnly = true;
         button2.Visible   = false;
         Text = "Просмотр предмета";
     }
     using (TestAppLibrary.TestAppContext db = new TestAppLibrary.TestAppContext())
     {
         var subject = db.Subjects.Where(i => i.Id == id).First();
         textBox1.Text = subject.Name;
     }
 }
예제 #11
0
 private void button1_Click(object sender, EventArgs e)
 {
     using (TestAppLibrary.TestAppContext db = new TestAppLibrary.TestAppContext())
     {
         var exam = db.Exams.Where(i => i.Id == id).First();
         if (numericUpDown1.Value < numericUpDown2.Value)
         {
             MessageBox.Show("Количество вопросов не совпадает с количеством ответов");
             return;
         }
         exam.QuestionNumber = (int)numericUpDown1.Value;
         exam.QuestionToPass = (int)numericUpDown2.Value;
         exam.Name           = textBox2.Text;
         exam.TimeToPass     = dateTimePicker1.Value.Hour * 60 + dateTimePicker1.Value.Minute;
         db.SaveChanges();
         form.TreeViewFill();
         this.Close();
     }
 }
 private void button2_Click(object sender, EventArgs e)
 {
     using (TestAppLibrary.TestAppContext db = new TestAppLibrary.TestAppContext())
     {
         var answers = db.Answers.Where(i => i.Question.Id == questionId).ToList();
         var texts   = table.Controls.OfType <TextBox>().ToList();
         var radios  = table.Controls.OfType <RadioButton>().ToList();
         var checks  = table.Controls.OfType <CheckBox>().ToList();
         for (int i = 0; i < answers.Count; i++)
         {
             TestAppLibrary.Answer ans = answers[i];
             ans.Challenge = texts[i].Text;
             ans.Question  = db.Questions.Where(j => j.Id == questionId).FirstOrDefault();
             ans.IsRight   = (radios.Count != 0) ? radios[i].Checked : checks[i].Checked;
         }
         db.SaveChanges();
         this.Close();
     }
 }
 private void button1_Click(object sender, EventArgs e)
 {
     using (TestAppLibrary.TestAppContext db = new TestAppLibrary.TestAppContext())
     {
         var subject = db.Subjects.Where(i => i.Id == id).First();
         if (textBox1.Text == String.Empty)
         {
             MessageBox.Show("Введите название предмета");
             return;
         }
         var temp = db.Subjects.Where(i => i.Name == textBox1.Text).FirstOrDefault();
         if (temp == null)
         {
             subject.Name = textBox1.Text;
             db.SaveChanges();
             form.TreeViewFill();
             this.Close();
         }
     }
 }
 private void FormRedactAnswer_Load(object sender, EventArgs e)
 {
     using (TestAppLibrary.TestAppContext db = new TestAppLibrary.TestAppContext())
     {
         textBox1.Text = db.Questions.Where(i => i.Id == questionId).FirstOrDefault().Challenge;
         var answers = db.Answers.Where(i => i.Question.Id == questionId).ToList();
         foreach (var a in answers)
         {
             Control ctrl;
             if (db.Questions.Where(i => i.Id == questionId).FirstOrDefault().MultiChoice == false)
             {
                 ctrl = new RadioButton();
                 if (a.IsRight == true)
                 {
                     ((RadioButton)ctrl).Checked = true;
                 }
             }
             else
             {
                 ctrl = new CheckBox();
                 if (a.IsRight == true)
                 {
                     ((CheckBox)ctrl).Checked = true;
                 }
             }
             ctrl.Parent = table;
             TextBox t = new TextBox();
             t.Parent    = table;
             t.Multiline = true;
             t.WordWrap  = true;
             t.Size      = new Size {
                 Width = 250, Height = 50
             };
             t.Text = a.Challenge;
             if (IsForRedact == false)
             {
                 t.ReadOnly = true;
             }
         }
     }
 }
예제 #15
0
 private void btnOk_Click(object sender, EventArgs e)
 {
     using (TestAppLibrary.TestAppContext db = new TestAppLibrary.TestAppContext())
     {
         TestAppLibrary.Exam ex = new TestAppLibrary.Exam();
         ex.Name = textBox1.Text;
         if (numericUpDown1.Value < numericUpDown2.Value)
         {
             MessageBox.Show("Количество вопросов не совпадает с количеством ответов");
             return;
         }
         ex.QuestionNumber = (int)numericUpDown1.Value;
         ex.QuestionToPass = (int)numericUpDown2.Value;
         ex.TimeToPass     = dateTimePicker1.Value.Hour * 60 + dateTimePicker1.Value.Minute;
         ex.Subject        = db.Subjects.Where(i => i.Name == (string)comboBox1.SelectedItem).First();
         db.Exams.Add(ex);
         db.SaveChanges();
         form.TreeViewFill();
         this.Close();
     }
 }
예제 #16
0
 private void btnOk_Click(object sender, EventArgs e)
 {
     using (TestAppLibrary.TestAppContext db = new TestAppLibrary.TestAppContext())
     {
         var temp = db.Subjects.Where(i => i.Name == textBox1.Text).FirstOrDefault();
         if (temp == null)
         {
             TestAppLibrary.Subject sub = new TestAppLibrary.Subject {
                 Name = textBox1.Text
             };
             db.Subjects.Add(sub);
             db.SaveChanges();
             form.TreeViewFill();
             this.Close();
         }
         else
         {
             MessageBox.Show("Такой предмет уже есть");
             return;
         }
     }
 }
 //Метод заполнения дерева
 public void TreeViewFill()
 {
     treeView1.Nodes.Clear();
     imageList1.ColorDepth = ColorDepth.Depth32Bit;
     using (TestAppLibrary.TestAppContext db = new TestAppLibrary.TestAppContext())
     {
         foreach (var item in db.Subjects.ToList())
         {
             int imageIndex = 0;
             if (item.Name.ToLower().Contains("html"))
             {
                 imageIndex = 0;
             }
             else if (item.Name.ToLower().Contains("script"))
             {
                 imageIndex = 1;
             }
             else if (item.Name.ToLower().Contains("python"))
             {
                 imageIndex = 2;
             }
             else if (item.Name.ToLower().Contains("php"))
             {
                 imageIndex = 3;
             }
             else if (item.Name.ToLower().Contains("react"))
             {
                 imageIndex = 4;
             }
             else if (item.Name.ToLower().Contains("ruby"))
             {
                 imageIndex = 5;
             }
             else if (item.Name.ToLower().Contains("sass"))
             {
                 imageIndex = 6;
             }
             else if (item.Name.ToLower().Contains("c++"))
             {
                 imageIndex = 7;
             }
             else if (item.Name.ToLower().Contains("c#"))
             {
                 imageIndex = 8;
             }
             else if (item.Name.ToLower().Contains("git"))
             {
                 imageIndex = 9;
             }
             else if (item.Name.ToLower().Contains("java"))
             {
                 imageIndex = 10;
             }
             else if (item.Name.ToLower().Contains("win"))
             {
                 imageIndex = 11;
             }
             else if (item.Name.ToLower().Contains("anjular"))
             {
                 imageIndex = 12;
             }
             else if (item.Name.ToLower().Contains("android"))
             {
                 imageIndex = 13;
             }
             else if (item.Name.ToLower().Contains("sql"))
             {
                 imageIndex = 14;
             }
             else if (item.Name.ToLower().Contains("c"))
             {
                 imageIndex = 15;
             }
             else
             {
                 imageIndex = 11;
             }
             TreeNode node = new TreeNode(item.Name, imageIndex, imageIndex);
             node.Tag = item;
             foreach (var examItem in db.Exams.ToList())
             {
                 if (examItem.Subject.Id == item.Id)
                 {
                     TreeNode examNode = new TreeNode(examItem.Name, 15, 15);
                     examNode.Tag = examItem;
                     int i = 1;
                     foreach (var question in db.Questions.ToList())
                     {
                         if (question.Exam.Id == examItem.Id)
                         {
                             TreeNode qNode = new TreeNode("Вопрос №" + i, 16, 16);
                             i++;
                             qNode.Tag = question;
                             examNode.Nodes.Add(qNode);
                         }
                     }
                     node.Nodes.Add(examNode);
                 }
             }
             treeView1.Nodes.Add(node);
         }
     }
 }
예제 #18
0
 //Обработка нажатия на кнопку "Следующий вопрос"
 private void button1_Click(object sender, EventArgs e)
 {
     //В начале происходит выборка предыдущих ответов пользователя. Результаты выбора пользователя заносятся в базу данных
     using (TestAppLibrary.TestAppContext db = new TestAppLibrary.TestAppContext())
     {
         TestAppLibrary.Question que = q[count - 1];
         var answer = db.Answers.Where(i => i.Question.Id == que.Id).ToList();
         if (q[count - 1].MultiChoice == true)
         {
             var boxes = table.Controls.OfType <CheckBox>();
             int i     = 0;
             foreach (var box in boxes)
             {
                 TestAppLibrary.UserAnswer uans = new TestAppLibrary.UserAnswer();
                 var user = db.Users.Where(l => l.Id == idUser).FirstOrDefault();
                 uans.User = user;
                 TestAppLibrary.Answer ans1 = answer[i++];
                 uans.Answer   = ans1;
                 uans.IsPicked = box.Checked;
                 db.UserAnswers.Add(uans);
             }
         }
         else
         {
             var radios = table.Controls.OfType <RadioButton>();
             int i      = 0;
             foreach (var radio in radios)
             {
                 TestAppLibrary.UserAnswer uans = new TestAppLibrary.UserAnswer();
                 var user = db.Users.Where(l => l.Id == idUser).FirstOrDefault();
                 uans.User = user;
                 TestAppLibrary.Answer ans1 = answer[i++];
                 uans.Answer   = ans1;
                 uans.IsPicked = radio.Checked;
                 db.UserAnswers.Add(uans);
             }
         }
         //Выведение на форму следующего вопроса
         TestAppLibrary.Question quest = q[count++];
         textBox1.Text = quest.Challenge;
         Control ctrl;
         textBox1.ReadOnly = true;
         table             = new TableLayoutPanel();
         table.Parent      = splitContainer2.Panel1;
         table.Dock        = DockStyle.Fill;
         table.ColumnCount = 2;
         table.Padding     = new Padding(20, 10, 10, 20);
         table.AutoScroll  = true;
         table.BringToFront();
         table.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 20));
         int k   = 0;
         var ans = db.Answers.Where(i => i.Question.Id == quest.Id).ToList();
         for (int j = 0; j < quest.AnswersNumber && j < ans.Count; j++)
         {
             if (quest.MultiChoice == false)
             {
                 ctrl = new RadioButton();
             }
             else
             {
                 ctrl = new CheckBox();
             }
             ctrl.Parent = table;
             TextBox t = new TextBox();
             t.Parent    = table;
             t.Multiline = true;
             t.WordWrap  = true;
             t.Size      = new Size {
                 Width = 250, Height = 50
             };
             t.Text = ans[k++].Challenge;
         }
         //Если вопросов больше нет в коллекции сделать неактивной кнопку продолжения
         if (count == q.Count)
         {
             button1.Enabled = false;
         }
         db.SaveChanges();
     }
 }
예제 #19
0
        //Обработка нажатия на кнопку "Завершить"
        private void button2_Click(object sender, EventArgs e)
        {
            t.Stop();
            int rAns = 0;

            // Выборка ответов пользователя на последний вопрос
            using (TestAppLibrary.TestAppContext db = new TestAppLibrary.TestAppContext())
            {
                TestAppLibrary.Question que = q[count - 1];
                var answer = db.Answers.Where(i => i.Question.Id == que.Id).ToList();
                if (q[count - 1].MultiChoice == true)
                {
                    var boxes = table.Controls.OfType <CheckBox>();
                    int i     = 0;
                    foreach (var box in boxes)
                    {
                        TestAppLibrary.UserAnswer uans = new TestAppLibrary.UserAnswer();
                        var user = db.Users.Where(l => l.Id == idUser).FirstOrDefault();
                        uans.User = user;
                        TestAppLibrary.Answer ans1 = answer[i++];
                        uans.Answer   = ans1;
                        uans.IsPicked = box.Checked;
                        db.UserAnswers.Add(uans);
                    }
                }
                else
                {
                    var radios = table.Controls.OfType <RadioButton>();
                    int i      = 0;
                    foreach (var radio in radios)
                    {
                        TestAppLibrary.UserAnswer uans = new TestAppLibrary.UserAnswer();
                        var user = db.Users.Where(l => l.Id == idUser).FirstOrDefault();
                        uans.User = user;
                        TestAppLibrary.Answer ans1 = answer[i++];
                        uans.Answer   = ans1;
                        uans.IsPicked = radio.Checked;
                        db.UserAnswers.Add(uans);
                    }
                }
                db.SaveChanges();
                //В случае, если пользователь прервал экзамен до ответа на все вопросы,
                //создаются записи для соблюдения целостности базы
                if (IsAllFinished == false)
                {
                    for (int i = count; i < q.Count; i++)
                    {
                        TestAppLibrary.Question quest = q[i];
                        var answers = db.Answers.Where(j => j.Question.Id == quest.Id).ToList();
                        foreach (var a in answers)
                        {
                            TestAppLibrary.UserAnswer uans = new TestAppLibrary.UserAnswer();
                            var us = db.Users.Where(l => l.Id == idUser).FirstOrDefault();
                            uans.User = us;
                            TestAppLibrary.Answer ans1 = a;
                            uans.Answer   = ans1;
                            uans.IsPicked = false;
                            db.UserAnswers.Add(uans);
                        }
                    }
                    db.SaveChanges();
                }
                //Проверка ответов пользователя
                foreach (var q in db.Questions.Where(i => i.Exam.Id == idExam).ToList())
                {
                    var ans      = db.Answers.Where(i => i.Question.Id == q.Id).ToList();
                    int questAns = 0;
                    foreach (var a in ans)
                    {
                        var ua = db.UserAnswers.Where(i => i.User.Id == idUser && i.Answer.Id == a.Id).FirstOrDefault();
                        if (ua != null)
                        {
                            if (ua.IsPicked == a.IsRight)
                            {
                                questAns += 0;
                            }
                            else
                            {
                                questAns += 1;
                            }
                        }
                    }
                    TestAppLibrary.UserQuestion uq = new TestAppLibrary.UserQuestion();
                    var user = db.Users.Where(l => l.Id == idUser).FirstOrDefault();
                    uq.User     = user;
                    uq.Question = db.Questions.Where(i => i.Id == q.Id).FirstOrDefault();
                    uq.IsRight  = (questAns == 0) ? true : false;
                    if (uq.IsRight == true)
                    {
                        rAns += 1;
                    }
                    db.UserQuestions.Add(uq);
                    db.SaveChanges();
                }

                TestAppLibrary.UserExams ue = db.UserExams.Where(i => i.User.Id == idUser && i.Exam.Id == idExam).FirstOrDefault();
                ue.IsPassed = (ex.QuestionToPass > rAns) ? false : true;
                db.SaveChanges();
                //Вызов формы с финальным результатом теста
                FormExamResult fer = new FormExamResult(this, q.Count, ex.QuestionToPass, rAns);
                fer.ShowDialog();
                this.Close();
            }
        }
        private void FormRedactQuestion_Load(object sender, EventArgs e)
        {
            try
            {
                using (TestAppLibrary.TestAppContext db = new TestAppLibrary.TestAppContext())
                {
                    var quest = db.Questions.Where(i => i.Id == id).FirstOrDefault();
                    if (quest != null)
                    {
                        textBox1.Text        = quest.Challenge;
                        numericUpDown1.Value = quest.AnswersNumber;
                        if (quest.MultiChoice == true)
                        {
                            radioButton1.Checked = true;
                        }
                        radioButton2.Checked = true;
                        var     answers = db.Answers.Where(i => i.Question.Id == quest.Id).ToList();
                        Control ctrl;
                        for (int j = 0; j < answers.Count; j++)
                        {
                            if (quest.MultiChoice == false)
                            {
                                ctrl = new RadioButton();
                            }
                            else
                            {
                                ctrl = new CheckBox();
                            }
                            ctrl.Location = new Point {
                                X = 30, Y = 200 * (j + 1)
                            };
                            TextBox t = new TextBox();
                            t.Location = new Point {
                                X = 30, Y = 200 * (j + 1) + 30
                            };
                            t.Size = new Size {
                                Width = 200, Height = 50
                            };
                            t.Multiline = true;
                            t.WordWrap  = true;
                            t.Text      = answers[j].Challenge;
                            if (answers[j].IsRight == true)
                            {
                                if (quest.MultiChoice == false)
                                {
                                    ((RadioButton)ctrl).Checked = true;
                                }
                                else
                                {
                                    ((CheckBox)ctrl).Checked = true;
                                }
                            }

                            this.Controls.Add(ctrl);
                            this.Controls.Add(t);
                        }
                    }
                }
            }
            catch (Exception e1)
            {
                MessageBox.Show(e1.Message);
            }
        }
 //Удаление узлов
 private void tsBtnDelete_Click(object sender, EventArgs e)
 {
     try
     {
         using (TestAppLibrary.TestAppContext db = new TestAppLibrary.TestAppContext())
         {
             int level = treeView1.SelectedNode.Level;
             if (level == 0)
             {
                 var subject = db.Subjects.Where(i => i.Id == ((TestAppLibrary.Subject)treeView1.SelectedNode.Tag).Id).FirstOrDefault();
                 if (subject != null)
                 {
                     var exams = db.Exams.Where(i => i.Subject.Id == subject.Id).ToList();
                     foreach (var ex in exams)
                     {
                         foreach (var q in db.Questions.Where(i => i.Exam.Id == ex.Id).ToList())
                         {
                             foreach (var ans in db.Answers.Where(i => i.Question.Id == q.Id).ToList())
                             {
                                 foreach (var ua in db.UserAnswers.Where(i => i.Answer.Id == ans.Id).ToList())
                                 {
                                     db.UserAnswers.Remove(ua);
                                 }
                                 db.Answers.Remove(ans);
                             }
                             foreach (var uq in db.UserQuestions.Where(i => i.Question.Id == q.Id).ToList())
                             {
                                 db.UserQuestions.Remove(uq);
                             }
                             db.Questions.Remove(q);
                         }
                         foreach (var ue in db.UserExams.Where(i => i.Exam.Id == ex.Id).ToList())
                         {
                             db.UserExams.Remove(ue);
                         }
                         db.Exams.Remove(ex);
                     }
                     db.Subjects.Remove(subject);
                     db.SaveChanges();
                     TreeViewFill();
                 }
             }
             else if (level == 1)
             {
                 var exam = db.Exams.Where(i => i.Id == ((TestAppLibrary.Exam)treeView1.SelectedNode.Tag).Id).FirstOrDefault();
                 if (exam != null)
                 {
                     foreach (var q in db.Questions.Where(i => i.Exam.Id == exam.Id).ToList())
                     {
                         foreach (var ans in db.Answers.Where(i => i.Question.Id == q.Id).ToList())
                         {
                             foreach (var ua in db.UserAnswers.Where(i => i.Answer.Id == ans.Id).ToList())
                             {
                                 db.UserAnswers.Remove(ua);
                             }
                             db.Answers.Remove(ans);
                         }
                         foreach (var uq in db.UserQuestions.Where(i => i.Question.Id == q.Id).ToList())
                         {
                             db.UserQuestions.Remove(uq);
                         }
                         db.Questions.Remove(q);
                     }
                     foreach (var ue in db.UserExams.Where(i => i.Exam.Id == exam.Id).ToList())
                     {
                         db.UserExams.Remove(ue);
                     }
                     db.Exams.Remove(exam);
                     db.SaveChanges();
                     TreeViewFill();
                 }
             }
             else if (level == 2)
             {
                 var question = db.Questions.Where(i => i.Id == ((TestAppLibrary.Question)treeView1.SelectedNode.Tag).Id).FirstOrDefault();
                 if (question != null)
                 {
                     foreach (var ans in db.Answers.Where(i => i.Question.Id == question.Id).ToList())
                     {
                         foreach (var ua in db.UserAnswers.Where(i => i.Answer.Id == ans.Id).ToList())
                         {
                             db.UserAnswers.Remove(ua);
                         }
                         db.Answers.Remove(ans);
                     }
                     foreach (var que in db.UserQuestions.Where(i => i.Question.Id == question.Id).ToList())
                     {
                         db.UserQuestions.Remove(que);
                     }
                     db.Questions.Remove(question);
                     db.SaveChanges();
                     TreeViewFill();
                 }
             }
         }
     }
     catch (Exception e1)
     {
         MessageBox.Show(e1.Message);
     }
 }
        //Управление доступом к пунктам меню и панели управления
        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            int level = treeView1.SelectedNode.Level;

            if (level == 0)
            {
                tsBtnAddQuestion.Enabled = false;
                tsBtnAddExam.Enabled     = true;
                tsBtnRedact.Enabled      = true;
                tsBtnDelete.Enabled      = true;
                tsBtnPreview.Enabled     = true;
                tsmiAddEx.Enabled        = true;
                tsmiAddSub.Enabled       = true;
                tsmiAddQuest.Enabled     = false;
                tsmiDelEx.Enabled        = false;
                tsmiDelSub.Enabled       = true;
                tsmiDelQuest.Enabled     = false;
                tsmiEditEx.Enabled       = false;
                tsmiEditQuest.Enabled    = false;
                tsmiEditSub.Enabled      = true;
                tsmiViewSub.Enabled      = true;
                tsmiViewQuest.Enabled    = false;
                tsmiViewEx.Enabled       = false;
            }
            else if (level == 1)
            {
                tsBtnAddExam.Enabled     = false;
                tsBtnAddQuestion.Enabled = true;
                tsBtnRedact.Enabled      = true;
                tsmiAddEx.Enabled        = false;
                tsmiAddSub.Enabled       = true;
                tsmiAddQuest.Enabled     = true;
                tsmiDelEx.Enabled        = true;
                tsmiDelSub.Enabled       = false;
                tsmiDelQuest.Enabled     = false;
                tsmiEditEx.Enabled       = true;
                tsmiEditQuest.Enabled    = false;
                tsmiEditSub.Enabled      = false;
                tsmiViewSub.Enabled      = false;
                tsmiViewQuest.Enabled    = false;
                tsmiViewEx.Enabled       = true;
                try
                {
                    using (TestAppLibrary.TestAppContext db = new TestAppLibrary.TestAppContext())
                    {
                        int count = db.Questions.Where(i => i.Exam.Id == ((TestAppLibrary.Exam)treeView1.SelectedNode.Tag).Id).Count();
                        if (count == ((TestAppLibrary.Exam)treeView1.SelectedNode.Tag).QuestionNumber)
                        {
                            tsBtnAddQuestion.Enabled = false;
                        }
                    }
                }
                catch (Exception e1)
                {
                    MessageBox.Show(e1.Message);
                }
            }
            else if (level == 2)
            {
                tsBtnAddExam.Enabled     = false;
                tsBtnAddQuestion.Enabled = false;
                tsBtnRedact.Enabled      = true;
                tsmiAddEx.Enabled        = true;
                tsmiAddSub.Enabled       = false;
                tsmiAddQuest.Enabled     = false;
                tsmiDelEx.Enabled        = true;
                tsmiDelSub.Enabled       = false;
                tsmiDelQuest.Enabled     = false;
                tsmiEditEx.Enabled       = false;
                tsmiEditQuest.Enabled    = true;
                tsmiEditSub.Enabled      = false;
                tsmiViewSub.Enabled      = false;
                tsmiViewQuest.Enabled    = true;
                tsmiViewEx.Enabled       = false;
            }
            PropertyGrid prop = new PropertyGrid();

            prop.SelectedObject = treeView1.SelectedNode.Tag;
            prop.Parent         = splitContainer1.Panel2;
            prop.Dock           = DockStyle.Fill;
            prop.BringToFront();
        }