[C#]学生成绩管理系统


实体类:Student
using System;
using System.Collections.Generic;
using System.Text;
namespace 成绩管理系统
{
class Student : IComparable<Student>
{
private string stuName;//学生姓名
public string StuName
{
get { return stuName; }
// set { stuName = value; }
}
private int mathScore;//高数成绩
public int MathScore
{
get { return mathScore; }
set { mathScore = value; }
}
private int englishScore;//英语成绩
public int EnglishScore
{
get { return englishScore; }
set { englishScore = value; }
}
private int specScore;//专业成绩
public int SpecScore
{
get { return specScore; }
set { specScore = value; }
}
private int totalScore1;
public int TotalScore1
{
get { return totalScore1=mathScore+englishScore+specScore; }
set { totalScore1 = value; }
}
public Student(string name, int math, int english, int spec)
{
this.stuName = name;
this.mathScore = math;
this.englishScore = english;
this.specScore = spec;
}
//计算成绩的方法
public int TotalScore()
{
int sum = mathScore + englishScore + specScore;
return sum;
}
//实现泛型借口中的方法
public int CompareTo(Student other)
{
return this.TotalScore().CompareTo(other.TotalScore());
}
//重写String的方法
public override string ToString()
{
return base.ToString();
}
}
}
实体类:Form1
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace 成绩管理系统
{
public partial class frmLogin : Form
{
public frmLogin()
{
InitializeComponent();
}
#region 窗体加载
private void frmLogin_Load(object sender, EventArgs e)
{
this.skinEngine1.SkinFile = "SportsOrange.ssk";
}
#endregion
#region 登陆检测
private void btnOk_Click(object sender, EventArgs e)
{
bool flag = false;
StreamReader sr = new StreamReader(@"Administrator.txt", Encoding.UTF8);
string uname, upwd;
while (sr.Peek() != -1)
{
uname = sr.ReadLine();
upwd = sr.ReadLine();
if (uname.Equals(this.txtName.Text.Trim()) && upwd.Equals(this.txtPwd.Text.Trim()))
{
flag = true;
break;
}
sr.ReadLine();
}
sr.Close();
if (flag == false)
{
MessageBox.Show("用户名或密码错误!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
this.progressBar1.Maximum = 50;
this.progressBar1.Value = 0;
Timer timer = new Timer();
timer.Interval = 1000;
timer.Tick += new EventHandler(timer1_Tick);
timer.Start();
for (int i = 0; i < 50; i++)
{
this.progressBar1.Value++;
}
}
}
#endregion
#region 时间计算
private void timer1_Tick(object sender, EventArgs e)
{
if (this.progressBar1.Value == this.progressBar1.Maximum)
{
((Timer)sender).Stop();
this.Hide();
Form2 frm = new Form2();
frm.Show();
}
}
#endregion
#region 退出
private void btnQuit_Click(object sender, EventArgs e)
{
if (MessageBox.Show("你真的要退出么?", "退出提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
Application.Exit();
}
}
#endregion
}
}
实体类:Form2
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 Form2 : Form
{
public Form2()
{
InitializeComponent();
}
List<Student> listStu = new List<Student>();//泛型
private void Form2_Load(object sender, EventArgs e)
{
timer1.Enabled = true;
this.skinEngine1.SkinFile = "SportsOrange.ssk";
}
#region 验证数据是否合法
private bool IsValidate()
{
//验证姓名
string strname = this.txtSname.Text.Trim();
if (strname.Length == 0)
{
MessageBox.Show("姓名不能为空!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
this.txtSname.Focus();
return false;
}
else
{
foreach(Student s in listStu)
{
if (txtSname.Text==s.StuName)
{
MessageBox.Show("姓名已经存在!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
this.txtSname.Focus();
this.txtSname.SelectAll();
return false;
}
}
}
//验证高数成绩
string strmath = this.txtSmath.Text.Trim();
if (strmath.Length == 0)
{
MessageBox.Show("高数成绩不能为空!", "数据错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
this.txtSmath.Focus();
return false;
}
try
{
int math = int.Parse(strmath);
if (math < 0 || math > 100)
{
MessageBox.Show("高数成绩输入不再范围内!", "数据错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
this.txtSmath.Focus();
this.txtSmath.SelectAll();
return false;
}
}
catch (FormatException e)
{
MessageBox.Show("高数成绩只能是整数!", "数据错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
this.txtSmath.Focus();
this.txtSmath.SelectAll();
return false;
}
//验证英语成绩
string strEnglish = this.txtSenglish.Text.Trim();
if (strEnglish.Length == 0)
{
MessageBox.Show("英语成绩不能为空!", "数据错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
this.txtSenglish.Focus();
return false;
}
try
{
int english = int.Parse(strEnglish);
if (english < 0 || english > 100)
{
MessageBox.Show("英语成绩输入不再范围内!", "数据错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
this.txtSenglish.Focus();
this.txtSenglish.SelectAll();
return false;
}
}
catch (FormatException e)
{
MessageBox.Show("英语成绩只能是整数!", "数据错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
this.txtSenglish.Focus();
this.txtSenglish.SelectAll();
return false;
}
//验证专业成绩
string strOwn = this.txtSown.Text.Trim();
if (strOwn.Length == 0)
{
MessageBox.Show("专业成绩不能为空!", "数据错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
this.txtSown.Focus();
return false;
}
try
{
int own = int.Parse(strOwn);
if (own < 0 || own > 100)
{
MessageBox.Show("专业成绩输入不再范围内!", "数据错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
this.txtSown.Focus();
this.txtSown.SelectAll();
return false;
}
}
catch (FormatException e)
{
MessageBox.Show("专业成绩只能是整数!", "数据错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
this.txtSown.Focus();
this.txtSown.SelectAll();
return false;
}
this.dataGridView1.DataSource=null;
Student stu = new Student(strname, Int32.Parse(strmath), Int32.Parse(strEnglish), Int32.Parse(strOwn));
listStu.Add(stu);//把实例对象加入泛型集合
ShowAll();
return true;
}
#endregion
#region 学生总成绩的排序
private void ShowAll()
{
int count = 0;
foreach (Student st in listStu)
{
if (st.MathScore > 59 && st.EnglishScore > 59 && st.SpecScore > 59)
{
count++;
lblpass.Text = "及格的学生为" + count + "个";
}
}
listStu.Sort();
dataGridView1.AutoGenerateColumns = false;
}
#endregion
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void textBox4_TextChanged(object sender, EventArgs e)
{
}
#region 添加信息
private void button1_Click(object sender, EventArgs e)
{
dataGridView1.DataSource = null;
if (IsValidate() == true)
{
this.dataGridView1.DataSource = listStu;
}
dataGridView1.AutoGenerateColumns = false;
ShowAll();
txtSname.Text = "";
txtSmath.Text = "";
txtSenglish.Text = "";
txtSown.Text = "";
this.lblCount.Text = "本程序中存在 "+listStu.Count.ToString()+" 条记录";
}
#endregion
#region 帮助
private void banToolStripMenuItem_Click_1(object sender, EventArgs e)
{
MessageBox.Show("无法取得任何帮助,有情况请自行了断!", "帮助", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
#endregion
#region 退出
private void 退出QToolStripMenuItem_Click(object sender, EventArgs e)
{
if (MessageBox.Show("你真的要退出么?", "退出提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
Application.Exit();
}
}
#endregion
#region 时间计算
private void timer1_Tick(object sender, EventArgs e)
{
lblTime.Text = DateTime.Now.ToString("现在是:yyyy年MM月dd日HH时mm分ss秒");
}
#endregion
private void Form2_MouseHover(object sender, EventArgs e)
{
}
private void Form2_MouseMove(object sender, MouseEventArgs e)
{
label6.Text = "欢迎使用本程序! 您鼠标的地理位置是:"+e.Location.ToString();
}
#region 修改
private void button2_Click(object sender, EventArgs e)
{
dataGridView1.DataSource = null;
string strname = this.txtSname.Text;
int strmath = Int32.Parse(this.txtSmath.Text);
int sEnglish = Int32.Parse(this.txtSenglish.Text);
int strOwn = Int32.Parse(this.txtSown.Text);
bool flag = false;
for (int i = 0; i < listStu.Count; i++)
{
txtSname.Text = "";
txtSmath.Text = "0";
txtSenglish.Text = "0";
txtSown.Text = "0";
if (listStu[i].StuName == strname)
{
//this.txtSname.ReadOnly;//只读,姓名不能修改
try
{
listStu[i].MathScore = strmath;
listStu[i].EnglishScore = sEnglish;
listStu[i].SpecScore = strOwn;
}
catch (Exception et)
{
MessageBox.Show("请输入完整的各科分数或者先查询并在查询的基础上修改!");
}
flag = true;
}
}
if (!flag)
{
MessageBox.Show("输入信息不存在!");
txtSname.Text = "";
txtSmath.Text = "";
txtSenglish.Text = "";
txtSown.Text = "";
}
this.dataGridView1.DataSource = listStu;
dataGridView1.AutoGenerateColumns = false;
ShowAll();
}
#endregion
#region 删除
private void button3_Click(object sender, EventArgs e)
{
dataGridView1.DataSource = null;
bool flag = false;
string strname = this.txtSname.Text;
for (int i = 0; i < listStu.Count; i++)
{
if (listStu[i].StuName == strname)
{
listStu.Remove(listStu[i]);
flag = true;
MessageBox.Show("删除成功");
txtSname.Text = "";
txtSmath.Text = "";
txtSenglish.Text = "";
txtSown.Text = "";
}
}
if (!flag)
{
MessageBox.Show("你删除的学生信息不存在!");
txtSname.Text = "";
txtSmath.Text = "";
txtSenglish.Text = "";
txtSown.Text = "";
}
this.dataGridView1.DataSource = listStu;
dataGridView1.AutoGenerateColumns = false;
ShowAll();
}
#endregion
#region 查询
private void button4_Click(object sender, EventArgs e)
{
dataGridView1.DataSource = null;
string strname = this.txtSname.Text;
bool flag = false;
foreach (Student s in listStu)
{
if (s.StuName == strname)
{
txtSmath.Text = s.EnglishScore.ToString();
txtSenglish.Text = s.EnglishScore.ToString();
txtSown.Text = s.SpecScore.ToString();
flag = true;
}
}
if (!flag)
{
MessageBox.Show("未找到你要搜索的学生信息!");
txtSname.Text = "";
txtSmath.Text = "";
txtSenglish.Text = "";
txtSown.Text = "";
}
this.dataGridView1.DataSource = listStu;
dataGridView1.AutoGenerateColumns = false;
ShowAll();
}
#endregion
private void lblCount_Click(object sender, EventArgs e)
{}
private void tabPage1_Click(object sender, EventArgs e)
{ }
#region 字体控件
private void 字体ToolStripMenuItem_Click(object sender, EventArgs e)
{
FontDialog fd = new FontDialog();
fd.AllowScriptChange = true;
fd.ShowColor = true;
fd.AllowVectorFonts = true;
fd.ShowEffects = true;
if (fd.ShowDialog() == DialogResult.OK)
{
txtSname.Font = fd.Font;
txtSname.ForeColor = fd.Color;
txtSmath.Font = fd.Font;
txtSmath.ForeColor = fd.Color;
txtSenglish.Font = fd.Font;
txtSenglish.ForeColor = fd.Color;
txtSown.Font = fd.Font;
txtSown.ForeColor = fd.Color;
lblCount.Font = fd.Font;
lblCount.ForeColor = fd.Color;
lblpass.Font = fd.Font;
lblpass.ForeColor = fd.Color;
lblTime.Font = fd.Font;
lblTime.ForeColor = fd.Color;
label6.Font = fd.Font;
label6.ForeColor = fd.Color;
}
}
#endregion
private void pictureBox1_Click(object sender, EventArgs e)
{
}
}
}
这个小小的项目使用的元素:C# Winform FileStream文本流 DataGridView DataTime 进度条 .......................欢迎指导!
需要源码学习的请联系:zhukuanglong@vip.qq.com 或者 zhukuanglong@gmail.com

浙公网安备 33010602011771号