在组合框显示listView.Items [英] Show listView.Items in a ComboBox
本文介绍了在组合框显示listView.Items的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我怎么能显示Form 2上listView.Items在Form1上放置一个组合框,我想从选定项目使用的所有数据(子项)。
我怎么能这样做?
解决方案
Form1.comboBox.Items.AddRange(
Form2.listView.Items.Cast< ListViewItem的>()选择(A => a.Text));
这将简单的 ListViewItem的
将文本复制到组合框。
有关所有子项,它变得更复杂一点:
Form1.comboBox.Items.AddRange(
Form2.listView.Items.Cast< ListViewItem的>()选择(。
A =>的string.join(,,a.SubItems
.Cast< System.Windows.Forms.ListViewItem.ListViewSubItem>()
。选择(S => s.Text)。.ToArray()))ToArray的());
此使用LINQ to获取文本值从每个项目的子项的数组中一起加入,
,并增加了每个连接字符串列表到组合框
How can I show the listView.Items on Form2 in a ComboBox on Form1, and I want to use all data (subitems) from the selected Item.
How can I do that?
解决方案
Form1.comboBox.Items.AddRange(
Form2.listView.Items.Cast<ListViewItem>().Select(a => a.Text));
This will simply copy the text of the ListViewItem
to the combo box.
For all subitems, it gets a bit more complex:
Form1.comboBox.Items.AddRange(
Form2.listView.Items.Cast<ListViewItem>().Select(
a => string.Join(", ", a.SubItems
.Cast<System.Windows.Forms.ListViewItem.ListViewSubItem>()
.Select(s => s.Text).ToArray())).ToArray());
This uses LINQ to get an array of text values from the subitems of each item to be joined together with ", "
, and adds each concatenated string list to the ComboBox
这篇关于在组合框显示listView.Items的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文