使用List <string>填充组合框

Lev*_*han 1 c# combobox winforms

组合框索引更改后,必须使用List<string>值填充另一个组合框.我怎么能这样做?

例如:

表格(这是我现在的方式,但不正确):

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

分类方法:

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;
}
Run Code Online (Sandbox Code Playgroud)

希望这种方式可以理解.如果我忘记了什么,请告诉我!

Mik*_*keH 5

我认为这AddRange是你正在寻找的方法

//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()); 
Run Code Online (Sandbox Code Playgroud)