将项目从一个列表框移动到另一个C-C#
我编写的程序是一个寄存器,当扫描条形码时,它将进入数据库,取出人员姓名和身份证号,并列出他们扫描的时间。然后,当他们第二次扫描时,会添加一个“超时”部分,例如:将项目从一个列表框移动到另一个C-C#,c#,windows,winforms,text,listbox,C#,Windows,Winforms,Text,Listbox,我编写的程序是一个寄存器,当扫描条形码时,它将进入数据库,取出人员姓名和身份证号,并列出他们扫描的时间。然后,当他们第二次扫描时,会添加一个“超时”部分,例如: Peters,Alastair(242242)输入时间:14:50输出时间:14:50输入时间:14:50 我希望它能做的是,当我第一次扫描时,你会看到: 阿拉斯泰尔彼得斯(242242)时间:14:50 然后,当您第二次扫描时,它将添加超时:,然后删除整行并将其存储在第二个列表框中,这样当此人不在时它不会显示 然后,当该人员扫描进来(
Peters,Alastair(242242)输入时间:14:50输出时间:14:50输入时间:14:50
我希望它能做的是,当我第一次扫描时,你会看到:
阿拉斯泰尔彼得斯(242242)时间:14:50
然后,当您第二次扫描时,它将添加超时:
,然后删除整行并将其存储在第二个列表框中,这样当此人不在时它不会显示
然后,当该人员扫描进来(第三次)时,它将重新列出上一行,但新的时间将进入
任何建议或想法都会非常有用
这是我的密码:
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)Time in:14:50 Time Out:14:50 Time in:14:50)。我很乐意提供帮助。你能解释一下什么东西不适合你吗?谢谢@Giovani。没有什么是“不”的工作,我只是需要建议或帮助,试图找出上述问题。当我扫描我自己时,在偶数次扫描时,我希望它将整行移动到另一个列表框中以隐藏它,并添加“超时:”,然后当我扫描回奇数次扫描时,我希望它将我重新输入到第一个列表框中,并添加“超时”