如何在Winforms中将Listbox值连接成字符串 [英] How to concat Listbox values into string in Winforms

查看:70
本文介绍了如何在Winforms中将Listbox值连接成字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我想知道如何将ListBox值连接成winforms中的字符串或数组。你能指导我或发送任何代码片段吗?



提前致谢!



:重复的问题如何在Winforms中将Listbox值连接成字符串 [ ^ ]< - 已经删除了 Manfred R. Bihy [ ^ ]

解决方案

最好的方法是使用StringBulder:

 StringBuilder sb =  new  StringBuilder(); 
foreach object o in myListBox.SelectedItems)
{
sb.AppendLine(o.ToString());
}
string s = sb.ToString();


OriginalGriff 给出的解决方案1很好。



或者,使用 string.Join 方法和 LINQ 可以按如下方式使用

  //  此对象用作Type参数,因为项目类型未在中给出
// 问题。否则,如果listBox中的项目类型为Person
// 则.OfType< Person> ;可以使用

// 作为字符串的项目数组
string [] itemsAsStrings =( from p in listBox1.Items.OfType< object>()
select p.ToString())。ToArray();

// 项目数组
object [] arrayOfItems = listBox1.Items.OfType< object>()。ToArray();

// 列表框所有项目的连接
< span class =code-keyword> string allItems = string .Join( < span class =code-string>,,listBox1.Items.OfType< object>());

// 列表框所选联结的连接
< span class =code-keyword> string selectedItems = string .Join( < span class =code-string>,,listBox1.SelectedItems.OfType< object>());


  //   linq查询以下所有选定项目 
string required = listBox1.SelectedItems.Cast< string>()。Aggregate((a,b)=> a + b);

// linq查询以下查询所有不同的选定项目
string required = listBox1.SelectedItems.Cast< string>()。Distinct()
.Aggregate((a,b)=> a + b );

// linq查询以下所有选中项目之间的字符串

string required = listBox1.SelectedItems.Cast< string>()。Distinct()
.Aggregate((a, b)=> a + + b);


Hello,
I want to know how to concat ListBox values into a string or an array in winforms. Can you guide me or send any code snippets?

Thanks in advance!

[EDIT] : Duplicate question How to concat Listbox values into string in Winforms[^] <-- Duplicate already deleted by Manfred R. Bihy[^]

解决方案

Best way is to use a StringBulder:

StringBuilder sb = new StringBuilder();
foreach (object o in myListBox.SelectedItems)
    {
    sb.AppendLine(o.ToString());
    }
string s = sb.ToString();


The solution 1 given by OriginalGriff is good.

Alternatively, using string.Join method and LINQ can be used as follows

//Here object is used as Type argument as the type of items is not given in
//the question. Otherwise, if the type of items in listBox is Person
//then .OfType<Person> can be used

//Array of Items as strings
string[] itemsAsStrings = (from p in listBox1.Items.OfType<object>()
                           select p.ToString()).ToArray();

//Array of Items
object[] arrayOfItems = listBox1.Items.OfType<object>().ToArray();

//Concatenation of all items of list box
string allItems = string.Join(", ",listBox1.Items.OfType<object>());

//Concatenation of selected tiems of list box
string selectedItems= string.Join(", ",listBox1.SelectedItems.OfType<object>());


//Below linq query concat all the selected item
 string required =listBox1.SelectedItems.Cast<string>().Aggregate((a, b)=>a+b);

//Below linq query concat all the distinct selected item
 string required =listBox1.SelectedItems.Cast<string>().Distinct()
                  .Aggregate((a, b)=>a+b);

//Below linq query concat all the selected item with a string in between
 
string required =listBox1.SelectedItems.Cast<string>().Distinct()
                  .Aggregate((a, b)=>a+","+b);


这篇关于如何在Winforms中将Listbox值连接成字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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