C# Windows窗体应用程序设计综合实例(二)--------学生成绩管理系统
今天给大家更新一个综合实例,学生成绩管理系统。从需求分析、窗体应用程序的编写、运行结果来阐述整个过程
(文末和评论区有源代码文件,需要自取,建议先看内容再看源代码,代码描述部分可忽略,比较长,影响观看时间。)
需求分析
首先先要进行需求分析,需求分析就是想清楚你写的窗体应用具体要实现哪些功能。最好把它用表格的形式列出来。本次实例的需求如下表格。
窗体 | 功能描述 |
---|---|
主窗体 | 对整个系统的功能进行汇总和导航 |
添加 | 对输入系统内的学生成绩进行添加输入数据 |
保存至TXT | 将系统内的成绩导出系统 |
从TXT导入 | 从系统外导入成绩信息 |
查找 | 根据特定信息查找学生的成绩信息 |
排序 | 按照特定要求进行降序或者升序排序 |
删除 | 对特定学生的成绩信息进行删除 |
修改 | 找到学生信息再修改相应的值 |
接下来我们了解了需求后,就开始在大脑里面构思具体的一个情景。
整体构思
假定这个系统完成了,你在使用的时候首先出现的应该是导航的页面,也就是主窗体,上面有各个功能的按钮。然后接着是每个功能对应的窗体的情景。最后退出结束。这一步的目的是为了在写的时候有一个大致的布局和编写习惯的预定判定。就像是一个工程师在心里画好了图纸,到时候就不用考虑布局和习惯的问题,只需要关注代码之间的逻辑和语法问题
下面就是整个最煎熬的步骤了,也就是按照前面说讲的窗体应用程序设计的步骤来完成。
窗体应用程序设计的编写
选取学生信息的存储结构
本次由于是做成绩管理系统,就要想好具体在程序里面的存储结构。这里我们用典型的单链表作为存储结构来存储学生的各项信息。
那么可能有一部分不太了解数据结构的小伙伴就比较疑惑了,接下来简单介绍一下单链表。
单链表的结构如下:
每个节点由数据域和指针域构成,在开头的的节点有一个头指针。该存储结构的优点是在进行某些操作的时候便于修改。具体的内容自行查找知识点,这里不做过多解释。(文末和评论区有源代码,需要自取)
设置布局
接下来我们就开始设置窗体的布局,如下图所示:
1.主界面
2.修改
3.添加
4.删除
5.排序
6.查找
在这里说明一下,在写的过程中,最好是完成一个界面就运行检查一次,不要等到写完了再去查错,工作量比较大容易混乱。
控件布局及属性设置
在了解了布局后,我们接下来根据具体的要求进行属性的设置,属性设置如下:
注意:控件较多的情况下,name属性会自动命名为控件名+加入的顺序,例如加入两个label,第一个自动命名为label1,第二个就为label2。因此后面添加控件按照表格的顺序来一个一个搞好再添加,不然容易混乱与代码对不上。
在这里再强调一下,最好是完成一个界面就运行检查一次,不要等到写完了再去查错,工作量比较大容易混乱!
最好是完成一个界面就运行检查一次,不要等到写完了再去查错,工作量比较大容易混乱!
最好是完成一个界面就运行检查一次,不要等到写完了再去查错,工作量比较大容易混乱! 重要的事情说三遍。
2.排序
3.查找
4.删除
5.修改
6.添加
严格注意区分name属性里面的大小写,具体事项见备注。
添加代码
添加代码的方法就是双击你的控件,对应的添加。具体的可以看主页之前写的匹配游戏设计和数学测验器。
***************************************
前方高能,非战斗人员请撤离
请爱护好自己的眼睛
***************************************
1.Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace C_sharp学生成绩管理系统
{
public class Node
{
//数据域,当前结点数据
//信息
public int id;
public string name;
public string classnum;
internal object tiyu;
internal object yingyu;
internal object shuxue;
//分数
public int c_sharp {
set; get; }
public int ds {
set; get; }
public int maogai {
set; get; }
public int ps {
set; get; }
public int sports {
set; get; }
public double total {
set; get; }
public double average {
set; get; }
//指针
public Node Next {
set; get; } //位置域,下一个结点地址
//构造函数
public Node(int id, string name, string classnum, int c_sharp, int ds, int maogai, int ps,int sports)
{
this.id = id;
this.name = name;
this.classnum = classnum;
this.c_sharp = c_sharp;
this.ds = ds;
this.maogai = maogai;
this.ps = ps;
this.sports = sports;
this.total = this.c_sharp + this.ds + this.maogai + this.ps + this.sports;
this.average = this.total / 6;
this.Next = null;
}
}
public class LinkList
{
public Node Head {
set; get; } //单链表头
public int total {
set; get; } //总人数
//构造
public LinkList()
{
Head = null;
total = 0;
}
//增加新元素到单链表末尾
public static void Append(int id, string name, string classnum, int c_sharp, int ds, int maogai, int ps, int sports)
{
Node foot = new Node(id, name, classnum, c_sharp, ds,maogai, ps, sports);
Node A = new Node(id, name, classnum, c_sharp, ds, maogai, ps, sports);
if (PublicValue.Head == null)
{
PublicValue.Head = foot;
return;
}
A = PublicValue.Head;
while (A.Next != null)
{
A = A.Next;
}
A.Next = foot;
}
}
//全局变量
public class PublicValue
{
public static Node Head; //单链表头
public static Node Tail; //单链表尾
public static int total; //学生总数
}
/// 应用程序的主入口点
static class Program
{
/// <summary>
/// 应用程序的主入口点
/// </summary>
[STAThread]
static void Main()
{
LinkList link = new LinkList();
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
}
}
2.主界面:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;//Stream流类
namespace C_sharp学生成绩管理系统
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void SaveTXT_Click_1(object sender, EventArgs e)
{
//清空txt
System.IO.File.WriteAllText(@"data.txt", string.Empty);
//写入
StreamWriter sw = new StreamWriter("data.txt", true, Encoding.Default);
Node B = new Node(0, "0", "0", 0, 0, 0, 0, 0);
B = PublicValue.Head;
sw.Write(PublicValue.total + "\r\n");
while (B != null)
{
sw.Write(B.id + "\r\n" + B.classnum + "\r\n" + B.name + "\r\n" + B.c_sharp + "\r\n" + B.ds + "\r\n" + B.maogai + "\r\n" + B.ps + "\r\n" + +B.sports + "\r\n");
B = B.Next;
}
//清空缓冲区
sw.Flush();
//关闭流
sw.Close();
MessageBox.Show("保存成功");
}
private void AppendTxt_Click(object sender, EventArgs e)
{
int i;
FileStream fs = new FileStream("data.txt", FileMode.Open);
StreamReader sr = new StreamReader(fs, Encoding.Default);
//临时接收
int id;
string name;
string classnum;
int c_sharp;
int ds;
int maogai;
int ps;
int sports;
PublicValue.total = int.Parse(sr.ReadLine());
for (i = 0; i < PublicValue.total; i++)
{
id = int.Parse(sr.ReadLine());
name = sr.ReadLine();
classnum = sr.ReadLine();
c_sharp = int.Parse(sr.ReadLine());
ds = int.Parse(sr.ReadLine());
maogai = int.Parse(sr.ReadLine());
ps = int.Parse(sr.ReadLine());
sports = int.Parse(sr.ReadLine());
LinkList.Append(id, classnum, name, c_sharp, ds, maogai, ps, sports);
}
sr.Close();
MessageBox.Show("导入成功");
List_Click_1(null, null);
}
private void List_Click_1(object sender, EventArgs e)
{
Node B = new Node(0, "0", "0", 0, 0, 0, 0, 0);//毫无意义的赋值
B = PublicValue.Head;
Change.Text = "";//先清空
Change.Text += " 学号 \t\t 班级 \t\t姓名\t语文\t体育\t数学\t英语\t物理\t总分\t平均分\r\n";
while (B != null)
{
Change.Text += B.id + " \t" + B.classnum + "\t" + B.name + "\t" + B.c_sharp + "\t" + B.ds + "\t" + "\t" + B.maogai + "\t" + B.ps + "\t" + + B.sports + "\t" + B.total + "\t" + B.average.ToString("f2") + "\r\n";
B = B.Next;
}
}
private void ch_Click(object sender, EventArgs e)
{
ChangeStuForm f3 = new ChangeStuForm();
f3.ShowDialog();
List_Click_1(null, null);
}
private void Delete_Click_1(object sender, EventArgs e)
{
DelStuForm f4 = new DelStuForm();
f4.ShowDialog();
List_Click_1(null, null);
}
private void Find_Click_1(object sender, EventArgs e)
{
FindForm f6 = new FindForm();
f6.ShowDialog();
}
private void Sort_Click_1(object sender, EventArgs e)
{
SortForm f5 = new SortForm();
f5.ShowDialog();
List_Click_1(null, null);
}
private void Exit_Click_1(object sender, EventArgs e)
{
System.Environment.Exit(0);
}
private void Add_Click(object sender, EventArgs e)
{
AddStuForm f2 = new AddStuForm();
f2.ShowDialog();
List_Click_1(null, null);
}
private void MainForm_Load(object sender, EventArgs e)
{
}
}
}
3.修改
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace C_sharp学生成绩管理系统
{
public partial class ChangeStuForm : Form
{
private Button button1;
private Button button2;
private TextBox textBox1;
private TextBox textBox2;
private TextBox textBox3;
private TextBox textBox4;
private TextBox textBox5;
private TextBox textBox6;
private TextBox textBox7;
private TextBox textBox8;
private Label label1;
private Label label2;
private Label label3;
private Label label4;
private Label label5;
private Label label6;
private Label label7;
private Label label8;
private TextBox textBox9;
private Label label9;
private Button button3;
public ChangeStuForm()
{
InitializeComponent();
}
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.textBox3 = new System.Windows.Forms.TextBox();
this.textBox4 = new System.Windows.Forms.TextBox();
this.textBox5 = new System.Windows.Forms.TextBox();
this.textBox6 = new System.Windows.Forms.TextBox();
this.textBox7 = new System.Windows.Forms.TextBox();
this.textBox8 = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.label5 = new System.Windows.Forms.Label();
this.label6 = new System.Windows.Forms.Label();
this.label7 = new System.Windows.Forms.Label();
this.label8 = new System.Windows.Forms.Label();
this.textBox9 = new System.Windows.Forms.TextBox();
this.label9 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// button1
//
this.button1.BackColor = System.Drawing.SystemColors.Info;
this.button1.Location = new System.Drawing.Point(75, 372);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(128, 41);
this.button1.TabIndex = 0;
this.button1.Text = "确定";
this.button1.UseVisualStyleBackColor = false;
this.button1.Click += new System.EventHandler(this.button1_Click_1);
//
// button2
//
this.button2.BackColor = System.Drawing.SystemColors.Info;
this.button2.Location = new System.Drawing.Point(264, 371);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(140, 41);
this.button2.TabIndex = 1;
this.button2.Text = "修改";
this.button2.UseVisualStyleBackColor = false;
this.button2.Click += new System.EventHandler(this.button2_Click_1);
//
// button3
//
this.button3.BackColor = System.Drawing.SystemColors.Info;
this.button3.Location = new System.Drawing.Point(479, 372);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(136, 40);
this.button3.TabIndex = 2;
this.button3.Text = "退出";
this.button3.UseVisualStyleBackColor = false;
this.button3.Click += new System.EventHandler(this.button3_Click_1);
//
// textBox1
//
this.textBox1.BackColor = System.Drawing.SystemColors.ButtonHighlight;
this.textBox1.Location = new System.Drawing.Point(94, 46);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(155, 25);
this.textBox1.TabIndex = 3;
//
// textBox2
//
this.textBox2.BackColor = System.Drawing.SystemColors.ButtonHighlight;
this.textBox2.Location = new System.Drawing.Point(94, 96);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(155, 25);
this.textBox2.TabIndex = 4;
//
// textBox3
//
this.textBox3.BackColor = System.Drawing.SystemColors.ButtonHighlight;
this.textBox3.Location = new System.Drawing.Point(436, 46);
this.textBox3.Name = "textBox3";
this.textBox3.Size = new System.Drawing.Size(130, 25);
this.textBox3.TabIndex = 5;
//
// textBox4
//
this.textBox4.BackColor = System.Drawing.SystemColors.ButtonHighlight;
this.textBox4.Location = new System.Drawing.Point(94, 209);
this.textBox4.Name = "textBox4";
this.textBox4.Size = new System.Drawing.Size(100, 25);
this.textBox4.TabIndex = 6;
//
// textBox5
//
this.textBox5.BackColor = System.Drawing.SystemColors.ButtonHighlight;
this.textBox5.Location = new System.Drawing.Point(304, 209);
this.textBox5.Name = "textBox5";
this.textBox5.Size = new System.Drawing.Size(100, 25);
this.textBox5.TabIndex = 7;
//
// textBox6
//
this.textBox6.BackColor = System.Drawing.SystemColors.ButtonHighlight;
this.textBox6.Location = new System.Drawing.Point(500, 276);
this.textBox6.Name = "textBox6";
this.textBox6.Size = new System.Drawing.Size(100, 25);
this.textBox6.TabIndex = 8;
//
// textBox7
//
this.textBox7.BackColor = System.Drawing.SystemColors.ButtonHig