0

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;
            }
        }
Ryan Gillooly
  • 315
  • 1
  • 5
  • 20

1 Answers1

1

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");
Koen
  • 2,501
  • 1
  • 32
  • 43
  • `var entry = item.ToString();` `if (entry.Contains(returnValue))` At this line it is giving error "The best overloaded method match for 'string.Contains(string)' has some invalid arguments" and also "Argument 1: cannot convert from 'object' to 'string'" – Ryan Gillooly Sep 27 '13 at 11:35
  • 2
    @RyanGillooly add the `.ToString()` at the end of the returnvalue. `entry.Contains(returnvalue.ToString()))` the returnvalue is the return of an `ExecuteScalar` which is always an `object` you need to cast that to the `type` you are returning in the case of a `string` `.ToString()` should be safe :) – Squirrel5853 Sep 27 '13 at 11:56
  • @SecretSquirrel that's perfect! :) Instead of extra add I have added in `listBox1.Items.Add(entry + " "+"Time: +DateTime.Now.ToString("HH:mm"));` so it shows Time : then the time... do you know how i could get it so that on all odd number of scans (1st, 3rd , 5th), it says "Time In" and on all even scans (2nd, 4th , 6th) it says "Time out"? – Ryan Gillooly Sep 27 '13 at 13:18
  • @SecretSquirrel Good catch. Hadn't thought about that. Edited my answer. – Koen Sep 27 '13 at 13:29
  • 1
    @RyanGillooly I've update dmy answer with a possible way to figure out your scan count. – Koen Sep 27 '13 at 13:30
  • Thank You @Koen! Im struggling with the syntax of the `string.LastIndexOF`, any ideas ? Also apologies, new to C# – Ryan Gillooly Sep 27 '13 at 14:15
  • Just an idea but why not bind to a custom class and instead of ADD/REMOVE strings, you could edit the property you are bound to in the list. This makes updates easier/cleaner. [Binding list of objects](http://stackoverflow.com/questions/2675067/binding-listbox-to-listobject) – Steve Sep 27 '13 at 21:04
  • 1
    Obviously, @Steve is correct, but if you want to stay with the current solution, I've updated my answer with a (possible) solution which will hopefully get you going. And no apologies needed. We've all been there one time ;-) – Koen Sep 30 '13 at 06:07