1

我编写的程序是一个寄存器,当扫描条形码时,它将进入数据库并提取人员姓名和身份证号,并列出他们扫描时的“时间:”。并且然后当他们第二次扫描时,它会添加一个“超时:”部分,如下所示:

Peters, Alastair (242242) Time In : 14:50 Time Out : 14:50 Time In : 14:50

我正在寻找它做的是当我第一次扫描时你会看到:

Peters, Alastair (242242) Time In : 14:50

然后当您第二次扫描时,它将添加Time Out :然后删除整行并将其存储在第二个列表框中,因此当人不在时它不会显示。

然后当该人(第三次)扫描时,它将重新列出上一行,但使用新的Time In

任何建议或想法都会非常有帮助!

这是我的代码:

    private string CreateNewEntry(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

        if (indexOut > indexIn)
        {
            return current + "      "+"Time In : "; // if the last "out" comes after the last "in"
        }
        else
        {
            // If the last "in" comes after the last "out"
            return current + "      " +"Time Out : ";
        }
    }

    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 entry = item.ToString();
                    if (entry.Contains(returnValue.ToString()))
                    {
                        listBox1.Items.Remove(item);
                        listBox1.Items.Add(CreateNewEntry(entry) + DateTime.Now.ToString("HH:mm"));
                        found = true;
                        break;
                    }
                }

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

                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

1 回答 1

0

我了解起始位(例如,“Peters, Alastair (242242)”)是唯一的并且保持不变;在这种情况下,您可以使用FindString. 示例代码:

int curIndex = listBox1.FindString("Peters, Alastair (242242)");
string wholePetersRecord = "";
if (curIndex >= 0)
{
    wholePetersRecord = listBox1.Items[curIndex].ToString();
    listBox1.Items.RemoveAt(curIndex);
}

此代码将查找、存储wholePetersRecord并删除以“Peters, Alastair (242242)”开头的第一项(“Peters, Alastair (242242) Time In : 14:50 Time Out : 14:50 Time In : 14:50”,例如)。

于 2013-10-03T14:45:01.850 回答