0

我的注册程序在人们扫描自己并隐藏他们时使用他们的姓名、id 和“Time In:”登录,但在他们的名字中添加“Time Out:”,反之亦然。我想做的是每次这个人扫描自己“In”时,我希望它查看“In”和“out”的时间,并计算在办公室的总时间。

附上代码:

    private string CreateTimeEntry(string current)
    {
        var indexIn = current.LastIndexOf("Time In : "); // Get the last index of the word "in"
        var indexOut = current.LastIndexOf("Time Out : "); // Get the last index of the word out
        string timeIn = current + "      " + "Time In : ";
        string timeOut = current + "      " + "Time Out : ";

        if (indexOut > indexIn)
        {
            // if the last "out" comes after the last "in"
            return timeIn;
        }
        else
        {
           // If the last "in" comes after the last "out"
            return timeOut;
        }
    }

    private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
        SqlConnection DBConnection = new SqlConnection("Data Source=DATABASE;Initial Catalog=imis;Integrated Security=True");
        SqlCommand cmd = new SqlCommand();

        Object returnValue;

        string txtend = textBox1.Text;

        if (e.KeyChar == 'L')
        {   
            DBConnection.Open();
        }
        if (DBConnection.State == ConnectionState.Open)
        {
            if (textBox1.Text.Length != 6) return;
            {
                cmd.CommandText = ("SELECT last_name +', '+ first_name from name where id =@Name");
                cmd.Parameters.Add(new SqlParameter("Name", textBox1.Text.Replace(@"L", "")));
                cmd.CommandType = CommandType.Text;
                cmd.Connection = DBConnection;

                returnValue = cmd.ExecuteScalar() + "\t (" + textBox1.Text.Replace(@"L", "") + ")";

                DBConnection.Close();

                bool found = false;

                System.DateTime resultTime1 = new System.DateTime(;

                foreach (var item in listBox1.Items)
                {
                    var itemEntry = item.ToString();

                    string newEntry = CreateTimeEntry(itemEntry) + DateTime.Now.ToString("HH:mm") + "   " + "Total Time: " + resultTime1 ;

                    if (itemEntry.Contains(returnValue.ToString()))
                    {
                        var indexIn = itemEntry.LastIndexOf("Time In : ");
                        var indexOut = itemEntry.LastIndexOf("Time Out : ");

                        if (indexOut > indexIn)
                        {
                            listBox2.Items.Remove(item);
                            listBox1.Items.Add(newEntry);
                            found = true;
                            break;
                        }
                        else
                        {
                            listBox1.Items.Remove(item);
                            listBox2.Items.Add(newEntry);
                            found = true;
                            break;
                        }

                    }

                }

                if (!found)
                {
                    string newEntry2 = "";
                    foreach (string str in listBox2.Items)
                    {
                        var itemEntry2 = str;
                        newEntry2 = CreateTimeEntry(itemEntry2) + DateTime.Now.ToString("HH:mm");

                        //if (listBox2.Items.Contains(returnValue.ToString()))
                        if (listBox2.Items.Contains(str) && str.Contains(textBox1.Text))
                        {
                            var indexIn = itemEntry2.LastIndexOf("Time In : ");
                            var indexOut = itemEntry2.LastIndexOf("Time Out : ");

                            if (indexOut > indexIn)
                            {
                                listBox2.Items.Remove(str);
                                listBox1.Items.Add(newEntry2);
                                found = true;
                                break;
                            }
                        }
                    }
                    var itemEntry = listBox1.Items.ToString();
                    var itemEntry1 = listBox2.Items.ToString();

                    if (!listBox1.Items.Contains(newEntry2))
                    {
                        listBox1.Items.Add(returnValue + "      " + "Time In : " + DateTime.Now.ToString("HH:mm"));
                    }
                }
            }

            textBox1.Clear();

            System.IO.StreamWriter SaveFile = new System.IO.StreamWriter(fullFileName);
            foreach (object item1 in listBox1.Items)
                SaveFile.WriteLine(item1.ToString());
            SaveFile.Flush();
            SaveFile.Close();

            if (listBox1.Items.Count != 0) { DisableCloseButton(); }
            else
            {
                EnableCloseButton();
            }
            Current_Attendance_Label.Text = "Currently " + listBox1.Items.Count.ToString() + " in attendance.";
            e.Handled = true;
        }
4

1 回答 1

1

您可以使用以下方法获取两个 DateTime 对象之间的差异:

TimeSpan timePassed = timeOut.Subtract(timeIn);

wheretimeOuttimeInDateTime对象。

如果您需要将差异(或任何一个时间)显示为字符串,我建议您仅在进行所需的计算后将它们转换为字符串。尽可能使用强类型对象。

于 2013-10-11T14:19:33.017 回答