-1

I am writing a program that keeps track of employee check in and check out times. There are two list boxes, one for checked in employees and the other for checked out employees. My question is how do I remove the name from the check in list box and populate the check out list box when a employee inputs his/her user id in a text box?

I am not sure where to begin. I know how to populate a list box from a data reader but not from a text box and I don't know how to remove a specific item from a list box. Any suggestions on how to use list boxes in this manner?

DeanOC
  • 7,142
  • 6
  • 42
  • 56
Evanark
  • 191
  • 2
  • 4
  • 16

2 Answers2

2

ListBox has an Items property that you can add and remove elements from.

listBox.Items.Add("new item");

listBox.Items.Remove("old item");

If you are binding your ListBox directly to a DataReader you can't directly manipulate the list of Items - instead you would modify the underlying datasource and let the binding refresh the UI.

To search for an items in a list

if (listBox.Items.Contains(searchvalue)) {
  listBox.Items.Remove(searchValue);
}
Jason
  • 86,222
  • 15
  • 131
  • 146
  • Thanks, one more question If I have a list of items in a listbox say for example a list of serial numbers. How can I enter a serial number in a text box and search listbox for the number entered and remove it when I click a button? The application I am writing populates a list box with serial numbers for a given work order. When the user scans a serial number, the application will search the list box with serial numbers and remove it from the loaded list and populate another listbox as a way of verifying that the number was scanned. Is that a difficult task? – Evanark Dec 30 '14 at 20:43
  • Thanks, about the data reader, the list box is not bound, I use a command with a select statement, I use a data reader to populate the listbox while (dr.Read(); it will add items to the listbox from the select statement results, will this method of searching and removing the data still work? – Evanark Dec 30 '14 at 21:08
1

Please refer to following link for adding item into listbox:

http://msdn.microsoft.com/en-us/library/aa288403%28v=vs.71%29.aspx

You can use any desired event and then add text from textbox to listbox using following statement.

listBox1.Items.Add(((TextBox)sender).Text);

For removing item from listbox refer to the following:

C# removing items from listbox

Community
  • 1
  • 1