Levialhan
Levialhan

Reputation: 97

Populating a combobox with List<string>

After a combobox index changes, another combobox has to be populated with List<string> values. How am I able to do this?

For example:

Form (This is the way I'm having it now, incorrect though):

private void cbSelectEditFunction_SelectedIndexChanged(object sender, EventArgs e)
{
    cbSelectEditName.Items.Add(emp.FindEmployeeinFunction(cbSelectEditFunction.Text));    
}

Class method:

public List<string> FindEmployeeinFunction(string aFunction)
{
    List<string> EmployeeListFunction = new List<string>();

    foreach (Employee TempEmployee in EmployeeList)
    {
        if(TempEmployee.Function == aFunction)
        {
            EmployeeListFunction.Add(TempEmployee.Username);
        }
    }
    return EmployeeListFunction;
}

Hope it's understandable this way. Let me know if I've forgotten something!

Upvotes: 3

Views: 11633

Answers (1)

MikeH
MikeH

Reputation: 4395

I think AddRange is the method you're looking for

//Assuming you don't want to continually add new items use Clear()
cbSelectEditName.Items.Clear();

//Use AddRange to add the list.  ToArray() is used to convert List<> to string[]
cbSelectEditName.Items.AddRange(emp.FindEmployeeinFunction(cbSelectEditFunction.Text).ToArray()); 

Upvotes: 7

Related Questions