📄 classui.cs
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace 课程安排
{
public partial class ClassUI : Form
{
private Grade curGrade = null;
private bool hasModify = false;
public ClassUI()
{
InitializeComponent();
LoadDate();
}
// 初始化数据
private void LoadDate()
{
if (School.Grades != null)
{
this.comboBox1.Items.Clear();
this.comboBox1.Text = "";
for (int i = 0; i < School.Grades.Count; i++)
{
this.comboBox1.Items.Add(Utility.GetShowGradeNameFromObject(School.Grades[i]));
}
if (this.comboBox1.Items.Count <= 0)
return;
this.comboBox1.SelectedIndex = 0;
if (Utility.CheckComboText(this.comboBox1))
{
Grade grade = Utility.GetGradeObjectFromShowName(this.comboBox1.Text);
LoadRelateDate(grade);
}
}
}
// 载入该年级相关的信息(教师和课程)
private void LoadRelateDate(Grade grade)
{
// 列举出该年级的班级
this.listBox1.Items.Clear();
if (grade.Classes != null)
{
for (int i = 0; i < grade.Classes.Count; i++)
{
listBox1.Items.Add(grade.Classes[i].ClassName);
}
}
List<string> tSource = new List<string>(); // 教授该年级的教师
List<string> tDistinct = new List<string>(); // 不教授该年级的教师
if (grade.Teachers != null)
{
for (int i = 0; i < School.Teachers.Count; i++)
{
Teacher t = School.Teachers[i];
if (!grade.HasTeacher(t))
tDistinct.Add(t.TeacherName);
}
for (int i = 0; i < grade.Teachers.Count; i++)
{
tSource.Add(grade.Teachers[i].TeacherName);
}
}
else
{
for (int i = 0; i < School.Teachers.Count; i++)
{
Teacher t = School.Teachers[i];
tDistinct.Add(t.TeacherName);
}
}
this.dragInfoControlTeacher.SourceStrings = tSource;
this.dragInfoControlTeacher.DestinationStrings = tDistinct;
List<string> sSource = new List<string>();
List<string> sDistinct = new List<string>();
if (grade.Subjects != null)
{
for (int i = 0; i < School.Subjects.Count; i++)
{
Subject s = School.Subjects[i];
if (!grade.HasSubject(s))
sDistinct.Add(s.SubjectName);
}
for (int i = 0; i < grade.Subjects.Count; i++)
{
sSource.Add(grade.Subjects[i].SubjectName);
}
}
else
{
for (int i = 0; i < School.Subjects.Count; i++)
{
Subject s = School.Subjects[i];
sDistinct.Add(s.SubjectName);
}
}
this.dragInfoControlSubject.SourceStrings = sSource;
this.dragInfoControlSubject.DestinationStrings = sDistinct;
}
// 保存该年级相关信息的修改
private void SaveRelateDate(Grade grade)
{
List<string> tSource = this.dragInfoControlTeacher.SourceStrings;
grade.RemoveAllTeacher();
for (int i = 0; i < tSource.Count; i++)
{
Teacher t = School.GetTeacher(tSource[i]);
grade.AddTeacher(t);
t.AddGrade(grade);
}
List<string> sSource = this.dragInfoControlSubject.SourceStrings;
grade.RemoveAllSubject();
for (int i = 0; i < sSource.Count; i++)
{
Subject s = School.GetSubject(sSource[i]);
grade.AddSubject(s);
}
List<string> tDestination = dragInfoControlTeacher.DestinationStrings;
for (int i = 0; i < tDestination.Count; i++)
{
Teacher t = School.GetTeacher(tDestination[i]);
t.RemoveGrade(grade);
grade.RemoveTeacher(t);
}
}
#region 销毁对象
private void mDispose()
{
if (this.dragInfoControlSubject.HasModify || this.dragInfoControlTeacher.HasModify)
{
this.hasModify = false;
this.dragInfoControlTeacher.HasModify = false;
this.dragInfoControlSubject.HasModify = false;
if (MessageBox.Show("是否保存修改?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
{
SaveRelateDate(this.curGrade);
}
}
}
protected override void OnClosing(CancelEventArgs e)
{
this.mDispose();
base.OnClosing(e);
}
protected override void OnFormClosing(FormClosingEventArgs e)
{
this.mDispose();
base.OnFormClosing(e);
}
#endregion
// 失去焦点时提示是否保存数据
protected override void OnLostFocus(EventArgs e)
{
mDispose();
base.OnLostFocus(e);
}
protected override void OnGotFocus(EventArgs e)
{
LoadDate();
base.OnGotFocus(e);
}
// 改变所选择的年级
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.dragInfoControlSubject.HasModify || this.dragInfoControlTeacher.HasModify)
{
this.hasModify = false;
this.dragInfoControlTeacher.HasModify = false;
this.dragInfoControlSubject.HasModify = false;
if (MessageBox.Show("是否保存修改?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
{
SaveRelateDate(this.curGrade);
}
}
if (Utility.CheckComboText(this.comboBox1))
{
this.curGrade = Utility.GetGradeObjectFromShowName(this.comboBox1.Text);
LoadRelateDate(this.curGrade);
}
}
// 添加班级
private void button1_Click(object sender, EventArgs e)
{
this.addClass();
}
// 删除选择班级
private void button2_Click(object sender, EventArgs e)
{
this.removeClass();
}
// 删除所有班级
private void button3_Click(object sender, EventArgs e)
{
this.clearClass();
}
// 添加班级
private void toolStripMenuItem1_Click(object sender, EventArgs e)
{
this.addClass();
}
// 删除选中班级
private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.removeClass();
}
// 删除所有班级
private void 删除该年级所有班级ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.clearClass();
}
// 修改班级名称
private void button4_Click(object sender, EventArgs e)
{
this.modifyClassName();
}
// 修改班级名称
private void 修改班级名称ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.modifyClassName();
}
// 将数据保存到数据库中
private void 保存到数据库ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.mDispose(); // 保存数据到内存数据结构中
if (School.WriteClassesToDB())
{
MessageBox.Show("班级信息已经保存到数据库中", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("信息保存失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
#region 函数部分
private void addClass()
{
frmClassName f = new 课程安排.frmClassName(this.curGrade);
f.ShowDialog();
if (f.DialogResult == DialogResult.Yes)
{
string name = f.ClassName;
if (Utility.IsNameString(name))
{
if (Utility.isRepeat(School.Classes, name))
{
if (MessageBox.Show("该名称已经被使用,是否要重新输入?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
{
addClass();
}
}
else
{
this.listBox1.Items.Add(name);
Class cls = School.CreateClass(name,f.ClassStartTime);
cls.Grade = this.curGrade;
//this.hasModify = true;
}
}
else
{
if (MessageBox.Show("该名称中包含了特殊字符(或为空值),是否要重新输入", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
{
addClass();
}
}
}
}
private void removeClass()
{
if (this.listBox1.SelectedItems != null)
{
if (this.listBox1.SelectedItems.Count > 0)
{
for (int i = 0; i < this.listBox1.SelectedItems.Count; i++)
{
string clsName = this.listBox1.SelectedItems[i].ToString();
School.RemoveClass(School.GetClass(clsName));
curGrade.RemoveClass(School.GetClass(clsName));
this.listBox1.Items.Remove(this.listBox1.SelectedItems[i]);
}
//this.hasModify = true;
}
}
}
private void clearClass()
{
if (this.listBox1.Items.Count > 0)
{
for (int i = 0; i < this.listBox1.Items.Count; i++)
{
string clsName = this.listBox1.Items[i].ToString();
School.RemoveClass(School.GetClass(clsName));
this.listBox1.Items.Remove(this.listBox1.Items[i]);
}
curGrade.RemoveAllClass();
this.listBox1.Items.Clear();
//this.hasModify = true;
}
}
private void modifyClassName()
{
if (this.listBox1.SelectedIndex >= 0)
{
string oldName = listBox1.SelectedItem.ToString();
frmClassName f = new 课程安排.frmClassName(oldName);
f.ShowDialog();
if (f.DialogResult == DialogResult.Yes)
{
string name = f.ClassName;
if (name != oldName)
{
if (Utility.IsNameString(name))
{
if (Utility.isRepeat(School.Classes, name))
{
if (MessageBox.Show("该名称已经被使用,是否要重新输入?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
{
modifyClassName();
}
}
else
{
this.listBox1.Items[listBox1.SelectedIndex] = name;
Class cls = School.GetClass(oldName);
cls.ClassName = name;
}
}
else
{
if (MessageBox.Show("该名称中包含了特殊字符(或为空值),是否要重新输入", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
{
modifyClassName();
}
}
}
}
}
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -