用窗体写一个员工打卡系统
效果图
//主窗体代码
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 using System.Collections; 11 12 namespace Qiandao 13 { 14 public partial class FrmMain : Form 15 { 16 public FrmMain() 17 { 18 InitializeComponent(); 19 this.dataGridView1.AutoGenerateColumns = false; 20 } 21 22 23 private Dictionary<string, Record> recordList = new Dictionary<string, Record>(); 24 public List<SE> abc = new List<SE>(); 25 public void Show() 26 { 27 SE aa = new SE(); 28 aa.id = "012"; 29 aa.name = "嘻嘻"; 30 aa.sex = "男"; 31 aa.age = 20; 32 33 SE bb = new SE(); 34 bb.id = "123"; 35 bb.name = "哈哈"; 36 bb.sex = "男"; 37 bb.age = 20; 38 39 SE cc = new SE(); 40 cc.id = "234"; 41 cc.name = "呵呵"; 42 cc.sex = "男"; 43 cc.age = 20; 44 45 abc.Add(aa); 46 abc.Add(bb); 47 abc.Add(cc); 48 49 dataGridView1.DataSource = new BindingList<SE>(abc); 50 } 51 52 public void BindGrid(List<SE> List) 53 { 54 this.dataGridView1.DataSource = new BindingList<SE>(abc); 55 } 56 private void Form1_Load(object sender, EventArgs e) 57 { 58 Show(); 59 } 60 61 private void button1_Click(object sender, EventArgs e) 62 { 63 List<SE> abcd = new List<SE>(); 64 65 foreach (SE item in this.abc) 66 { 67 if (item.id.IndexOf(this.textBox1.Text.Trim()) != -1) 68 { 69 abcd.Add(item); 70 } 71 } 72 this.dataGridView1.DataSource = new BindingList<SE>(abcd); 73 } 74 75 private void 新增ToolStripMenuItem_Click(object sender, EventArgs e) 76 { 77 FrmInsert ins = new FrmInsert(); 78 ins.cnm = this; 79 80 ins.Show(); 81 } 82 83 private void 修改ToolStripMenuItem_Click(object sender, EventArgs e) 84 { 85 86 } 87 88 private void 删除ToolStripMenuItem_Click(object sender, EventArgs e) 89 { 90 if (this.dataGridView1.SelectedRows.Count != 1) 91 { 92 MessageBox.Show("请选中一行"); 93 return; 94 } 95 DialogResult result = MessageBox.Show("确定吗", "信息"); 96 if (result != DialogResult.OK) 97 { 98 return; 99 } 100 string No = dataGridView1.CurrentRow.Cells["工号"].Value.ToString(); 101 foreach (SE item in abc) 102 { 103 if (item.id == No) 104 { 105 this.abc.Remove(item); 106 break; 107 } 108 } 109 this.BindGrid(this.abc); 110 MessageBox.Show("删除成功", "执行信息"); 111 } 112 113 private void 签到ToolStripMenuItem_Click(object sender, EventArgs e) 114 { 115 if (this.dataGridView1.SelectedRows.Count != 1) 116 { 117 MessageBox.Show("请选中一行"); 118 return; 119 } 120 string workNo = dataGridView1.CurrentRow.Cells["工号"].Value.ToString(); 121 foreach (string id in recordList.Keys) 122 { 123 if (workNo == id) 124 { 125 MessageBox.Show("您已签到过"); 126 return; 127 } 128 } 129 130 Record record = new Record(); 131 record.ID = workNo; 132 record.Name = dataGridView1.CurrentRow.Cells["姓名"].Value.ToString(); 133 record.SignInTime = DateTime.Now; 134 this.recordList.Add(record.ID, record); 135 MessageBox.Show("签到成功"); 136 } 137 138 private void 签退ToolStripMenuItem_Click(object sender, EventArgs e) 139 { 140 if (this.dataGridView1.SelectedRows.Count != 1) 141 { 142 MessageBox.Show("请选中一行"); 143 return; 144 } 145 string ID = dataGridView1.CurrentRow.Cells["工号"].Value.ToString(); 146 bool isOut = false; 147 foreach (string key in recordList.Keys) 148 { 149 if (key==ID) 150 { 151 this.recordList[key].SignOutTime = DateTime.Now; 152 MessageBox.Show("签退成功"); 153 isOut = true; 154 break; 155 } 156 } 157 if (!isOut) 158 { 159 MessageBox.Show("很抱歉请继续签退"); 160 } 161 } 162 163 private void 打卡记录ToolStripMenuItem_Click(object sender, EventArgs e) 164 { 165 FrmMaa frm = new FrmMaa(); 166 frm.recordList = this.recordList; 167 frm.Show(); 168 } 169 } 170 }
//显示签到时间窗体的代码
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 Qiandao 12 { 13 public partial class FrmMaa : Form 14 { 15 public FrmMaa() 16 { 17 InitializeComponent(); 18 } 19 public Dictionary<string, Record> recordList { get; set; } 20 private void frmMaa_Load(object sender, EventArgs e) 21 { 22 BindingSource bs = new BindingSource(); 23 bs.DataSource = recordList.Values; 24 this.dataGridView.DataSource = bs; 25 } 26 27 28 } 29 }
//新增员工窗体的代码
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 Qiandao 12 { 13 14 public partial class FrmInsert : Form 15 { 16 public FrmMain cnm; 17 18 public FrmMain FrmMain {get; set;} 19 public FrmInsert() 20 { 21 InitializeComponent(); 22 this.comboBox1.SelectedIndex = 0; 23 } 24 25 26 27 private void button1_Click(object sender, EventArgs e) 28 { 29 30 SE pr = new SE(); 31 pr.id = this.txtId.Text.Trim(); 32 pr.age = Int32.Parse(this.txtAge.Text.Trim()); 33 if (this.comboBox1.SelectedItem.ToString() == "男") 34 { 35 pr.sex = "男"; 36 } 37 else { 38 pr.sex = "女"; 39 } 40 41 pr.name = this.txtName.Text.Trim(); 42 43 44 foreach (SE item in cnm.abc) 45 { 46 if (item.id == pr.id) { 47 MessageBox.Show("此工号已存在"); 48 return; 49 } 50 } 51 cnm.abc.Add(pr); 52 53 this.cnm.BindGrid(cnm.abc); 54 55 56 FrmMain frm = new FrmMain(); 57 this.Hide(); 58 frm.Show(); 59 60 61 } 62 } 63 }
//SE类
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace Qiandao 8 { 9 public class SE 10 { 11 public string id { get; set; } 12 public string name { get; set; } 13 public string sex { get; set; } 14 public int age { get; set; } 15 16 } 17 }
//Record类
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace Qiandao 8 { 9 public class Record 10 { 11 public DateTime SignInTime { get; set; } 12 public DateTime SignOutTime{ get; set; } 13 public string ID { get; set; } 14 public string Name { get; set; } 15 } 16 }