Reputation: 315
I have a registration application for a Windows Surface Tablet PC. It works brilliantly but there's one more thing i need to work:
The registration works by either being in an Online State(For a Staff Register), or an Offline State(For Student Event Registrations).
When it's in its Online State, we scan barcodes in to it which holds 6 digits + 'L' , which goes to the database and pulls out the Staff Members name and 6 digit code, then it will list the Staff Members name in order down the listbox.
What i am looking to do is List the time it was first entered into the application, and then when they scan the same code again, instead of removing the first entry it will add another time onto the existing line, such as:
First Scan:
Ryan Gillooly (123456) Time: 11:40
Second Scan:
Ryan Gillooly (123456) Time: 11:40 Time: 13:26
and so on and so forth.
Here is the code :
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;
}
}
Upvotes: 0
Views: 198
Reputation: 2571
Try something like this:
.....
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();
.....
UPDATE
Regarding your extra question in the comments:
You could use string.LastIndexOf to see which word was added last: "In" or "Out". And then you take the other.
LastIndexOf returns the index of the last occurence of the searchstring, or -1 when it's not present.
Out of my mind you would get something like:
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 ";
}
}
This will return the current entry + " in " or " out ". Anbd then you just have to add your extra stuff to it. To call this, replace the Add-sentences with:
listBox1.Items.Add(CreateNewEntry(entry) + " extra stuff");
Upvotes: 1