C#之考勤系统

Posted 瞿亮

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C#之考勤系统相关的知识,希望对你有一定的参考价值。

员工类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 第三章_上机练习一_考勤管理
{
    class sign
    {
        private string _id;//员工工号
        private string _name;//员工姓名
        private DateTime _gettime;//签到时间
        private DateTime _outtime;//签退时间

        public DateTime Outtime
        {
            get { return _outtime; }
            set { _outtime = value; }
        }

        public DateTime Gettime
        {
            get { return _gettime; }
            set { _gettime = value; }
        }


        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }
        public string Id
        {
            get { return _id; }
            set { _id = value; }
        }

    public static    Dictionary<string, sign> dic = new Dictionary<string, sign>();

    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 第三章_上机练习一_考勤管理
{
   public class SE
    {
        private string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        private int id;

        public int Id
        {
            get { return id; }
            set { id = value; }
        }
        private int age;

        public int Age
        {
            get { return age; }
            set { age = value; }
        }
        private string sex;

        public string Sex
        {
            get { return sex; }
            set { sex = value; }
        }

        public static List<SE> list = new List<SE>();

    }
}
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 FrmqdTable : Form
    {
        public FrmqdTable()
        {
            InitializeComponent();
        }

        public Form1 table;
        private void FrmqdTable_Load(object sender, EventArgs e)
        {

            label1.Text = string.Format("你好,共有{0}条打卡记录", sign.dic.Count);
            dataGridView1.AutoGenerateColumns = false;
            BindingSource bs = new BindingSource();
            bs.DataSource = sign.dic.Values;
            dataGridView1.DataSource = bs;
        }

        private void button1_Click(object sender, EventArgs e)
        {
          //  string id = dataGridView1.SelectedRows[0].Cells[2].Value.ToString();
         //    MessageBox.Show(sign.dic[id].Outtime.ToString());
                  
             
            Form1 f = new Form1();
            f.table = this;
            this.Hide();
            f.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 第三章_上机练习一_考勤管理
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public FrmqdTable table;
        private void 新增ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FrmAdd a = new FrmAdd();
            a.main = this;
            a.Show();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Initial();
        }


  public void Initial() {
      SE s1 = new SE();
      s1.Name = "二狗子";
      s1.Age = 28;
      s1.Sex = "";
      s1.Id = 1;

      SE.list.Add(s1);

      SE s2 = new SE();
      s2.Name = "三狗子";
      s2.Age = 20;
      s2.Sex = "";
      s2.Id = 2;

      SE.list.Add(s2);


      SE s3 = new SE();
      s3.Name = "五狗子";
      s3.Age = 34;
      s3.Sex = "";
      s3.Id = 3;

      SE.list.Add(s3);
      BindGrid(SE.list);
      dataGridView1.DataSource = SE.list;   

        }

  private void button1_Click(object sender, EventArgs e)
  {
      if (textBox1.Text=="")
      {
          dataGridView1.DataSource = SE.list;
          return;
      }

       List<SE> searchList = new List<SE>();
     
        //定义打卡记录的Dictionary
      if (isNaN(textBox1.Text) == true)
      {
      string id = this.textBox1.Text;
          int myid = Convert.ToInt32(id);

          foreach (SE item in SE.list)
          {
                 if (item.Id==myid)
              {
                  searchList.Add(item);
              }
              else
              {
                  BindGrid(SE.list);
              }
          }

          if (textBox1.Text != "")
          {
              //刷新dgvlist
              BindGrid(searchList);
          }
          
      }
      else {
          MessageBox.Show("不好意思,您输入的不是数字!");
      }
  }


  public bool isNaN(string temp)
  {

      for (int i = 0; i < temp.Length; i++)
      {
          byte tempByte = Convert.ToByte(temp[i]);   //设置byte格式
          if ((tempByte < 48) || (tempByte > 57))   //如果改tempByte不在范围内
          {
              return false;
          }

      }

      return true;
  }


  public  void BindGrid(List<SE> list)
  {
      dataGridView1.DataSource = new BindingList<SE>(list);
  }

  private void 签到ToolStripMenuItem_Click(object sender, EventArgs e)
  {

      //确保有选中的行
      if (dataGridView1.SelectedRows.Count != 1)
      {
          MessageBox.Show("请选择一行");
          return;
      }


      string num = dataGridView1.SelectedRows[0].Cells["id"].Value.ToString();

      foreach (string item in sign.dic.Keys)
      {
          if (item==num)
          {
              MessageBox.Show("你已经签到过了");
              return;
          }
      }

      sign s = new sign();
      s.Id = num;
      s.Name = dataGridView1.SelectedRows[0].Cells["name"].Value.ToString();
      s.Gettime = DateTime.Now;
      sign.dic.Add(s.Id,s);
      opt = true;
      MessageBox.Show("你已经成功签到了!!!");
  }

  public static int i=0;
  private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
  {
      if (dataGridView1.SelectedRows.Count != 1)
      {
          MessageBox.Show("请选择一行");
      }
      else
      {
          DialogResult dr = MessageBox.Show("确认删除该条员工信息吗?", "温馨提示", MessageBoxButtons.OKCancel);
          if (dr == DialogResult.OK)
          {
              int num = Convert.ToInt32(dataGridView1.Rows[i].Cells["id"].Value);
      
              for (int j = 0; j <SE.list.Count; j++)
              {
                  //如果存在员工号则删除
                  if (SE.list[j].Id == num)
                  {
                      SE.list.RemoveAt(j);
                  }
              }

          }
          //刷新dgvlist
          BindGrid(SE.list);
      }
  }

  public static int counter = 0;
  public static  bool  opt = false;
  private void 签退ToolStripMenuItem_Click(object sender, EventArgs e)
  {
    
      
      if (dataGridView1.SelectedRows.Count != 1)
      {
          MessageBox.Show("请选择一行");
      }
      else
      {
          string id = dataGridView1.SelectedRows[0].Cells["id"].Value.ToString();
          //bool result=sign.dic.ContainsKey(id);  
         
          bool flag = false;
          
          foreach (var item in sign.dic.Keys)
          {
              if (item==id)
              {
                  sign.dic[item].Outtime = DateTime.Now;

                  //MessageBox.Show(sign.dic[item].Outtime.ToString());
                  if (!(sign.dic[item].Gettime.AddSeconds(7) < sign.dic[item].Outtime))
                  {
                      //      MessageBox.Show(sign.dic[item].Gettime.AddSeconds(1).ToString());
                      MessageBox.Show("还没工作完,不许签退");
                      counter = 1;

                      return;
                  }

                  
                   if(counter==0){
                  //证明已经执行签到,设置签退时间
                      sign.dic[item].Outtime = DateTime.Now;
                      MessageBox.Show("签退成功!");
                      opt = true;
                      i++;
                    flag = true;
                    i = 0;
                    counter++;
                      break;
                   }
                 
                   else
                   {
                       MessageBox.Show("已经签到了");
                       return;

                   }
              }
             
          } 
      }
  }

  private void 打卡记录ToolStripMenuItem_Click(object sender, EventArgs e)
  {
      FrmqdTable t = new FrmqdTable();
      t.table = this;
      t.Show();
      this.Hide();

  }

  private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
  {

  }
    }
}

三个窗体的全部代码,但是一直点签退的时候,唯独这里有漏洞,如果有大牛解决的话,希望能在下面留言,方便大家交流学习,谢谢!!!!

以上是关于C#之考勤系统的主要内容,如果未能解决你的问题,请参考以下文章

C# 最有用的(自定义)代码片段是啥? [关闭]

c#代码片段快速构建代码

此 Canon SDK C++ 代码片段的等效 C# 代码是啥?

是否可以动态编译和执行 C# 代码片段?

C#常用代码片段备忘

优化 C# 代码片段、ObservableCollection 和 AddRange