tags:

views:

25

answers:

2

Hello!

I got a List that I add to every time I create a new Employee. The content of this list is then displayed in a Listbox. The problem is that each time I add a new Employee and then call the below method the items in the listbox gets duplicated over and over again. So if I got 3 items in the list the Listbox displays 6. I have checked with employeeList.Count and I am sure about the amount of items in the list.

What is wrong?

public void UpdateEmployeeList()
{
    foreach (Employees values in employeeRegistry.employeerList)
    {
       lstResults.Items.Add(values);
    }
}

Thankful for all help!

+3  A: 

Have you Clear your listbox before adding new employees ?

public void UpdateEmployeeList()
{
    lstResults.Items.Clear();
    foreach (Employees values in employeeRegistry.employeerList)
    {
       lstResults.Items.Add(values);
    }
}
Florian
Yes! I worked perfectly fine! Thanks!
+1  A: 

If you always add all employees, you must first clear the items:

lstResults.Items.Clear();

or better only add the new one...

HCL