Esempio n. 1
0
        private void AddAll_Click(object sender, EventArgs e)
        {
            bool flag = false;

            foreach (CheckBox x in Controls.OfType <CheckBox>())
            {
                if (x.Checked)
                {
                    flag = true;
                    break;
                }
            }
            foreach (RadioButton x in ControlPanel.Controls.OfType <RadioButton>())
            {
                if (x.Checked)
                {
                    flag = true;
                    break;
                }
            }
            //foreach (TextBox x in Controls.OfType<TextBox>())
            //    if (x.Text == "")
            //    {
            //        flag = false;
            //        break;
            //    }
            int buf;

            if (!flag ||
                DisciplineName.Text == "Название дисциплины" ||
                SemestrValue.Text == "" ||
                SNP.Text == "Ф.И.О" ||
                Pulpit.Text == "Кафедра" ||
                books.Count == 0 || LecturesCountValue.Text == "" ||
                LabsCountValue.Text == "" ||
                int.Parse(SemestrValue.Text) > 6)
            {
                MessageBox.Show("Некорректно введены данные");
            }
            else
            {
                discipline = new Discipline(DisciplineName.Text,
                                            int.Parse(SemestrValue.Text), (int)CourseValue.Value,
                                            new Spec(SpecializationPOIT.Checked, SpecializationPOIBMS.Checked,
                                                     SpecializationISIT.Checked, SpecializationDEVI.Checked),
                                            LecturesCount.Value, LabsCount.Value, ((exam.Checked) ? PassType.Exam : PassType.Test),
                                            new Lector(SNP.Text, Pulpit.Text, (int)NumberAudit.Value, (int)CorpusAydit.Value), books);


                var results = new List <ValidationResult>();
                var context = new ValidationContext(discipline);
                if (!Validator.TryValidateObject(discipline, context, results, true))
                {
                    foreach (var error in results)
                    {
                        string strWithErrroe = error.ErrorMessage;
                        MessageBox.Show(strWithErrroe);
                    }
                    return;
                }

                try
                {
                    DataContractJsonSerializer Serializer = new DataContractJsonSerializer(typeof(List <Discipline>));
                    using (FileStream File = new FileStream("Serialize.json", FileMode.Open))
                    {
                        disciplines = (List <Discipline>)Serializer.ReadObject(File);
                    }
                }
                catch { }
                disciplines.Add(discipline);
                DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(List <Discipline>));
                using (FileStream file = new FileStream("Serialize.json", FileMode.OpenOrCreate))
                {
                    serializer.WriteObject(file, disciplines);
                }
                disciplines.Clear();
                BookList.Items.Clear();
                books.Clear();
                CountOfObject.Text = countObj().ToString();
                LastAction.Text    = "Добавление дисциплины";
            }
        }