简体   繁体   English

如何填充列表 <String> 使用Winforms中的Listbox控件中已存在的值

[英]How do I populate a List<String> with values already present in Listbox control in Winforms

I have a listbox which has a couple of values and is already populated (from user input). 我有一个列表框,它有几个值,已经填充(来自用户输入)。 Later in my program I want to take these values from the listbox and populate them to a List collection. 稍后在我的程序中,我想从列表框中获取这些值并将它们填充到List集合中。

One of the approach is of course to iterate through the items of the listbox and populate the List collection one by one (in a loop) using Add method. 其中一种方法当然是遍历列表框中的项目并使用Add方法逐个填充List集合(在循环中)。

But is there a better more efficient way to do this in one shot meaning all the items of the listbox get copied over to a List collection. 但是有一种更好的方法可以一次性完成此操作,这意味着列表框的所有项目都会被复制到List集合中。

I also looked at the AddRange method but that doesnt seem to help. 我也查看了AddRange方法,但似乎没有帮助。

Any suggestions for this? 有什么建议吗?

It's not necessarily more efficient (in terms of speed/memory), but you can save some typing via LINQ: 它不一定更高效(在速度/内存方面),但你可以通过LINQ节省一些输入:

List<string> items = listBox.Items.Cast<object>()
                            .Select(item => item.ToString()).ToList();

If it must be a List<string> , I think you will have to enumerate the items and add them to a list (since you are concerned about the efficiency of LINQ). 如果它必须是List<string> ,我认为你必须枚举这些项并将它们添加到列表中(因为你担心LINQ的效率)。 If you can use a string[] , you could use the CopyTo method: 如果你可以使用string[] ,你可以使用CopyTo方法:

string[] destination = new string[listBox1.Items.Count];
listBox1.Items.CopyTo(destination, 0);

No there isn't a more efficient way. 没有更有效的方法。

If anything existed it would simply abstract away what you will be doing anyways via an extension method or other means and could have the possibility of making your code less readable. 如果存在任何东西,它将简单地通过扩展方法或其他方式抽象出你将要做的事情,并且可能使你的代码不那么可读。

The CopyTo method does exist via the ObjectCollection on the Items property which could then be LINQ'ed to a List<T> . CopyTo方法确实通过Items属性上的ObjectCollection存在,然后可以将LINQ转换为List<T>

        ListBox lb = new ListBox();
        object[] items = new object[lb.Items.Count];
        lb.Items.CopyTo(items, 0);

The ItemCollection class implements IList , you'll need to cast the items in the collection to String since it's not a generic collection, but you already have a "List collection" of the items. ItemCollection类实现IList ,您需要将集合中的项目转换为String,因为它不是泛型集合,但您已经拥有了项目的“List集合”。

If you really need a copy of the items in collection then no, there isn't going to be a more efficient way. 如果你真的需要一个集合中的项目副本然后没有,那么就不会有更有效的方法。 There are plenty of ways to implement syntatic sugar so that the code is more concise, but ultimately they will all be iterating over the items and creating copies to be added to a new collection. 有很多方法可以实现语法糖使代码更简洁,但最终他们将被遍历项目,并创建要添加到一个新的集合副本。

This seems to be a duplicate of: Most succinct way to convert ListBox.items to a generic list 这似乎是一个重复: 最简洁的方式将ListBox.items转换为通用列表

If you're copying from the listbox, what is populating that list? 如果您从列表框中复制,填充该列表的内容是什么? It might be a better option to go about it that way, versus taking 200k items and moving it over. 这可能是一个更好的选择,而不是采取20万件物品并将其移动。 if you're taking all items in a listbox, populate the List at the same time. 如果您要将所有项目都放在列表框中,请同时填充列表。

On that note.. really? 在那个说明..真的吗? 200k items in a listbox?. 列表框中有200k项? That right there seems a little over the top in a real world application. 在现实世界的应用程序中,这似乎有点过头了。

Foreach(...) loops are very expensive - even when compiled into IL: http://diditwith.net/2006/10/05/PerformanceOfForeachVsListForEach.aspx Foreach(...)循环非常昂贵 - 即使编译成IL: http//diditwith.net/2006/10/05/PerformanceOfForeachVsListForEach.aspx

If you are unable to work with LINQ (your using <= VS 2005), this is one of the easier methods of doing it. 如果您无法使用LINQ(使用<= VS 2005),这是一种更简单的方法。

    string[] items = new string[listBox1.Items.Count];
    listBox1.Items.CopyTo(items, 0);
    List<string> list = new List<string>(items);

@Aaron McIver, your own method is a bit faster in my opinion. @Aaron McIver,我认为你自己的方法有点快。 See this link for some performance testing I have done. 请参阅此链接以了解我所做的一些性能测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM