使用List <string>填充组合框

时间:2015-10-21 21:53:28

标签: c# winforms combobox

组合框索引发生变化后,另一个组合框必须填充List<string>个值。我怎么能这样做?

例如:

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

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

班级方法:

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;
}

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

1 个答案:

答案 0 :(得分: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());