0

我有一个注册程序,当您输入特定数字后跟“L”时,它会查看数据库并将该名称填充到列表框中,后跟Time In: (DateTime.Now),当您再次扫描时,它将删除前一行并输入在第二个列表框中添加了一个额外的部分 - Time Out: (Datetime.Now)

但是,如果我第三次扫描,我希望它再次将其重新输入到 listbox1 中Time In: (DateTime.Now)

在它进入listbox2后我无法让它重新输入,它只是在listbox1中输入一个新条目。

任何帮助都很好,这里是代码:

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;


                foreach (var item in listBox1.Items)
                {
                    var itemEntry = item.ToString();
                    string newEntry = CreateTimeEntry(itemEntry) + DateTime.Now.ToString("HH:mm");

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

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

                   }      

              }

                if (!found)
                {
                        listBox1.Items.Add(returnValue + "      " + "Time In : " + DateTime.Now.ToString("HH:mm"));
                        //MessageBox.Show("Not already in box");
                }

                textBox1.Clear();

                System.IO.StreamWriter SaveFile = new System.IO.StreamWriter(fullFileName);
                foreach (object item in listBox1.Items)
                SaveFile.WriteLine(item.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

0 回答 0