Click here to Skip to main content
15,910,981 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

VB
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()
Posted

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()
 
Share this answer
 
Comments
Zachary.shupp 8-Mar-12 13:26pm    
when i try to ise this item i get the following error:'List' is not a member of 'System.Windows.Forms.ListView.SelectedListViewItemCollection'. could you give me an example of how i would ad this to my code that i can test?
Clifford Nelson 8-Mar-12 14:13pm    
Are you having problems with the ToList, that may mean that you have not referenced Linq. Besides that not sure what the issue is since do not have anything called List. You can always just do a foreach loop and add to the DEL_LIST.
Zachary.shupp 8-Mar-12 14:17pm    
i have referenced the linq. Could you give me an example of the foreach loop and how i would add it to the del list.
Clifford Nelson 8-Mar-12 14:24pm    
I am not a VB programmer, so here goes:

Dim DEL_LIST = New List<object>()
For Each (var x in Me.ListView1.SelectedItems)
DEL_LIST.Add (x)
Next

For Each heap_num In DEL_LIST
Process.ExecMethod_("DeleteInstance", inParameters)
Next
Zachary.shupp 8-Mar-12 14:34pm    
if you work with c# thats fine
VB
'=======================================================================================
'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 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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900