在删除用列表视图选择的多项项时出现问题 [英] issues deleting multipe items selected with a listview

查看:52
本文介绍了在删除用列表视图选择的多项项时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用选择的多个项目获取heam_name,然后从文件中删除该项目.我关注了几个站点,但我无法使它正常工作.

我正在阅读以下网站: http://msdn.microsoft.com /en-us/library/system.windows.forms.listview.selecteditems.aspx [

i have been trying to use the multiple items selected to take the heam_name and then delete that item from the file. I have followed several sites and i just cant get this to work.

I was reading the following site: http://msdn.microsoft.com/en-us/library/system.windows.forms.listview.selecteditems.aspx[^]

I dont think i doing this correctly and if someone could help that would be great.
this code below is all under one button click.

Dim strComputer As String = TextBox1.Text

If strComputer = "" Then
    strComputer = "127.0.0.1"
End If

Dim DEL_LIST As ListView.SelectedListViewItemCollection = Me.ListView1.SelectedItems'Dim item As ListViewItem
Dim heap_num = ListView1.SelectedItems(0).Text 'heap number
Dim Process = GetObject("winmgmts://./root/novadigm:NVD_Agent")
Dim method = Process.Methods_("DeleteInstance")
Dim inParameters = method.inParameters.SpawnInstance_()
inParameters.Path = "c:\temp\aservice.edm"
inParameters.Index = heap_num

For Each heap_num In DEL_LIST
    Process.ExecMethod_("DeleteInstance", inParameters)
Next

ListView1.Items.Clear()
Button1.PerformClick()

推荐答案

我相信问题是您正在删除项目,然后从列表中将其删除,因此您不再拥有有效的每个循环.

将所有项目移动到新列表,然后使用该列表删除.使用System.Linq ToList() 方法可以很容易地做到这一点.您还可以手动移动到新列表.

I beleive the problem is that you are deleting items which then deletes themfrom the list, so you no longer have a valid for each loop.

Move all the items to a new list, and then delete using that list. This can be done easily using the System.Linq ToList() method. You can also manually move to a new list.

Dim DEL_LIST = Me.ListView1.SelectedItems.ToList()


'=======================================================================================
'The below code will create an array for the items that need to be deleted from Zservice
'=======================================================================================

' Delcaring the selected items as heap_num
Dim Zheap_num = CAE_Listview.SelectedItems(0).SubItems(1).Text ' heap name
Dim ZPATH As String = "\\" & strComputer & "\C


\ Progra〜2 \ Hewlett-Packard \ HPCA \ Agent \ Lib \ SYSTEM \ RADIA \ SOFTSRVC \ ZSERVICE \" Dim Zlist As 新建 ArrayList ' 对于每个选定的项目,将项目编号添加到数组列表中 对于 每个 sITEM 中 Zlist.Add(sITEM) 下一步 ' 将数组列表从012345反转为543210 Zlist.Reverse() 对于 每个 DelZ Zlist ' 将堆名称添加到del_heap的文本框中,该名称将在完成后显示 ' 删除所选项目 Del_Heap.Del_TextBox.Text + = DelZ.text& vbCrLf ' 如果该文件夹存在,则将其删除;如果该文件夹存在,它将继续运行 如果 System.IO.Directory.Exists(ZPATH& DelZ.text)然后 Directory.Delete(ZPATH& DelZ.text,) 结束 如果 下一个
\Progra~2\Hewlett-Packard\HPCA\Agent\Lib\SYSTEM\RADIA\SOFTSRVC\ZSERVICE\" Dim Zlist As New ArrayList 'For each selected item add the item number to an array list For Each sITEM In Zheap_num Zlist.Add(sITEM) Next 'reverse the arraylist from 012345 to 543210 Zlist.Reverse() For Each DelZ In Zlist 'adds the heap name to the textbox for del_heap that will display once its finished 'deleting the items selected Del_Heap.Del_TextBox.Text += DelZ.text & vbCrLf 'if the folder exist it will delete it if it dosent exist it will keep going If System.IO.Directory.Exists(ZPATH & DelZ.text) Then Directory.Delete(ZPATH & DelZ.text, True) End If Next


这篇关于在删除用列表视图选择的多项项时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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