博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
点名器
阅读量:6914 次
发布时间:2019-06-27

本文共 7428 字,大约阅读时间需要 24 分钟。

1.搭建窗体

点名器实现页面效果图

需要实现功能块

统计添加后的姓名

添加页面

删除页面

修改页面

首先创建一个Person类    用到了List<T> 集合初始化

using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Runtime.Serialization.Formatters.Binary;using System.Text;using System.Threading.Tasks;namespace 点名器{    [Serializable]    public class Person    {        public List
list = new List
(); public string name { get; set; } public string imageindex { get; set; } public void Save() { FileStream fs = new FileStream("save.bin",FileMode .OpenOrCreate);//导命名空间using System.IO; BinaryFormatter bf = new BinaryFormatter(); //导命名空间using System.Runtime.Serialization.Formatters.Binary; bf.Serialize(fs, list); fs.Close(); } public void Return() { //首先定义一个数组 FileStream fs = new FileStream("save.bin",FileMode.Open); BinaryFormatter bf = new BinaryFormatter(); list = (List< Person>) bf.Deserialize(fs); } }}

在主窗体中调用各页面,和Timer事件   在退出的时候保存用到了序列化和反序列化

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 点名器{    public partial class FrmMain : Form    {        public FrmMain()        {            InitializeComponent();        }        List
List = new List
(); Person Main = new Person(); private void FrmMain_Load(object sender, EventArgs e) { //主窗体页面 Main.Return(); List = Main.list; } private void btnEntry_Click(object sender, EventArgs e) { //登录按钮 this.timer.Enabled = true; } private void btnCease_Click(object sender, EventArgs e) { //停止按钮 this.timer.Enabled = false; } int i = 0; private void timer_Tick(object sender, EventArgs e) { //时间控件 if (i < List.Count) { txtFortunate.Text = List[i].name; //把图片放在bin目录下 新建文件夹下中 this.pictureBox1.Image = Image.FromFile("新建文件夹\\"+i+".jpg");//图片 i++; } else { i = 0; } } private void 查询ToolStripMenuItem_Click(object sender, EventArgs e) { //查询 FrmInquiry frm = new FrmInquiry(); frm.Inquiry.list = List; frm.Show(); } private void 增加ToolStripMenuItem_Click(object sender, EventArgs e) { //增加 FrmAdd frm = new FrmAdd(); frm.adds.list = List; frm.Show(); } private void 删除ToolStripMenuItem_Click(object sender, EventArgs e) { //删除 FrmAdd frm = new FrmAdd(); frm.adds.list = List; frm.num = 1; frm.btnAdd.Text = "删除"; frm.Text = "删除"; frm.lblName.Text = "请输入要删除的人"; frm.Show(); } private void 修改ToolStripMenuItem_Click(object sender, EventArgs e) { //修改 FrmRevise frm = new FrmRevise(); frm.Revise.list = List; frm.Show(); } private void FrmMain_FormClosing(object sender, FormClosingEventArgs e) { DialogResult result = MessageBox.Show("是否保存?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning); if (result == DialogResult.OK) { Main.Save(); MessageBox.Show("保存成功!"); } } }}

统计人数   用到了forearm遍历输出

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 点名器{    public partial class FrmInquiry : Form    {        public FrmInquiry()        {            InitializeComponent();        }        public Person Inquiry = new Person();        private void FrmInquiry_Load(object sender, EventArgs e)        {            foreach (Person item in Inquiry.list)            {                listBox.Items.Add(item.name);            }        }    }}

 

1添加和删除用到了同一个页面(窗体的复用),实现了各自的功能块

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 点名器{    public partial class FrmAdd : Form    {        public FrmAdd()        {            InitializeComponent();        }        public Person adds = new Person();        public int num = 0;        Person ps = new Person();        private void btnAdd_Click(object sender, EventArgs e)        {            if(string.IsNullOrEmpty(txtName.Text))            {                MessageBox.Show("不能为空!");                return;                        }            if (num == 0)            {               ps.name = this.txtName.Text;                foreach (Person item in adds.list)                {                    if (item.name == ps.name)                    {                        MessageBox.Show("已经存在!");                        return;                    }                }                adds.list.Add(ps);                MessageBox.Show("增加成功!");                txtName.Text = "";            }            else            {                ps.name = this.txtName.Text;                foreach (Person  item in adds.list )                {                    if (item.name == ps.name)                    {                        adds.list.Remove(item);                        MessageBox.Show("删除成功!");                                              return;                    }                                  }                MessageBox.Show("删除失败!");            }            txtName.Text = "";            txtName.Focus();                 }    }}

1.修改中,判断了listBox控件中是否存在该人,存在就修改,否则输出没有该人,请确定后重新修改

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 点名器{    public partial class FrmRevise : Form    {        public FrmRevise()        {            InitializeComponent();        }        public Person Revise = new Person();        private void btnRevise_Click(object sender, EventArgs e)        {            Person ps = new Person();            ps.name = this.txtFront.Text;            foreach (Person  item in Revise.list)            {                if (item.name == this.txtFront.Text)                {                    DialogResult result = MessageBox.Show("是否修改?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);                    if (result == DialogResult.OK)                    {                        item.name = txtBehind.Text;                        MessageBox.Show("修改成功!");                        return;                    }                    else                    {                        MessageBox.Show("修改失败!");                        txtFront.Focus();                        return;                    }                }            }            MessageBox.Show("没有此人!");        }    }}

 

转载于:https://www.cnblogs.com/WuXuanKun/p/5483310.html

你可能感兴趣的文章
对Html5 Web App 的背景调查研究
查看>>
SQL GROUP BY 语句
查看>>
.NET 动态脚本语言Script.NET 开发指南
查看>>
根绝ip限制访问
查看>>
ubuntu下virtualbox配置host-only网络
查看>>
Windows Server 2008 IIS7.0 发布html和Asp.net网站
查看>>
分布式搜索方案选型
查看>>
简单介绍一些HTML代码(字幕、音频和视频)
查看>>
[深入浅出Cocoa]iOS网络编程之NSStream
查看>>
Cocos2d-html5 笔记2: director
查看>>
程序猿你是否有这些理解误区?
查看>>
C语言整理——文件系统和文件访问
查看>>
阿里面试
查看>>
ECSHOP修改后台地址
查看>>
运动补偿与运动估计
查看>>
jQuery的一个关键函数
查看>>
表单模型+安装目录+侵入表单模型
查看>>
系统如何端子app弄root才干
查看>>
HTML表格边框的设置小技巧
查看>>
jmeter ---模拟http请求/发送gzip数据
查看>>