将 listview1 所选项目复制到 listview2 [英] Copy listview1 selected items to listview2

查看:23
本文介绍了将 listview1 所选项目复制到 listview2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有代码可以成功地将第一行从一个列表视图复制到另一个列表视图.

I have code which successfully copies the first row from one listview to another.

Listview2.Items.Add(Listview1.Items(0).Clone())

但是,它不会复制选定的项目(除非只有一个选定的项目并且它恰好是第一个项目).我错过了什么?

However, it does not copy selected items (unless there is only one selected item and it happens to be the first item). What am I missing?

推荐答案

可以通过ListView1.SelectedItems.

我看到您使用 .Clone() 创建了一个新实体以添加到 ListView2 - ListViewItem.Clone() 函数创建一个对象,所以你需要将它转换为ListViewItem 能够将其添加到另一个 ListView.

I see that you have used .Clone() to create a new entity to add to ListView2 - the ListViewItem.Clone() function creates an object, so you need to cast it to a ListViewItem to be able to add it to another ListView.

迭代所选项目,我们得到...

Iterating over the selected items, we get...

For Each si As ListViewItem In ListView1.SelectedItems
    ListView2.Items.Add(DirectCast(si.Clone(), ListViewItem))
Next

注意您应该使用 Option Strict On确保所有变量类型都匹配——该选项会让 Visual Studio 告诉你哪里出了问题,甚至给出一些关于如何纠正它的有用建议.

N.B. You should use Option Strict On to make sure that all variable types match up - that option will let Visual Studio tell you where something is awry, and even give some useful suggestions as to how to correct it.

这篇关于将 listview1 所选项目复制到 listview2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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