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