private void onSubjectsCheckstateChanged(object sender, ItemCheckEventArgs e) { int indexClasses = lbClasses.SelectedIndex; int indexSubject = e.Index; CheckState isChecked = e.NewValue; if (indexClasses < 0 || indexSubject < 0) return; lbClasses.ItemCheck -= new ItemCheckEventHandler(this.onClassesCheckstateChanged); if (classesWithLessons.Any(p => p.Name.Equals(classes.ElementAt(indexClasses).Name))) { if (isChecked == CheckState.Unchecked) { int indexClassInLessons = classesWithLessons.FindIndex(p => p.Name.Equals(classes.ElementAt(indexClasses).Name)); int indexSubjectInLesson = classesWithLessons.ElementAt(indexClassInLessons).Lessons.FindIndex(p => p.Subject.Token.Equals(subjects.ElementAt(indexSubject).Token)); if (indexClassInLessons < 0 || indexSubjectInLesson < 0) return; classesWithLessons.ElementAt(indexClassInLessons).Lessons.RemoveAt(indexSubjectInLesson); if (classesWithLessons.ElementAt(indexClassInLessons).Lessons.Count == 0) { lbClasses.SetItemChecked(indexClasses, false); classesWithLessons.RemoveAt(indexClassInLessons); } } else if (isChecked == CheckState.Checked) { int indexClassInLessons = classesWithLessons.FindIndex(p => p.Name.Equals(classes.ElementAt(indexClasses).Name)); classesWithLessons.ElementAt(indexClassInLessons).addLesson(new Lesson(teacher, subjects.ElementAt(indexSubject))); lbClasses.SetItemChecked(indexClasses, true); } } else { if (isChecked == CheckState.Checked) { Class newClass = new Class(classes.ElementAt(indexClasses).Name); newClass.addLesson(new Lesson(teacher, subjects.ElementAt(indexSubject))); classesWithLessons.Add(newClass); lbClasses.SetItemChecked(indexClasses, true); } } lbClasses.ItemCheck += new ItemCheckEventHandler(this.onClassesCheckstateChanged); }
private void initializeVariablesDone() { if (hadException) { return; } this.lbClasses.ItemCheck -= new ItemCheckEventHandler(onClassesCheckstateChanged); this.lbClasses.SelectedIndexChanged -= new EventHandler(onSelectedClassChanged); this.lbSubjects.ItemCheck -= new ItemCheckEventHandler(onSubjectsCheckstateChanged); foreach (JsonLesson jsonLesson in jsonLessons) { if (!classesWithLessons.Any(p => p.Name.Equals(jsonLesson.ClassToken))) { Lesson lesson = new Lesson(jsonLesson.Teachers, new Subject(jsonLesson.SubjectToken)); Class newClass = new Class(jsonLesson.ClassToken); newClass.addLesson(lesson); classesWithLessons.Add(newClass); } else { int index = classesWithLessons.FindIndex(p => p.Name.Equals(jsonLesson.ClassToken)); Lesson lesson = new Lesson(jsonLesson.Teachers, new Subject(jsonLesson.SubjectToken)); classesWithLessons.ElementAt(index).addLesson(lesson); } } oldClasses = (List<Class>)ObjectCloner.DeepClone(classesWithLessons); lbClasses.InvokeIfRequired(() => { lbClasses.Items.Clear(); }); lbSubjects.InvokeIfRequired(() => { lbSubjects.Items.Clear(); }); foreach (Class newClass in classes) { if (classesWithLessons.Any(p => p.Name.Equals(newClass.Name))) lbClasses.InvokeIfRequired(() => { lbClasses.Items.Add(newClass.Name, CheckState.Checked); }); else lbClasses.InvokeIfRequired(() => { lbClasses.Items.Add(newClass.Name, CheckState.Unchecked); }); } foreach (Subject subject in subjects) { if (classesWithLessons.Any(p => p.Name.Equals(classes.ElementAt(0).Name) && p.Lessons.Any(q => q.Subject.Token.Equals(subject.Token)))) lbSubjects.InvokeIfRequired(() => { lbSubjects.Items.Add(subject.Token + " - " + subject.Name, CheckState.Checked); }); else lbSubjects.InvokeIfRequired(() => { lbSubjects.Items.Add(subject.Token + " - " + subject.Name, CheckState.Unchecked); }); } lbClasses.InvokeIfRequired(() => { lbClasses.SelectionMode = SelectionMode.One; }); lbSubjects.InvokeIfRequired(() => { lbSubjects.SelectionMode = SelectionMode.One; }); lbClasses.InvokeIfRequired(() => { lbClasses.SelectedIndex = 0; }); btSaveChanges.InvokeIfRequired(() => { btSaveChanges.Enabled = true; }); lbClasses.InvokeIfRequired(() => { lbClasses.ItemCheck += new ItemCheckEventHandler(onClassesCheckstateChanged); lbClasses.SelectedIndexChanged += new EventHandler(onSelectedClassChanged); } ); lbSubjects.InvokeIfRequired(() => { lbSubjects.ItemCheck += new ItemCheckEventHandler(onSubjectsCheckstateChanged); }); }