C# 如何在列表框中水平列出数据
我有一个Windows Surface Tablet PC的注册应用程序。 它工作得非常出色,但我还需要做一件事: 注册通过在线状态(员工注册)或离线状态(学生活动注册)进行 当它处于联机状态时,我们将条形码扫描到其中,其中包含6位数字+L,然后进入数据库并提取员工姓名和6位代码,然后它将按顺序在列表框中列出员工姓名 我想做的是列出它第一次进入应用程序的时间,然后当他们再次扫描相同的代码时,它将在现有行上添加另一个时间,而不是删除第一个条目,例如: 第一次扫描:C# 如何在列表框中水平列出数据,c#,winforms,datetime,textbox,listbox,C#,Winforms,Datetime,Textbox,Listbox,我有一个Windows Surface Tablet PC的注册应用程序。 它工作得非常出色,但我还需要做一件事: 注册通过在线状态(员工注册)或离线状态(学生活动注册)进行 当它处于联机状态时,我们将条形码扫描到其中,其中包含6位数字+L,然后进入数据库并提取员工姓名和6位代码,然后它将按顺序在列表框中列出员工姓名 我想做的是列出它第一次进入应用程序的时间,然后当他们再次扫描相同的代码时,它将在现有行上添加另一个时间,而不是删除第一个条目,例如: 第一次扫描: Ryan Gillooly(12
Ryan Gillooly(123456)时间:11:40
第二次扫描:
Ryan Gillooly(123456)时间:11:40时间:13:26
等等等等
代码如下:
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();
if (listBox1.Items.Contains(returnValue))
{
for (int n = listBox1.Items.Count - 1; n >= 0; --n)
{
string removelistitem = returnValue.ToString();
if (listBox1.Items[n].ToString().Contains(removelistitem))
{
//listBox1.Items.Add(" " + "\t Time:" + " " + DateTime.Now.ToString("HH.mm"));
listBox1.Items.RemoveAt(n);
}
}
}
else
listBox1.Items.Add(returnValue + "\t Time:" + " " + 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;
}
}
试着这样做:
.....
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(entry + " extra add");
found = true;
break;
}
}
if (!found)
{
listBox1.Items.Add(returnvalue.ToString());
}
textBox1.Clear();
.....
更新
关于评论中的额外问题:
您可以使用查看最后添加的单词:“In”或“Out”。
然后你拿另一个
LastIndexOf返回搜索字符串最后一次出现的索引,如果不存在,则返回-1
从我的脑海中你会得到这样的东西:
private string CreateNewEntry(string current)
{
var indexIn = current.LastIndexOf("in"); // Get the last index of the word "in"
var indexOut = current.LastIndexOf("out"); // Get the last index of the word out
if (indexOut > indexIn)
{
return current + " in "; // if the last "out" comes after the last "in"
}
else
{
// If the last "in" comes after the last "out"
return current + " out ";
}
}
这将返回当前条目+“输入”或“输出”。
然后你只需要把多余的东西加进去。
若要调用此选项,请将“添加句子”替换为:
listBox1.Items.Add(CreateNewEntry(entry) + " extra stuff");
当前输出是什么?像这样@SecretSquirrel例如:第一次扫描:
Ryan Gillooly(123456)时间:11:52
第二次扫描:Ryan Gillooly(123456)时间:11:52
Ryan Gillooly(123456)时间:11:53没有日期时间?你是什么意思@Koen?没问题,你的评论在我的位置上没有显示正确;-)<代码>变量条目=item.ToString()代码>if(entry.Contains(returnValue))
在这一行,它给出了错误“与'string.Contains(string)'匹配的最佳重载方法具有一些无效参数”以及“参数1:无法从'object'转换为'string'”@RyanGillooly在returnValue末尾添加.ToString()
entry.Contains(returnvalue.ToString())
returnvalue是ExecuteScalar
的返回,它始终是对象
您需要将它转换为类型
字符串。ToString()
应该是安全的:)@secretsquirre这太完美了!:)我在listBox1.Items.add(entry++“Time:+DateTime.Now.ToString(“HH:mm”);
中添加了时间,而不是额外的add;因此它显示时间:然后是时间……你知道我如何获得它,以便在所有奇数次扫描(第1次、第3次、第5次)中,它都显示“Time in”,在所有偶数次扫描(第2次、第4次、第6次)中,它都显示“Time out”“?@RyanGillooly我已经用一种可能的方法更新了dmy答案,以计算您的扫描计数。显然,@Steve是正确的,但是如果您想继续使用当前的解决方案,我已经用一种(可能的)解决方案更新了我的答案,希望能让您继续。不需要道歉。我们都去过一次;-)