void showStudentInfo(Student stu) { if (stu != null) { textBoxName.Text = stu.NAME; textBoxNum.Text = stu.NUM; textBoxClass.Text = stu.MyClass.ClassName; textBoxChinese.Text = Convert.ToString(stu.CHINESE); textBoxMath.Text = Convert.ToString(stu.MATH); textBoxEnglish.Text = Convert.ToString(stu.ENGLISH); textBoxAverage.Text = Convert.ToString((stu.ENGLISH + stu.MATH + stu.CHINESE) / 3); textBoxTotal.Text = Convert.ToString(stu.ENGLISH + stu.MATH + stu.CHINESE); } }
private void buttonSearch_Click(object sender, EventArgs e) { switch (buttonSearch.Text) { case "删除": { if (studentInUse != null) { studentInUse.MyClass.removeStudent(studentInUse.NUM); studentInUse = null; } break; } case "完成": { try { studentInUse.NAME = textBoxName.Text; studentInUse.NUM = textBoxNum.Text; studentInUse.CHINESE = Convert.ToSingle(textBoxChinese.Text); studentInUse.ENGLISH = Convert.ToSingle(textBoxEnglish.Text); studentInUse.MATH = Convert.ToSingle(textBoxEnglish.Text); refreshStudentList(classInUse); enableReadOnly(true); checkBoxChangeable.Visible = true; checkBoxChangeable.Checked = false; } catch { MessageBox.Show("更新失败,有些数据没有更新!"); } finally { } break; } case "添加": { Student stuTmp = studentInUse; try { studentInUse = new Student(textBoxNum.Text, textBoxName.Text, Convert.ToSingle(textBoxChinese.Text), Convert.ToSingle(textBoxMath.Text), Convert.ToSingle(textBoxEnglish.Text), classInUse); refreshStudentList(classInUse); showStudentInfo(studentInUse); enableReadOnly(true); checkBoxChangeable.Visible = true; checkBoxChangeable.Checked = false; } catch { MessageBox.Show("创建失败"); studentInUse = stuTmp; listBoxStuList.SelectedIndex = 0; } break; } default: break; } }
private void listBoxStuList_SelectedIndexChanged(object sender, EventArgs e) { if (listBoxStuList.SelectedItem.ToString() == "添加新学生") { checkBoxChangeable.Checked = true; buttonSearch.Text = "添加"; enableReadOnly(false); checkBoxChangeable.Visible = false; { textBoxName.Text = ""; textBoxNum.Text = ""; textBoxClass.Text = classInUse.ClassName; textBoxChinese.Text = "0"; textBoxMath.Text = "0"; textBoxEnglish.Text = "0"; textBoxAverage.Text = "0"; textBoxTotal.Text = "0"; } } else { enableReadOnly(true); checkBoxChangeable.Checked = false; checkBoxChangeable.Visible = true; buttonSearch.Text = "删除"; int index; studentInUse = classInUse.getStudent(listBoxStuList.SelectedIndex + 1); index = listBoxStuList.SelectedIndex; showStudentInfo(studentInUse); listBoxStuList.SelectedIndex = index; } }
/// <summary> /// 添加一个学生进入班级实例的最后 /// </summary> /// <param name="stu"></param> /// <returns></returns> public bool addStudent(Student stu) { int i; if(studentNum!=1) { for (i = 0; i < studentNum;i++ ) { if (studList[i].NUM == stu.NUM) return false; } } studList[studentNum] = stu; studentNum++; return true; }