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等方法,都是通過雙擊設計界面的控件生成的,所以控件事件會自動和你的方法綁定,而如果你直接復制粘貼我的代碼的話,就可能會產生沒有綁定的情況。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM