📄 selectcourse.cs
字号:
this.btnExit.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.btnExit.Image = ((System.Drawing.Image)(resources.GetObject("btnExit.Image")));
this.btnExit.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.btnExit.Location = new System.Drawing.Point(448, 448);
this.btnExit.Name = "btnExit";
this.btnExit.Size = new System.Drawing.Size(56, 24);
this.btnExit.TabIndex = 100;
this.btnExit.Text = "退出";
this.btnExit.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
//
// SelectCourse
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(536, 509);
this.Controls.Add(this.label5);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.btnDelete);
this.Controls.Add(this.btnAdd);
this.Controls.Add(this.label1);
this.Controls.Add(this.listBox2);
this.Controls.Add(this.listBox1);
this.Controls.Add(this.btnApply);
this.Controls.Add(this.btnExit);
this.ForeColor = System.Drawing.SystemColors.Desktop;
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.Name = "SelectCourse";
this.Text = "班级选课";
this.Load += new System.EventHandler(this.SelectCourse_Load);
this.groupBox1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private void Combo_Fill()
{
try
{
if(this.conn1.State==ConnectionState.Closed)this.conn1.Open();
this.commandStr="select DepartmentName from Department";
this.Command1.CommandText=this.commandStr;
this.DataReader1=this.Command1.ExecuteReader();
while(this.DataReader1.Read())
{
this.comboDepartment.Items.Add(this.DataReader1["DepartmentName"].ToString());
}
this.DataReader1.Close();
this.commandStr="select SemesterName from Semester";
this.Command1.CommandText=this.commandStr;
this.DataReader1=this.Command1.ExecuteReader();
while(this.DataReader1.Read())
{
this.comboSemester.Items.Add(this.DataReader1["SemesterName"].ToString());
}
this.DataReader1.Close();
this.commandStr="select CourseName from Course";
this.Command1.CommandText=this.commandStr;
this.DataReader1=this.Command1.ExecuteReader();
while(this.DataReader1.Read())
{
this.listBox1.Items.Add(this.DataReader1["CourseName"].ToString());
}
this.DataReader1.Close();
}
catch(Exception E)
{
MessageBox.Show(E.ToString());
}
finally
{
this.conn1.Close();
}
}
private void comboDepartment_SelectedIndexChanged(object sender, System.EventArgs e)
{
this.comboClass.Text="";
this.comboClass.Items.Clear();
this.listBox2.Items.Clear();
try
{
this.conn1.Open();
this.commandStr="select ClassName from Class where DepartmentName="+"'"+this.comboDepartment.Text+"'";
this.Command1.CommandText=this.commandStr;
this.DataReader1=this.Command1.ExecuteReader();
while(this.DataReader1.Read())
{
this.comboClass.Items.Add(this.DataReader1["ClassName"].ToString());
}
this.DataReader1.Close();
}
catch(Exception E)
{
MessageBox.Show(E.ToString());
}
finally
{
this.DataReader1.Close();
this.conn1.Close();
}
}
private void SelectCourse_Load(object sender, System.EventArgs e)
{
this.Combo_Fill();
this.btnAdd.Enabled=false;
this.btnDelete.Enabled=false;
this.btnApply.Enabled=false;
}
private void btnExit_Click(object sender, System.EventArgs e)
{
this.Close();
}
private void btnAdd_Click(object sender, System.EventArgs e)
{
if(this.listBox1.SelectedItem==null)
return;//如果没有选择待选课程则返回
for(int i=0;i<this.listBox2.Items.Count;i++)
{
if(this.listBox1.SelectedItem.Equals(this.listBox2.Items[i]))
{
MessageBox.Show("已经选择了本课程","错误");
return;//不准重复选择
}
}
DataRow newrow;
newrow=DataSetDep.Tables[0].NewRow();
//生长选课数据表的新行
newrow["Department"]=this.department;
newrow["Class"]=this.classname;
newrow["Semester"]=this.semester;
newrow["Course"]=this.listBox1.SelectedItem.ToString();
this.DataSetDep.Tables[0].Rows.Add(newrow);
this.listBox2.Items.Add(this.listBox1.SelectedItem);
this.comboClass.Text=this.classname;
this.comboDepartment.Text=this.department;
this.comboSemester.Text=this.semester;
}
private void btnDelete_Click(object sender, System.EventArgs e)
{
for(int i=0;i<this.DataSetDep.Tables[0].Rows.Count;i++)
{
if(this.DataSetDep.Tables[0].Rows[i]["Course"].ToString()==this.listBox2.SelectedItem.ToString())
{
this.DataSetDep.Tables[0].Rows[i].Delete();
//从数据集中删除记录
this.listBox2.Items.RemoveAt(this.listBox2.SelectedIndex);
//从ListBox2中删除记录
return;
}
}
}
private void btnOk_Click(object sender, System.EventArgs e)
{
if(this.comboDepartment.Text==""|this.comboSemester.Text==""|this.comboClass.Text=="")
{
MessageBox.Show("院系、班级和学期不能为空");
return;//必选选择院系,班级和学期
}
this.department=this.comboDepartment.Text;
this.classname=this.comboClass.Text;
this.semester=this.comboSemester.Text;
//用类属性记录院系、班级和学期
this.selectStr="select * from SelectCourse where Department="+"'"+department+"'";
this.selectStr+=" and Class="+"'"+this.classname+"'";
this.selectStr+=" and Semester="+"'"+this.semester+"'";
this.sqlDataAdapter1=new SqlDataAdapter(this.selectStr,this.conn1);
this.DataSetDep.Clear();
this.sqlDataAdapter1.Fill(this.DataSetDep,"SelectCourse");
//填充数据集
this.ListBox_Fill();
//显示此班级此学期已经选择的课程
this.btnAdd.Enabled=true;
this.btnDelete.Enabled=true;
this.btnApply.Enabled=true;
}
private void ListBox_Fill()
{
this.listBox2.Items.Clear();
for(int i=0;i<this.DataSetDep.Tables[0].Rows.Count;i++)
{
this.listBox2.Items.Add(this.DataSetDep.Tables[0].Rows[i]["Course"]);
}
MessageBox.Show("班级:"+this.comboClass.Text+" 学期:"+this.comboSemester.Text+
"\n目前选课数为:"+this.listBox2.Items.Count+" 请开始为此班级选课");
}
private void DataSet_Bingding()
{
this.comboSemester.DataBindings.Add("Text",this.DataSetDep,"SelectCourse.Semester");
this.comboClass.DataBindings.Add("Text",this.DataSetDep,"SelectCourse.Class");
this.comboDepartment.DataBindings.Add("Text",this.DataSetDep,"SelectCourse.Department");
}
private void btnApply_Click(object sender, System.EventArgs e)
{
MessageBox.Show("班级:"+this.comboClass.Text+" 学期:"+this.comboSemester.Text+
"\n完成选课数为:"+this.listBox2.Items.Count+" 信息已经提交");
try
{
if(this.conn1.State==ConnectionState.Closed)
this.conn1.Open();
SqlCommandBuilder commandbuilder1=new SqlCommandBuilder(this.sqlDataAdapter1);
this.sqlDataAdapter1.Update(this.DataSetDep,"SelectCourse");
this.DataSetDep.AcceptChanges();
}
catch(Exception E)
{
this.ErrorHandle(E);
}
finally
{
this.conn1.Close();
}
}
public void ErrorHandle(System.Exception E)
{
MessageBox.Show(E.ToString());
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -