[英]Show listView.Items in a ComboBox
如何在Form1的ComboBox中顯示Form2上的listView.Items,並且我想使用所選Item中的所有數據(子項)。
我怎樣才能做到這一點?
Form1.comboBox.Items.AddRange(
Form2.listView.Items.Cast<ListViewItem>().Select(a => a.Text));
這將簡單地將ListViewItem
的文本復制到組合框中。
對於所有子項目,它都變得更加復雜:
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());
它使用LINQ從每個要與", "
連接的子項中獲取文本值的數組,並將每個串聯的字符串列表添加到ComboBox
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.