private void addButton_Click(object sender, EventArgs e) { try { string group = groupsBox.Text; List <string> checkedSubjects = new List <string>(); if (group == "") { throw new Exception("Jūs turite pasirinkti kažkurią grupę prieš priskirdami dėstomus dalykus"); } if (subjectsListBox.CheckedItems.Count == 0) { throw new Exception("Jūs nepasirinkote dėstomų dalykų"); } foreach (string item in subjectsListBox.CheckedItems) { checkedSubjects.Add(item); } foreach (String item in checkedSubjects) { subjectsListBox.Items.Remove(item); string title = item; otherData.AssignSubjectsToGroup(otherData.FindGroupsId(group), otherData.FindSubjectsId(title)); } MessageBox.Show("Sėkmingai priskyrėte dėstomus dalykus prie grupės"); foreach (int checkedItemIndex in subjectsListBox.CheckedIndices) { subjectsListBox.SetItemChecked(checkedItemIndex, false); } } catch (Exception exc) { MessageBox.Show(exc.Message); } }