丰富“WinForms” 的一个别样项目(学生管理)

     一个别样的WinForms项目,他并没多么的新颖,但是它的用处确实有点多,或许会有你需要的地方;如果你对WinForms中那么多控件无法把握,又或者是你根本就不懂,那我觉得你应该好好看看,如果一个人的人生命运无法去选择时,那我觉得接受它将是冲刺命运终点的最好动力,一切的不公平都是你为了躲避困难而找的借口,年轻人就需要不平坦的人生道路,通过刻苦奋斗得来的果实永远是最甜、平坦的人生亦是最幸福的瞬间。

     那首先就是项目的框架分别是:登录(FrmLogin )——>主界面(FrmMain)——>修改密码(FrmUpdate)——>添加/修改/删除学生基本信息等。

    框架搭好之后,我们了解一些框架内的代码吧!至于具体的一些控件呢!只能是自己慢慢去看看,有些注意点大家可以记下,在控件生成后要及时去修改控件的“Name”的属性,如果够专业就尽量取专业点名字,那样对你可能会有帮助的。

    当界面形成后,事件会是你填写代码的最大障碍,所以WinForms的几个事件需要认真记住:(单击)Click,当界面显示在眼前就形成的(Load),窗体正在运行的(FormClosing),(鼠标在界面移动)mouseClick

    1.登录界面(FrmLogin)

点击登陆进去的代码:

   string str = "Data source=.;Initial catalog=Myschool;Uid=sa";SqlConnection con = new SqlConnection(str);string sql = "select count(1) from student where studentname='"+txtUserName.Text+"' and Loginpwd='"+txtPwd.Text+"'";SqlCommand cmd = new SqlCommand(sql,con);try{con.Open();int count=Convert.ToInt32(cmd.ExecuteScalar());if (count>0){//隐藏原界面this.Hide();//跳转至主界面FormMain frm = new FormMain();Tool.pwd = txtPwd.Text;frm.Show();}}catch (Exception){MessageBox.Show("出现异常");}finally {con.Close();}

2.跳转主界面(FrmMain)

菜单控件,工具控件、右键(contextMenuStrip)控件,ListView控件、视图等

其中有ListView五大视图

  1.大图标(LargeIcon)

  2.小图标(SmallIcon)

  3.列表(List)

  4.详细信息(Detail)

  5.平铺(Tile)

 生成上方主界面ListView代码:

 private void btnBig_Click(object sender, EventArgs e){//设置listview的属性view为大图标lvlist.View = View.LargeIcon;}private void btnSmall_Click(object sender, EventArgs e){//设置listview的属性view为小图标lvlist.View = View.SmallIcon;}private void btnTile_Click(object sender, EventArgs e){//设置listview的属性view为平铺lvlist.View = View.Tile;}private void btnList_Click(object sender, EventArgs e){//设置listview的属性view为列表lvlist.View = View.List;}private void btnDetails_Click(object sender, EventArgs e){//设置listview的属性view为详细信息lvlist.View = View.Details;}

跳转至其它的各个功能的窗体:

        private void TsmiUpdate_Click(object sender, EventArgs e){//修改密码FrmUpdate frm = new FrmUpdate();frm.Show();}private void lvlist_SelectedIndexChanged(object sender, EventArgs e){}private void StuName_Click(object sender, EventArgs e){//查找this.Hide();FrmColor frm = new FrmColor();frm.Show();}

3.修改密码(FrmUpdate)

判断密码是否为空,修改密码是否成功

 private void btnupdate_Click(object sender, EventArgs e){string name = string.Empty;if (txtoldpwd.Text==name){DialogResult result=MessageBox.Show("原密码为空","通知",MessageBoxButtons.YesNo,MessageBoxIcon.Information);if (result==DialogResult.No){this.Close();}}if (txtnewpwd.Text == name){DialogResult result = MessageBox.Show("新密码密码为空", "通知", MessageBoxButtons.YesNo, MessageBoxIcon.Information);if (result == DialogResult.No){this.Close();}}if(txtOK.Text == name){DialogResult result = MessageBox.Show("确认密码为空", "通知", MessageBoxButtons.YesNo, MessageBoxIcon.Information);if (result == DialogResult.No){this.Close();}}if (txtoldpwd.Text != name && txtnewpwd.Text != name && txtOK.Text != name){string str = "Data source=.;Initial catalog=Myschool;Uid=sa";SqlConnection conn = new SqlConnection(str);string sql = "update student set Loginpwd='" + txtnewpwd.Text + "' where Loginpwd='" + txtoldpwd.Text + "'";SqlCommand cmd = new SqlCommand(sql, conn);try{conn.Open();if (txtOK.Text.Equals(txtnewpwd.Text)){int count = cmd.ExecuteNonQuery();if (count > 0){MessageBox.Show("修改成功");}}                                    }catch (Exception){MessageBox.Show("异常!"); ;}finally{conn.Close();}}}private void FrmUpdate_Load(object sender, EventArgs e){txtoldpwd.Text = Tool.pwd;}private void btncancel_Click(object sender, EventArgs e){this.Close();}

4.查找学生信息(FrmReseach)

从查找可以跳转到添加/修改/删除窗体

  private void btnselect_Click(object sender, EventArgs e){UpdateSelect();}//按学生编号查找的方法public void LoadDataFromDBToListView(string sql){string str = "data source=.;initial catalog=MySchool;uid=sa";SqlConnection con = new SqlConnection(str);SqlCommand cmd = new SqlCommand(sql, con);try{con.Open();SqlDataReader dr = cmd.ExecuteReader();if (dr != null){if (dr.HasRows){while (dr.Read()){//每从DB中读取出一条数据,需要形成一个ListViewItem对象,代表的是ListView中的一行int stuNo = Convert.ToInt32(dr["studentno"]);//第二列 学生名字string stuName = Convert.ToString(dr["studentname"]);//第三列  性别string stugender = Convert.ToString(dr["gender"]);//第四列  年级名称string gname = Convert.ToString(dr["gradename"]);//第五列  电话string phone = Convert.ToString(dr["phone"]);//第六列   地址string address = Convert.ToString(dr["address"]);//第七列   邮箱string email = Convert.ToString(dr["email"]);//第八列   出生日期string birthday = Convert.ToString(dr["birthday"]);ListViewItem lvItem = new ListViewItem(stuNo.ToString());lvItem.SubItems.Add(stuName);lvItem.SubItems.Add(stugender);lvItem.SubItems.Add(gname);lvItem.SubItems.Add(phone);lvItem.SubItems.Add(address);lvItem.SubItems.Add(email);lvItem.SubItems.Add(birthday);//让lvItem和ListView关联
                           lvlist.Items.Add(lvItem);lvlist.FullRowSelect = true;lvlist.ContextMenuStrip = ctmslist;}dr.Close();}}}catch (Exception){MessageBox.Show("异常!"); ;}finally{con.Close();}}private void listView1_SelectedIndexChanged(object sender, EventArgs e){string sql = @"select Studentno,Studentname,Gender,gradename,phone,address,email,birthday from student,gradewhere student.gradeid=grade.gradeid";LoadDataFromDBToListView(sql);}public void UpdateSelect(){lvlist.Items.Clear();string sql = @"select Studentno,Studentname,Gender,gradename,phone,address,email,birthday from student,gradewhere student.gradeid=grade.gradeid and studentname like '%" + txtname.Text + "%' ";LoadDataFromDBToListView(sql);}private void TsStudentInfo_Click(object sender, EventArgs e){int stuno=Convert.ToInt32(lvlist.SelectedItems[0].Text);FrmEditStu frm = new FrmEditStu();frm.stuno = stuno;frm.frmselect = this;frm.Show();}private void FrmColor_FormClosing(object sender, FormClosingEventArgs e){Application.Exit();}private void 删除学生信息ToolStripMenuItem_Click(object sender, EventArgs e){int sno = Convert.ToInt32(lvlist.SelectedItems[0].Text);FrmEditStu frm = new FrmEditStu();frm.stuno = sno;frm.frmselect = this;frm.Show();}

5.添加学生信息(FrmEditStu)

修改学生信息:

 

删除学生信息:

 

 

添加到数据库:

  private void btnSave_Click(object sender, EventArgs e){//接收到的学号//判断学号是否为空来选择sql语句//判断是否要删除信息if (sno!=0){DialogResult result=MessageBox.Show("是否删除?","提示",MessageBoxButtons.YesNo,MessageBoxIcon.Error);if (result==DialogResult.Yes){//删除this.Hide();ChangDelete();}}if (stuno!=0){//修改this.Hide();GetUpdate();}            else{//新增this.Hide();GetSave();}                  }          //判断stuno是否有值来修改select语句//新增public void GetSave(){string pwd = txtpwd.Text;//密码string pwdTrue = txtPwdTrue.Text;//确认密码string name = txtStuName.Text;//学生名string stuGender = string.Empty;//取单选列表if (rbtnGenderTure.Checked){stuGender = "1";}else{stuGender = "0";}//给下拉框取值int gid = GetIdByName();string phone = txtPhone.Text;//电话string address = txtAddress.Text;//地址DateTime dt = dtBirthday.Value;//日期string stuEmail = txtEmail.Text;//邮箱//StudentNo, LoginPwd, StudentName, Gender, GradeId, Phone, Address, Birthday, Emailstring sql = "insert into student values('" + pwd + "','" + name + "','" + stuGender + "'," + gid + ",'" + phone + "','" + address + "','" + dt + "','" + stuEmail + "')";string str = "Data source=.;initial catalog=MySchool;uid=sa";SqlConnection con = new SqlConnection(str);SqlCommand cmd = new SqlCommand(sql, con);con.Open();if (pwd.Equals(pwdTrue)){int count = cmd.ExecuteNonQuery();if (count > 0){frmselect.UpdateSelect();MessageBox.Show("成功!");}con.Close();}else{MessageBox.Show("前后密码不匹配");}       }//修改public void GetUpdate(){               string name = txtStuName.Text;//学生名string stuGender = string.Empty;//取单选列表if (rbtnGenderTure.Checked){stuGender = "1";}else{stuGender = "0";}//给下拉框取值int gid = GetIdByName();string phone = txtPhone.Text;//电话string address = txtAddress.Text;//地址DateTime dt = dtBirthday.Value;//日期string stuEmail = txtEmail.Text;//邮箱//StudentNo, LoginPwd, StudentName, Gender, GradeId, Phone, Address, Birthday, Emailstring sql = @"update student set studentname='" + name +"',gender='" + stuGender + "',gradeid='" + gid + "',phone='" +phone + "',address='"+address+"',birthday='"+dt+"',email='"+stuEmail+"' where studentno="+txtStuNo.Text+"";        string str = "Data source=.;initial catalog=MySchool;uid=sa";SqlConnection con = new SqlConnection(str);SqlCommand cmd = new SqlCommand(sql, con);con.Open();                             int count = cmd.ExecuteNonQuery();con.Close();if (count > 0){frmselect.UpdateSelect();MessageBox.Show("成功!");}                                             }//删除public void ChangDelete(){string pwd = txtpwd.Text;//密码string pwdTrue = txtPwdTrue.Text;//确认密码string name = txtStuName.Text;//学生名string stuGender = string.Empty;//取单选列表if (rbtnGenderTure.Checked){stuGender = "1";}else{stuGender = "0";}//给下拉框取值int gid = GetIdByName();string phone = txtPhone.Text;//电话string address = txtAddress.Text;//地址DateTime dt = dtBirthday.Value;//日期string stuEmail = txtEmail.Text;//邮箱//StudentNo, LoginPwd, StudentName, Gender, GradeId, Phone, Address, Birthday, Emailstring sql = @"delete from student where studentno='"+txtStuNo.Text+"' ";string str = "Data source=.;initial catalog=MySchool;uid=sa";SqlConnection con = new SqlConnection(str);SqlCommand cmd = new SqlCommand(sql, con);con.Open();if (pwd.Equals(pwdTrue)){int count = cmd.ExecuteNonQuery();if (count > 0){frmselect.UpdateSelect();MessageBox.Show("成功!");}}else{MessageBox.Show("前后密码不匹配");}con.Close();}//根据对应条件查询班级idpublic int GetIdByName(){string str = "Data source=.;initial catalog=MySchool;uid=sa";SqlConnection con = new SqlConnection(str);string sql = "select gradeid from grade where gradename='"+cmbGrade.Text+"'";SqlCommand cmd = new SqlCommand(sql, con);int gid = 0;try{con.Open();gid =Convert.ToInt32(cmd.ExecuteScalar());}catch (Exception){MessageBox.Show("网络异常");}finally{con.Close();}return gid;}//根据对应条件查询班级gnamepublic string GetNameById(int gid){string str = "Data source=.;initial catalog=MySchool;uid=sa";SqlConnection con = new SqlConnection(str);string sql = "select gradename from grade where gradeid=" + gid + "";SqlCommand cmd = new SqlCommand(sql, con);string name = "";try{con.Open();name =cmd.ExecuteScalar().ToString();}catch (Exception){MessageBox.Show("网络异常");}finally{con.Close();}return name;}//加载年级下拉框public void LoadGrade(){//拿到所有年级名称string str = "Data source=.;Initial catalog=MySchool;uid=sa";SqlConnection con = new SqlConnection(str);string sql = "select gradename from grade";SqlCommand cmd = new SqlCommand(sql, con);try{con.Open();SqlDataReader dr = cmd.ExecuteReader();if (dr != null){if (dr.HasRows){while (dr.Read()){string gname = dr["gradename"].ToString();//每读到一个年级名称,我就将"S1",添加到下拉框
                                 cmbGrade.Items.Add(gname);}}}}catch (Exception ex){MessageBox.Show("网络异常");}finally{con.Close();}}//根据学号加载其他信息public void LoadStuInfoByStuNo(){string str = "Data source=.;Initial catalog=MySchool;uid=sa";SqlConnection con = new SqlConnection(str);string sql = "select * from student where studentno="+stuno+"";SqlCommand cmd = new SqlCommand(sql, con);try{con.Open();SqlDataReader dr = cmd.ExecuteReader();txtStuNo.Text = stuno.ToString();if (dr!=null){if (dr.HasRows){while (dr.Read()){string stuname = dr["studentname"].ToString();txtStuName.Text = stuname;//姓名string stuGender = dr["gender"].ToString();if (stuGender == "1"){rbtnGenderTure.Checked = true;}else if (stuGender == "0"){rbtnGender.Checked = true;}//性别int gid = Convert.ToInt32(dr["gradeid"]);string gname = GetNameById(gid);cmbGrade.Text = gname;//年级string stuphone = dr["phone"].ToString();txtPhone.Text = stuphone;//电话string stuaddress = dr["address"].ToString();txtAddress.Text = stuaddress;//地址string stuemail = dr["email"].ToString();txtEmail.Text = stuemail;//邮箱DateTime stubirthday = Convert.ToDateTime(dr["birthday"]);dtBirthday.Value = stubirthday;//出生日期    
                             }}}}catch (Exception){MessageBox.Show("异常!!"); ;}finally {}}private void FrmEditStu_Load(object sender, EventArgs e){   //判断学号是否为空来选择sql语句if ( sno != 0){DialogResult result = MessageBox.Show("是否删除?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Error);if (result == DialogResult.Yes){this.Text = "删除学生信息";btnSave.Text = "删除";LoadStuInfoByStuNo();}}if(stuno!=0){this.Text = "修改学生信息";btnSave.Text = "修改";LoadStuInfoByStuNo();}           else{btnSave.Text = "保存";LoadGrade();}}private void FrmEditStu_FormClosing(object sender, FormClosingEventArgs e){Application.Exit();}private void btnClear_Click(object sender, EventArgs e){this.Close();}

6.动态图片时钟

计时控件:

通过时间来让几张图连接形成动态的计时:

  private int index = 0;private void timers_Tick(object sender, EventArgs e){if (index<imglist.Images.Count-1){index++;}else{index = 0;}piclist.Image = imglist.Images[index];}

      WinForms很重要,虽说难度不是很大,但它的内容太多,之间的融合又太复杂,所以难易与否只有自己知道,但只要认真听课、刻苦学习,我想时间会给我们大家最好的结果,所以我希望大家能指出我的一些不足之处,相信我们的互帮互助能让我们都变得更好。

转载于:https://www.cnblogs.com/bdpsc/p/5001955.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/404645.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

OSPF区域不能与area 0 相连的解决方法

有些时候&#xff0c;由于区域包含的路由器过多或区域的地理位置原因等&#xff0c;造成网络中配置的OSPF区域&#xff08;非area 0&#xff09;不能够与area 0相连。大家都知道&#xff0c;在OSPF的所有区域内&#xff0c;area 0 是骨干区域&#xff0c;非0区域都要与area0相连…

emqx使用webhook数据持久化到mysql

官方文档&#xff1a;WebHook | EMQ Docs 一、启用webhook和触发规则 编辑webhook规则配置文件&#xff1a;/etc/emqx/plugins/emqx_web_hook.conf 指定webhook的url位置&#xff1a;web.hook.url http://127.0.0.1:5000/mqtt/webhook 增加消息推送事件规则&#xff1a;指…

为什么你应该使用OpenGL而不是DirectX?

From: http://www.cnblogs.com/Baesky/archive/2011/04/08/2009128.html 这是一篇很意思的博文&#xff0c;原文链接为&#xff1a;http://blog.wolfire.com/2010/01/Why-you-should-use-OpenGL-and-not-DirectX 大家可以思考一下&#xff1a;why we choose a closed source AP…

flask web开发的相关博文学习

一、基础教程 flask-tutorial/SUMMARY.md at master greyli/flask-tutorial GitHubThe Flask Mega-Tutorial Part I: Hello, World! - miguelgrinberg.com全面的Flask教程 - 简书Flask入门教程 - HelloFlask 主推miguelgrinberg&#xff0c;课程如下 二、高级应用 flask-sq…

linux c 获取时间戳 打印时间戳

以下是项目开发中常用到的时间戳接口&#xff0c;可以直接用。 一、相关接口 二、代码实现 char* lgw_get_now_time(void) {time_t timep;time(&timep);return asctime(localtime(&timep)); } int lgw_get_now_tick(void) {time_t timep;time(&timep);return ti…

阿里云linux主机安装qt报错:缺少libxkbcommon-x11.so.0

ubuntu云主机安装xfce桌面后&#xff0c;下载qt5.12.10&#xff0c;开发桌面应用&#xff0c;安装qt时报错缺少libxkbcommon-x11.so.0。 由于xfce是轻量级桌面不带libxkbcommon-x11.so.0&#xff0c;而qt5.12.10默认系统已经自带&#xff0c;所以&#xff0c;报错。此时更新一下…

Qt treeWidget 查找指定字段内容的条目并跳转到该条目

遍历Qt treeWidget&#xff0c;查找指定字段内容的条目&#xff0c;并跳转到该条目。 void MainWindow::on_pushButton_sidFind_clicked() {QString sid ui->lineEdit_sidFind->text();QTreeWidgetItemIterator it(ui->treeWidget_sqItem);while (*it) {if ((*it)-&…

SpringMVC之控制器的单例和多例管理

版权声明&#xff1a;本文为博主原创文章&#xff0c;未经博主允许不得转载。 在使用Spring3对控制器Controller进行bean管理时&#xff0c;如果要对控制器是否单例进行管理。 有两种方式配置多例模式&#xff1a; 1.springXML 2.注解本身的控制器类 [java] view plaincopyprin…

BZOJ 1997: [Hnoi2010]Planar( 2sat )

平面图中E ≤ V*2-6..一个圈上2个点的边可以是在外或者内, 经典的2sat问题..------------------------------------------------------------------------------------------#include<cstdio>#include<cstring>#include<algorithm>#include<stack>usin…

Qt6.2.1在线安装教程

1、Qt下载官网 Download Qt | Develop Desktop & Embedded Systems | Qt 2、下载后在线安装 安装时会要求登陆账号&#xff0c;这个可以去官网注册账号。 相关模块和版本按需选择。以下为我的项目开发需要安装的部分模块&#xff1a; 然后漫长等待安装结束即可。

Qt6.2.1使用clang格式化代码

1、环境说明 Qt6.2.1 64bit, windows环境, 安装链接&#xff1a;Download Qt | Develop Desktop & Embedded Systems | Qt llvm 10.0.0 64bit windows环境&#xff0c;安装链接&#xff1a; LLVM Download Page 2、启用Beautifer 帮助--关于插件&#xff0c;勾选Beautif…

Getting Started With Hazelcast 读书笔记(第七章)

第七章 部署策略 Hazelcast具有适应性,能根据不同的架构和应用进行特定的部署配置,每个应用可以根据具体情况选择最优的配置: 数据与应用紧密结合的模式(重点,of就是这种) 胖客户端模式(最好用一种) 轻成员节点模式(折衷上面两种) 1.使用紧密结合的模式&#xff0c;每个JVM实例…

Qt 调用Python引擎混合编程

0、前言 使用Qt和Python做混合编程&#xff0c;充分发挥Qt GUI的图形化框架优势和Python数据处理的计算能力。复杂的计算可用到Python众多三方库&#xff0c;将Python需要执行的功能写成py文件&#xff0c;做成插件的形式&#xff0c;被Qt c调用&#xff0c;修改py文件即可实现…

C# 特性 Attribute

特性就是在类的类名称、属性、方法等上面加一个标记&#xff0c;使这些类、属性、方法等具有某些统一的特征&#xff0c;从而达到某些特殊的需要。举个小栗子&#xff1a;方法的异常捕捉&#xff0c;你是否还在某些可能出现异常的地方&#xff08;例如数据库的操作、文件的操作…

Qt 调用MATLAB引擎混合编程

0、前言 C/C调用MATLAB有三个方式&#xff1a;调用dll文件、调用exe可执行程序和调用Engin引擎。 第一种方式利用MATLAB Coder 将MATLAB代码生成C/C代码和相关动态链接库&#xff0c;可以在没有MATLAB的电脑和设备上运行。 第二种方式利用MATLAB将MATLAB代码编译成exe可执行…

xcode3.2.6升级至4.0.2经验加教训总结(转)

首先&#xff0c;完美运行都项目直接在Xcode4.0.2中build可能会出现这样一个错误&#xff08;人品好的自觉绕道&#xff09;&#xff1a;[BEROR]No architectures to compile for (ARCHSi386, VALID_ARCHS). 首先第一反应是打开工程的Build Settings&#xff0c;最基本的检测&a…

Qt 调用VISA库通过SCPI程控仪器-以IT8906A电子负载为例

SCPI是一种可编程仪器的标准命令&#xff0c;可通过 GPIB&#xff0c; RS232&#xff0c; USB&#xff0c; LAN接口等控制仪器功能的编程语言。 SCPI 置于 IEEE 488.2 硬件部分的顶层。相同 SCPI 命令和参数控制着不同机器的相同功能。一般标准仪器都支持SCPI协议&#xff0c;如…

关于如何使用的MSCS(微软群集服务器)中的iSCSI Guest Initiators (客户机启动器)...

发布者&#xff1a;Dell-Terry Z 作者&#xff1a;戴尔公司 William U 嗨&#xff0c;大家好&#xff01;过去的几周实在太激动人心了。第一届戴尔全球存储论坛&#xff08;Dell Storage Forum&#xff09;刚刚结束&#xff0c;我有一大堆的东西想跟大家分享&#xff0c;我想…

openssl版本信息和支持的命令

1、查看openssl版本 openssl version -a openssl version 可以看到版本信息和安装位置: 2、查看openssl支持的命令 openssl help Standard commands asn1parse ca ciphers cms crl crl2pkcs7 dgst …

栅格像元做地理坐标映射后的坐标值是像元中心点还是其他位置?

做了下实验&#xff0c;GDAL做完映射后像元&#xff08;x&#xff0c;y&#xff09;--->(lat,lon)&#xff0c;得到的坐标值是像元左上角的坐标值。 下面这个看到更清楚&#xff1a;&#xff08;其实我做了一个替换&#xff0c;实际我求的是左上点和右下点&#xff08;通过&…