如果我在listview1中选择的行已经在listview2中,我如何验证我的listview2?我不明白listView1.Items.Contains()和ContainKey()
这是我在listview1中获取所选行的代码:
private void listView1_DoubleClick(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count > 0)
{
foreach (ListViewItem item in listView1.SelectedItems)
{
//how do i condition this selected item? to prevent inserting same items in the listview2
listView2.Items.Add((ListViewItem)item.Clone());//cloning the item to insert in listview2
}
}
}
如何将listview1中的选定行与项目listview2进行比较?
答案 0 :(得分:1)
如果您的listView1
项是字符串列表,则可以使用
private void listView1_DoubleClick(object sender, EventArgs e)
{
// You don't need to check: if (listView1.SelectedItems.Count > 0)
foreach (ListViewItem item in listView1.SelectedItems)
{
bool isExist = false;
foreach (ListViewItem item2 in listView2.Items)
{
if (item2.Text == item.Text) // Compare Text
//if (item2.ImageKey == item.ImageKey) // Compare Key. If you don't use key then ignore this line
{
isExist = true;
break;
}
}
if (!isExist) listView2.Items.Add((ListViewItem)item.Clone());
}
}
答案 1 :(得分:0)
private void listView1_DoubleClick(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count > 0)
{
foreach (ListViewItem item in listView1.SelectedItems)
{
if(!listView2.Items.Contains(item)) //verify text and value. if don't contain add
listView2.Items.Add((ListViewItem)item.Clone());
}
}
}