标签:
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<Person> list = new List<Person>(); 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<Person> List = new List<Person>(); 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("没有此人!"); } } }
标签:
原文地址:http://www.cnblogs.com/WuXuanKun/p/5483310.html