[英]Move items from one listbox to anotherc - C#
我写的程序是一个寄存器,当扫描条形码时,它将进入数据库并取出人员名称和ID号,并列出他们扫描时的“ Time In:”。然后当他们第二次扫描时,将添加一个“超时:”部分,例如:
Peters, Alastair (242242) Time In : 14:50 Time Out : 14:50 Time In : 14:50
我要寻找的是当我第一次扫描您时会看到:
Peters, Alastair (242242) Time In : 14:50
然后当您第二次扫描时,它将添加Time Out :
,然后删除整行并将其存储在第二个列表框中,因此当此人不在时不会显示。
然后,当此人扫描(第3次)时,它将重新列出前一行,但带有新的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;
}
}
我了解起始位(例如“ 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)进入时间:14:50超时时间:14:50进入时间:14:50” , 例如)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.