Winform:如何从列表框中收集所有字符串值 [英] Winform: How to Collect All String Values from a ListBox

查看:33
本文介绍了Winform:如何从列表框中收集所有字符串值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够收集 Winform ListBox 中的所有字符串值.目前我的代码循环遍历 ListBox 并获取值,但它将所有值附加到一个长字符串中:

I'd like to be able to collect all the string values in a Winform ListBox. At the moment my code loops through the ListBox and gets the values, but it's appending all values together in one long string:

private string GetFormNumberValues()
{
    string formNumbers = "";
    foreach (string item in this.lbFormNumbers.Items)
    {
        formNumbers += item.ToString();
    }
    return formNumbers;
}

如何收集每个单独的字符串值以供以后使用?谢谢.

How can I collect each individual string value to use for later? Thanks.

推荐答案

试试这个:

private string[] GetFormNumberValues()
{
    List<string> strings = new List<string>();
    foreach (string item in this.lbFormNumbers.Items)
    {
        strings.Add(item.ToString());
    }
    return strings.ToArray();
}

(根据您的需要,您可以通过返回 List 而不是数组来简化此操作...)

(Depending on your needs, you could simplify this by returning a List rather than an array...)

这篇关于Winform:如何从列表框中收集所有字符串值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆