Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 组合框和通用列表_C# - Fatal编程技术网

C# 组合框和通用列表

C# 组合框和通用列表,c#,C#,我有一张单子 List<string> strArraylist = new List<string>(); List strArraylist=new List(); 我想向它添加组合框的值。更新:我刚刚意识到您可能问了与我下面提供的相反的问题:如何将组合框中的项目添加到列表中。如果是这样的话,你可以这样做: List<string> strList = new List<string>(); strList.AddRange(cbx.It

我有一张单子

 List<string> strArraylist = new List<string>();
List strArraylist=new List();

我想向它添加组合框的值。

更新:我刚刚意识到您可能问了与我下面提供的相反的问题:如何将组合框中的项目添加到
列表中。如果是这样的话,你可以这样做:

List<string> strList = new List<string>();
strList.AddRange(cbx.Items.Cast<object>().Select(x => x.ToString()));
请注意,您也可以将此方法设置为非泛型,因为
组合框.Items
属性属于非泛型类型(您可以将任何
对象
添加到
)。在这种情况下,
Populate
方法将接受普通的
IEnumerable
,而不是
IEnumerable

public static class ControlHelper
{
    public static void Populate<T>(this ComboBox comboBox, IEnumerable<T> items)
    {
        try
        {
            comboBox.BeginUpdate();
            foreach (T item in items)
            {
                comboBox.Items.Add(item);
            }
        }
        finally
        {
            comboBox.EndUpdate();
        }
    }
}
List<string> strList = new List<string> { "abc", "def", "ghi" };
cbx.Populate(strList);