深入.NET平臺和c#理解------第三章上機題員工打卡
阿新 • • 發佈:2019-01-31
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace GenericDemo { public class SE { public string ID { get; set; } public string Name { get; set; } public int Age { get; set; } public string Gender { get; set; } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace GenericDemo { public class Records { public DateTime SignInTime { get; set; } public DateTime SignOutTime { get; set; } public string ID { get; set; } public string Name { get; set; } } }
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 GenericDemo { public partial class FrmMain : Form { public FrmMain() { InitializeComponent(); } //窗體的跳轉 private void toolStripButton1_Click(object sender, EventArgs e) { FrmMaintance frm = new FrmMaintance(); frm.MaintanceType = 1; frm.FrmParent = this; frm.Show(); } //建立員工資訊集合 public List<SE> programmerList = new List<SE>(); //顯示資料 public void BindGrid(List<SE> list) { this.dvgprogrammer.DataSource = new BindingList<SE>(list); } private void btnSee_Click(object sender, EventArgs e) { IndexOf(); } //模糊查詢 public string IndexOf() { List<SE> tempList = new List<SE>(); foreach (SE item in this.programmerList) { if (item.ID.IndexOf(this.txtID.Text.Trim()) != -1) { tempList.Add(item); } } //重新整理 this.dvgprogrammer.DataSource = new BindingList<SE>(tempList); return null; } //刪除資訊 private void toolStripButton3_Click(object sender, EventArgs e) { DialogResult result = MessageBox.Show("要刪除員工資訊嗎?","輸入提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Question); // MessageBox.Show(result.ToString()); OK if (result == DialogResult.OK) { foreach (SE item in programmerList) { //選中整行時返回的值與員工ID相同時,刪除所選中的 if (this.dvgprogrammer.SelectedRows[0].Cells[0].Value.ToString() == item.ID) { this.programmerList.Remove(item); break; } } //重新整理窗體 this.dvgprogrammer.DataSource = new BindingList<SE>(programmerList); } } //打卡記錄的Dictionary public Dictionary<string, Records> recordList = new Dictionary<string, Records>(); //簽到操作 private void tsmiSignIn_Click(object sender, EventArgs e) { //驗證是否有選中的行 if (this.dvgprogrammer.SelectedRows.Count != 1) { MessageBox.Show("請選中一行!"); return; } //確保沒有簽到過 string workNo = dvgprogrammer.CurrentRow.Cells["workNo"].Value.ToString(); MessageBox.Show(workNo); foreach (string id in recordList.Keys) { if (workNo == id) { MessageBox.Show("您已經簽到過了!"); return; } } //簽到 Records record = new Records(); record.ID = workNo; record.Name = this.dvgprogrammer.CurrentRow.Cells["name"].Value.ToString(); record.SignInTime = DateTime.Now; //添加簽到資訊到記錄中 this.recordList.Add(record.ID, record); MessageBox.Show("簽到成功!"); } //簽退操作 private void tsmiSignOut_Click(object sender, EventArgs e) { //驗證是否有選中的行 if (this.dvgprogrammer.SelectedRows.Count != 1) { MessageBox.Show("請選中一行!"); return; } string ID = dvgprogrammer.CurrentRow.Cells["workNo"].Value.ToString(); //標識是否已簽到過 bool isOut = false; //遍歷key,與ID對比,若相等可以簽退,反之不行. foreach (string key in recordList.Keys) { if (key == ID) { //簽退時間 this.recordList[key].SignOutTime = DateTime.Now; MessageBox.Show("簽退成功!"); isOut = true; break; } } if (!isOut) { MessageBox.Show("很抱歉,請簽到!"); } } private void toolStripButton4_Click(object sender, EventArgs e) { FrmRecords frms = new FrmRecords(); frms.recordList = this.recordList; frms.Show(); } } }
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 GenericDemo
{
public partial class FrmMaintance : Form
{
public FrmMain FrmParent { get; set; }
public FrmMaintance()
{
InitializeComponent();
//載入窗體時下拉框中為"男"
this.cmbGender.SelectedIndex = 0;
}
public int MaintanceType;
private void FrmUpdate_Load(object sender, EventArgs e)
{
}
private void btnOK_Click(object sender, EventArgs e)
{
try
{
//插入資料
SE se = new SE();
se.ID = this.txtId.Text.Trim();
se.Name = this.txtName.Text.Trim();
se.Age = Int32.Parse(this.txtAge.Text.Trim());
if (this.cmbGender.SelectedItem.ToString() == "男")
{
se.Gender = "男";
}
else
{
se.Gender = "女";
}
//工號驗證
if (FrmParent.programmerList.Equals(se.ID))
{
MessageBox.Show("此工號存在!");
return;
}
//將資料新增FrmMain窗體中的programmerList裡
FrmParent.programmerList.Add(se);
MessageBox.Show("是否新增員工資訊", "輸入提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
//新增成功,關閉窗體
this.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
//重新整理FrmMain窗體
this.FrmParent.BindGrid(FrmParent.programmerList);
}
}
}
}
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 GenericDemo
{
public partial class FrmRecords : Form
{
//打卡記錄
public Dictionary<string, Records> recordList { get; set; }
public FrmRecords()
{
InitializeComponent();
}
private void FrmRecords_Load(object sender, EventArgs e)
{
BindingSource bs = new BindingSource();
bs.DataSource = recordList.Values;
this.dvgRecords.DataSource = bs;
Rows();
}
public void Rows()
{
int row = this.dvgRecords.RowCount;
this.lbRecord.Text = "共有" + row + "條打卡記錄";
}
}
}