zoukankan      html  css  js  c++  java
  • c#简易学生信息管理系统

    在近期的学习中,我们学习了泛型及泛型集合的概念和使用,泛型是c#中的一个重要概念,为了巩固我们学习的成果,我们可以使用一个实例来进行练习

    题目及要求

    要求使用Windows窗体应用程序,制作出如上图的界面,并实现增删改查的功能

    StuInfo类的编写

    同往常一样,在编写窗体的代码前,我们需要先编写一个StuInfo类用来存放学生的信息

     StuInfo.cs代码如下:

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace cs学生信息管理系统1121
     8 {
     9     class StuInfo
    10     {
    11         private string sno;     //学号
    12         private string name;    //姓名
    13         private string sclass;  //班级
    14         private string tele;    //电话
    15 
    16         //定义成员变量的索引器
    17         public string Sno
    18         {
    19             get { return sno; }
    20             set { sno = value; }
    21         }
    22         public string Name
    23         {
    24             get { return name; }
    25             set { name = value; }
    26         }
    27         public string SClass
    28         {
    29             get { return sclass; }
    30             set { sclass = value; }
    31         }
    32         public string Tele
    33         {
    34             get { return tele; }
    35             set { tele = value; }
    36         }
    37 
    38         //构造函数
    39         public StuInfo(string sno, string name, string sclass, string tele)
    40         {
    41             Sno = sno;
    42             Name = name;
    43             SClass = sclass;
    44             Tele = tele;
    45         }
    46     }
    47 }

    主窗体代码的编写

    写好了StuInfo类之后,我们终于可以开始窗体应用程序的编写了,首先我们需要设置一下页面布局

    页面布局及我的部分控件命名

    接下来我们来编写代码

     Form1.cs代码:

      1 using System;
      2 using System.Collections.Generic;
      3 using System.ComponentModel;
      4 using System.Data;
      5 using System.Drawing;
      6 using System.Linq;
      7 using System.Text;
      8 using System.Threading.Tasks;
      9 using System.Windows.Forms;
     10 
     11 namespace cs学生信息管理系统1121
     12 {
     13     public partial class Form1 : Form
     14     {
     15         //声明值为StuInfo类型数据的泛型字典StuDic
     16         Dictionary<string, StuInfo> StuDic = new Dictionary<string, StuInfo>();
     17 
     18         //显示数据方法
     19         private void FillGrid(Dictionary<string, StuInfo> dic)
     20         {
     21             //如果数据网格中没有任何元素,则初始化
     22             if(dataGridViewStuInfo.ColumnCount == 0)
     23             {
     24                 //初始化一个新列
     25                 DataGridViewTextBoxColumn col_sno = new DataGridViewTextBoxColumn();
     26                 col_sno.HeaderText = "学号";  //设置标题
     27                 col_sno.DataPropertyName = "Sno";   //设置数据绑定文本
     28                 col_sno.Name = "sno";   //设置该列的名字
     29 
     30                 DataGridViewTextBoxColumn col_name = new DataGridViewTextBoxColumn();
     31                 col_name.HeaderText = "姓名";
     32                 col_name.DataPropertyName = "Name";
     33                 col_name.Name = "name";
     34 
     35                 DataGridViewTextBoxColumn col_class = new DataGridViewTextBoxColumn();
     36                 col_class.HeaderText = "班级";
     37                 col_class.DataPropertyName = "SClass";
     38                 col_class.Name = "class";
     39 
     40                 DataGridViewTextBoxColumn col_tele = new DataGridViewTextBoxColumn();
     41                 col_tele.HeaderText = "电话";
     42                 col_tele.DataPropertyName = "Tele";
     43                 col_tele.Name = "tele";
     44 
     45                 //向数据网格控件中加入我们刚才定义的列
     46                 dataGridViewStuInfo.Columns.Add(col_sno);
     47                 dataGridViewStuInfo.Columns.Add(col_name);
     48                 dataGridViewStuInfo.Columns.Add(col_class);
     49                 dataGridViewStuInfo.Columns.Add(col_tele);
     50             }
     51             //声明数据源绑定对象
     52             BindingSource bs = new BindingSource();
     53             bs.DataSource = dic.Values; //将我们数据字典中的元素绑定到bs中
     54             dataGridViewStuInfo.DataSource = bs;    //将bs中的数据与数据网格控件绑定
     55         }
     56 
     57         public Form1()
     58         {
     59             InitializeComponent();
     60             this.Text = "学生信息管理系统";
     61             PanelEdit.Visible = false;  //将编辑面板隐藏
     62 
     63             //定义初始的数据
     64             StuInfo zhang = new StuInfo("001", "张三", "1601", "18096471357");
     65             StuInfo luo = new StuInfo("002", "罗辑", "1503", "13968743218");
     66             StuInfo sun = new StuInfo("003", "孙雪", "1704", "13579314567");
     67             StuInfo wang = new StuInfo("004", "王莱", "1605", "18034976521");
     68 
     69             //将我们定义的数据加入到数据字典中
     70             StuDic.Add(zhang.Sno, zhang);
     71             StuDic.Add(luo.Sno, luo);
     72             StuDic.Add(sun.Sno, sun);
     73             StuDic.Add(wang.Sno, wang);
     74 
     75             FillGrid(StuDic);   //显示数据
     76         }
     77 
     78         //信息查询方法
     79         private void ButtonQuery_Click(object sender, EventArgs e)
     80         {
     81             PanelEdit.Visible = false;  //查询数据时关闭编辑面板
     82             //如果输入框中没有输入数据,则默认显示所有数据
     83             if(textBoxQuery.Text == "")
     84             {
     85                 FillGrid(StuDic);
     86                 return;
     87             }
     88             //若找不到用户要查询的学生,则弹出错误提示
     89             if(!StuDic.ContainsKey(textBoxQuery.Text))
     90             {
     91                 MessageBox.Show("查无此人!", "错误",
     92                     MessageBoxButtons.OK, MessageBoxIcon.Error);
     93                 return;
     94             }
     95 
     96             StuInfo s = StuDic[textBoxQuery.Text];  //找出对应的学生信息
     97             //创建一个新的数据字典,用于存放查询的结果
     98             Dictionary<string, StuInfo> dic = new Dictionary<string, StuInfo>();
     99             dic.Add(s.Sno, s);
    100             FillGrid(dic);  //显示数据
    101         }
    102 
    103         //信息删除方法
    104         private void ButtonDel_Click(object sender, EventArgs e)
    105         {
    106             PanelEdit.Visible = false;  //删除数据时关闭编辑面板
    107             //如果找不到用户要删除的数据,报错
    108             if(!StuDic.ContainsKey(textBoxQuery.Text))
    109             {
    110                 MessageBox.Show("您要删除的元素不存在!", "错误",
    111                     MessageBoxButtons.OK, MessageBoxIcon.Error);
    112                 return;
    113             }
    114 
    115             StuDic.Remove(textBoxQuery.Text);   //删除数据
    116             FillGrid(StuDic);   //显示数据
    117         }
    118 
    119         //修改数据方法
    120         private void ButtonEdit_Click(object sender, EventArgs e)
    121         {
    122             if(!StuDic.ContainsKey(textBoxQuery.Text))
    123             {
    124                 MessageBox.Show("您要修改的数据不存在!", "错误",
    125                      MessageBoxButtons.OK, MessageBoxIcon.Error);
    126                 return;
    127             }
    128 
    129             PanelEdit.Visible = true;   //修改数据时开启编辑面板
    130             textBoxStuNo.Enabled = false;   //学号不允许修改
    131 
    132             //新建对象存储要修改的元素
    133             StuInfo s = StuDic[textBoxQuery.Text];
    134 
    135             //将数据分别放到各个输入框中
    136             textBoxName.Text = s.Name;
    137             textBoxClass.Text = s.SClass;
    138             textBoxStuNo.Text = s.Sno;
    139             textBoxTele.Text = s.Tele;
    140         }
    141 
    142         //添加数据方法
    143         private void ButtonAdd_Click(object sender, EventArgs e)
    144         {
    145             //将所有输入框中的数据清零
    146             textBoxStuNo.Text = "";
    147             textBoxName.Text = "";
    148             textBoxClass.Text = "";
    149             textBoxTele.Text = "";
    150 
    151             PanelEdit.Visible = true;   //添加数据时开启编辑面板
    152             textBoxStuNo.Enabled = true;    //启用学号输入框
    153         }
    154 
    155         //编辑面板区域的确定按钮事件
    156         private void ButtonOK_Click(object sender, EventArgs e)
    157         {
    158             //实行添加方法时
    159             if(textBoxStuNo.Enabled)
    160             {
    161                 //要添加的学号已存在时,发出警告
    162                 if(StuDic.ContainsKey(textBoxStuNo.Text))
    163                 {
    164                     MessageBox.Show("学号已存在!", "警告",
    165                          MessageBoxButtons.OK, MessageBoxIcon.Warning);
    166                     return;
    167                 }
    168                 //填写信息不全时,发出警告
    169                 if(textBoxStuNo.Text == "" || textBoxName.Text == ""
    170                     || textBoxClass.Text == "" || textBoxTele.Text == "")
    171                 {
    172                     MessageBox.Show("请将信息填写完整!", "警告",
    173                          MessageBoxButtons.OK, MessageBoxIcon.Warning);
    174                     return;
    175                 }
    176 
    177                 //新建对象s用于存放待添加的数据
    178                 StuInfo s = new StuInfo(textBoxStuNo.Text, textBoxName.Text,
    179                     textBoxClass.Text, textBoxTele.Text);
    180                 StuDic.Add(s.Sno, s);   //将数据添加进数据字典
    181             }
    182             //实行修改方法时
    183             else
    184             {
    185                 if(textBoxName.Text == "" || textBoxClass.Text == "" || textBoxTele.Text == "")
    186                 {
    187                     MessageBox.Show("请将信息填写完整!", "警告",
    188                          MessageBoxButtons.OK, MessageBoxIcon.Warning);
    189                     return;
    190                 }
    191 
    192                 //先将数据删除再添加来实现修改
    193                 StuDic.Remove(textBoxStuNo.Text);
    194 
    195                 //新建对象s用于存放待添加的数据
    196                 StuInfo s = new StuInfo(textBoxStuNo.Text, textBoxName.Text,
    197                     textBoxClass.Text, textBoxTele.Text);
    198                 StuDic.Add(s.Sno, s);   //将数据添加进数据字典
    199             }
    200 
    201             FillGrid(StuDic);   //显示数据
    202 
    203             //将所有输入框中的数据清零
    204             textBoxStuNo.Text = "";
    205             textBoxName.Text = "";
    206             textBoxClass.Text = "";
    207             textBoxTele.Text = "";
    208 
    209             PanelEdit.Visible = false;  //关闭编辑面板
    210         }
    211 
    212         //取消按键
    213         private void ButtonCel_Click(object sender, EventArgs e)
    214         {
    215             //将所有输入框中的数据清零
    216             textBoxStuNo.Text = "";
    217             textBoxName.Text = "";
    218             textBoxClass.Text = "";
    219             textBoxTele.Text = "";
    220 
    221             PanelEdit.Visible = false;  //关闭编辑面板
    222         }
    223     }
    224 }

    实际效果

    查询

    删除

    修改

    添加

     关于一些问题的解答

    为什么找不到PanelEdit

    在我的页面布局的下半段,有一块被虚线围起来的部分,那块部分属于一个Panel控件,需要将他的名字改成PanelEdit

    查询/删除等按键无效

    很可能是你的控件事件没有和你的方法绑定,以查询为例,在Form1.cs[设计]中,右键点击查询按钮,在属性->事件->Click中,将其值改为ButtonQuery_Click,其他所有的按键都要进行同样的操作后才能使用。

    我在Form1.cs中的ButtonQuery_Click等方法,都是通过双击设计界面的控件生成的,所以控件事件会自动和你的方法绑定,而如果你直接复制粘贴我的代码的话,就可能会产生没有绑定的情况。

  • 相关阅读:
    如何用SQL命令修改字段名称
    两个sql server 2000的通用分页存储过程
    Tomcat 6 连接 MS SQL 2005
    log4net 配置与应用
    如何去除Google搜索结果病毒提示
    Windows 2003远程桌面连接数限制
    ntext replace sql
    FCKeditor详细的设置
    SQL Server 自增字段归零等问题
    SQLServer2005数据库还原到SQLServer2000
  • 原文地址:https://www.cnblogs.com/sunriseblogs/p/9997911.html
Copyright © 2011-2022 走看看