public partial class Form1 : Form { public Form1() { InitializeComponent(); }
//FROM 1 中的内容 //公共列表<SE>工程师=新列表<SE>(); public List<SE> engineers = new List<SE>(); //公共<string,Record>列表显示<string,Record>(); public Dictionary<string, Record> recordList = new Dictionary<string, Record>(); public void initial() { //初始化信息 SE wang = new SE(); wang.Name = "王小毛"; wang.Age = 19; wang.ID = "20090101"; wang.gender = Gender.男; SE joke = new SE(); joke.Name = "周新宇"; joke.Age = 19; joke.ID = "20090102"; joke.gender = Gender.女; //添加元素 engineers.Add(wang); engineers.Add(joke); } //泛型添加 //公共void BindGrid(<SE>列出) public void BindGrid(List<SE> list) { //dataGridView1。 数据源=新BindingList<SE>(列表); //添加显示数据 dataGridView1.DataSource = new BindingList<SE>(list); } private void Form1_Load(object sender, EventArgs e) { //引用方法 initial(); BindGrid(engineers); } private void button1_Click(object sender, EventArgs e) { //<SE>工程师=新列表List<SE>(); List<SE> engineer = new List<SE>(); //foreach(SE项目工程师) foreach(SE item in engineers) { //判断数据是否存在 if(item.ID.IndexOf(textBox1.Text.Trim())!=-1) { engineer.Add(item);//导入数据 } } BindGrid(engineer);//泛型添加 } private void toolStripButton1_Click(object sender, EventArgs e) { Form2 frm = new Form2(); frm.frmparent = this; frm.ShowDialog();//模式化窗体 显示 } private void toolStripButton3_Click(object sender, EventArgs e) { //提示 是否删除 DialogResult i = MessageBox.Show("是否删除", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (i == DialogResult.Yes) { //字符串id=dataGridView1. 隐藏所选择[0] ToString(); string id = dataGridView1.SelectedRows[0].Cells[3].Value.ToString(); foreach (SE item in engineers) { if (item.ID==id) { engineers.Remove(item);// 删除 break; } } BindGrid(engineers); } } //签到操作 private void 签到ToolStripMenuItem_Click(object sender, EventArgs e) { // string workNo = dataGridView1.SelectedRows[0].Cells[3].Value.ToString(); //验证 确保有选中的行 foreach (string id in recordList.Keys) { if (id == workNo) { MessageBox.Show("今天已经签到过了"); return; } } //执行签到 Record record = new Record(); record.ID = workNo; record.Name = dataGridView1.SelectedRows[0].Cells[0].Value.ToString(); record.SignInTime = DateTime.Now; //获取系统的当前时间 recordList.Add(record.ID, record);//添加到签到记录中 MessageBox.Show("签到成功"); } //签退操作 private void 签退ToolStripMenuItem_Click(object sender, EventArgs e) { string ID = dataGridView1.SelectedRows[0].Cells[3].Value.ToString(); bool isOut = false; //标识是否已经签到过 foreach(string key in recordList.Keys) { if(key==ID) { //执行签到,设置签退的时间 this.recordList[key].SignOutTime = DateTime.Now; MessageBox.Show("签退成功"); isOut = true; break; } } if(isOut==false) //不成立 { MessageBox.Show("尚未签到"); } } private void toolStripButton4_Click(object sender, EventArgs e) { Form3 frm = new Form3(); frm.name = "共有" + recordList .Count+ "条打卡记录"; //窗体传值 frm.frmparent = this; frm.ShowDialog(); }
//Form 2中的内容
public partial class Form2 : Form { public Form2() { InitializeComponent(); } //定义Form1 公用的属性 public Form1 frmparent{ get; set; } private void button1_Click(object sender, EventArgs e) { //初始化信息 SE pr = new SE(); //窗体传值 pr.ID = textBox1.Text; pr.Name = textBox3.Text; pr.Age = Convert.ToInt32(textBox2.Text); //判断男女 if (comboBox1.SelectedValue.ToString() == "男") { pr.gender = Gender.男; } else { pr.gender = Gender.女; } foreach (SE item in frmparent.engineers) { if (item.ID == pr.ID) { MessageBox.Show("此工号已存在"); return; } } //绑定数据源 frmparent.engineers.Add(pr); this.Close(); this.frmparent.BindGrid(frmparent.engineers); } private void Form2_Load(object sender, EventArgs e) { Com();//调用Com方法 } private void Com() { //初始化 ComboBoxItem<Gender> itemmale = new ComboBoxItem<Gender>(); itemmale.ItemText = Gender.男.ToString(); itemmale.ItemValue = Gender.男; ComboBoxItem<Gender> itemfemale = new ComboBoxItem<Gender>(); itemfemale.ItemText = Gender.女.ToString(); itemfemale.ItemValue = Gender.女; //声明集合 List<ComboBoxItem<Gender>> item = new List<ComboBoxItem<Gender>>(); //添加集合中的数据 item.Add(itemmale); item.Add(itemfemale); //绑定信息 comboBox1.DataSource = item; comboBox1.DisplayMember = "ItemText"; comboBox1.ValueMember = "ItemValue"; }
//Form 3中的内容
public partial class Form3 : Form { public Form3() { InitializeComponent(); } //定义Form3的公共属性 public string name = ""; //定义Form1 公用的属性 public Form1 frmparent { get; set; } private void Form3_Load(object sender, EventArgs e) { label1.Text = name;//绑定数据 NewMethod();//调用方法 } private void NewMethod() { //创建数据源的组件 // 用BindingSource可以提高程序的性能和安全性 BindingSource bs = new BindingSource(); //把集合元素添加到数据原中 bs.DataSource = frmparent.recordList.Values; //this 本对象 //展示数据源中的数据 this.dataGridView1.DataSource = bs; } private void button1_Click(object sender, EventArgs e) { //消息框.显示(frmparent。列表显示。 计数。 ToString()); //消息框展示集合中元素的数量 MessageBox.Show(frmparent.recordList.Count.ToString()); }
//ComboBoxItem类
class ComboBoxItem<T> { public string ItemText { get; set; } public T ItemValue { get; set; } } }
//Gender类
//公开枚举性别 public enum Gender { 男,女 }
//Record类
public class Record { //保存父窗体的使用 //签到的时间 public DateTime SignInTime { get; set; } //签退的时间 public DateTime SignOutTime { get; set; } //工号 public string ID { get; set; } //员工姓名 public string Name { get; set; } } }
//SE 类
public class SE { //公共名称的字符串 public string Name { get; set; } public int Age { get; set; } public Gender gender { get; set; } public string ID { get; set; } }