private void AddStudentButton_Click(object sender, RoutedEventArgs e) { ListBoxItem studentListBoxItem = new ListBoxItem(); studentListBoxItem.Content = LastNameTextBox.Text + " " + FirstNameTextBox.Text; StudentsListBox.Items.Add(studentListBoxItem); if (LevelBox.Text == "Undergraduate") { UndergraduateStudent tempStudent = new UndergraduateStudent(); tempStudent.Age = Convert.ToInt32(AgeTextBox.Text); tempStudent.FirstName = FirstNameTextBox.Text; tempStudent.LastName = LastNameTextBox.Text; tempStudent.Gender = GenderBox.SelectedIndex; tempStudent.StudentID = Convert.ToInt32(StudentIDTextBox.Text); students.Add(tempStudent); } else { GraduateStudent tempStudent = new GraduateStudent(); tempStudent.Age = Convert.ToInt32(AgeTextBox.Text); tempStudent.FirstName = FirstNameTextBox.Text; tempStudent.LastName = LastNameTextBox.Text; tempStudent.Gender = GenderBox.SelectedIndex; tempStudent.StudentID = Convert.ToInt32(StudentIDTextBox.Text); students.Add(tempStudent); } StudentsListBox.SelectedItem = StudentsListBox.Items.GetItemAt(StudentsListBox.Items.Count - 1); }
private void AddStudentButton_Click(object sender, RoutedEventArgs e) { UndergraduateStudent test = new UndergraduateStudent(); try //test if in correct format if not cancel action by return { test.Confirm(FirstNameTextBox.Text, LastNameTextBox.Text, Convert.ToInt32(StudentIDTextBox.Text), comboBox.Text, levelBox.Text, Convert.ToInt32(AgeTextBox.Text)); } catch (OverflowException) { MessageBox.Show("ID or age to large please try again"); return; } catch (FormatException) { MessageBox.Show(" Input was not in proper format\n" + "\t- All fields are required"); return; } //if no exception fill usuable variables string fName = FirstNameTextBox.Text; string lName = LastNameTextBox.Text; int id = Convert.ToInt32(StudentIDTextBox.Text); string gender = comboBox.Text; string level = levelBox.Text; int age = Convert.ToInt32(AgeTextBox.Text); string nameTest = lName + fName; string name = fName + " " + lName; try { while (storage != null) { for (int counter = 0; counter < storage.Length; counter++) { string checkName = storage[counter].LastName + storage[counter].FirstName; int checkID = storage[counter].ID; if (!test.StudentCheck(nameTest, checkName, id, checkID)) { MessageBox.Show(nameTest + "\n" + checkName + "\n" + Convert.ToString(id) + "\n" + Convert.ToString(checkID)); return; } } continue; } } catch (NullReferenceException) { //do nothing, storage array is empty and nothing to compare it to } if (levelBox.Text == test.Check) //if student will be undergraduate then make one and put into array for later use { UndergraduateStudent student = new UndergraduateStudent(fName, lName, gender, age, id, level); storage[i] = student; MessageBox.Show(name + " Added to storage " + "\n"); } else //else must be grad student, create one, put into storage { GraduateStudent gStudent = new GraduateStudent(fName, lName, gender, age, id, level); storage[i] = gStudent; MessageBox.Show(name + " Added to storage " + "\n"); } //populate list box with relevant info ListBoxItem studentListBoxItem = new ListBoxItem(); studentListBoxItem.Content = storage[i].LastName + " " + storage[i].FirstName + "\n" + storage[i].ID + "\n" + storage[i].Gender + " " + storage[i].Age + "\n" + storage[i].lvl; StudentsListBox.Items.Add(studentListBoxItem); //clear text boxes for ease of use and bump i for next use FirstNameTextBox.Text = null; LastNameTextBox.Text = null; StudentIDTextBox.Text = null; AgeTextBox.Text = null; comboBox.SelectedIndex = 0; levelBox.SelectedIndex = 0; i++; }