Click here to Skip to main content
15,901,205 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have altered a delete code but have no idea what went wrong and why it has stopped working.

To start with the code was made for a simple listbox but then decided to add checklistbox but the same search results are found after deleting and the msgbox that pops up says... File System.windows.forms.checkedlistbox+checkeditemcollection successfully deleted?

VB
Private Sub Button3_Click(ByVal sender As System.Object,
                           ByVal e As System.EventArgs) Handles Button3.Click

  red = Replace(CheckedListBox1.CheckedItems.ToString,"Threat Found:","")        
  Try
    System.IO.File.Delete(red)
    If Dir(red) <> "" Then
      MsgBox("Unable to delete" & red, msgboxstyle.critical)
    Else
      MsgBox(String.Format("File {0} successfully deleted", red),
             MsgBoxStyle.Critical)

      CheckedListBox1.Items.Remove (CheckedListBox1.CheckedItems)
    End If
  Catch ex As Exception
  End Try
End Sub
Posted
Updated 31-Oct-10 14:47pm
v2

Well the Delete will certainly fail since the string red is not pointing to a valid file name any more. Surely you did recognize this from the messagebox! Remember that CheckedListBox.CheckedItems is a collection, so you probably need to iterate through that and delete each item.
 
Share this answer
 
v3
Comments
Dale 2012 31-Oct-10 20:58pm    
ok but this message comes even when nothing is checked and I press the delete button. How can I point (red) to a valid file name when I may not know the name to search for?
I modified your original code.

VB
Dim arrList As New ArrayList()
        For Each item As Object In CheckedListBox1.CheckedItems
            arrList.Add(item)
        Next
        For Each item As Object In arrList 'iteration
            red = Replace(item.ToString(), "Threat Found:","")
            Try
                System.IO.File.Delete(red)
                If Dir(red) <> "" Then
                    MsgBox("Unable to delete" & red, MsgBoxStyle.Critical)
                Else
                    MsgBox(String.Format("File {0} successfully deleted", red), MsgBoxStyle.Critical)
                    CheckedListBox1.Items.Remove(item)
                End If
            Catch ex As Exception
                Console.WriteLine("{0}", ex.Message)
            End Try
        Next
 
Share this answer
 
v4
Comments
Dale 2012 31-Oct-10 23:29pm    
Ok I am going to check this out and then get right back to let you know the outcome......

thank you very much in advance for your help
Dale 2012 1-Nov-10 0:15am    
Ok I have tested the code and it corrected the text that is displayed when I click delete but again the file still remains on the computer.... I think something is missing from the code to actually delete the found checklistbox1 item
qontary 1-Nov-10 0:35am    
For more info on delete go here...http://msdn.microsoft.com/en-us/library/system.io.file.delete.aspx
Try to display the Exception thrown inside the Catch block. Eg: MsgBox(ex.Message)
Dale 2012 1-Nov-10 0:38am    
says sorry page not found........ :(
Dale 2012 1-Nov-10 2:05am    
any other ideas to why this still does not delete my file but displays the correct msgbox?
I re-post my answer again.

VB
Dim arrList As New ArrayList()
        For Each item As Object In CheckedListBox1.CheckedItems
            arrList.Add(item)
        Next
        For Each item As Object In arrList 'iteration
            red = Replace(item.ToString(), "Threat Found:","")
            Try
                System.IO.File.Delete(red)
                If Dir(red) <> "" Then
                    MsgBox("Unable to delete" & red, MsgBoxStyle.Critical)
                Else
                    MsgBox(String.Format("File {0} successfully deleted", red), MsgBoxStyle.Critical)
                    CheckedListBox1.Items.Remove(item)
                End If
            Catch ex As Exception
                Console.WriteLine("{0}", ex.Message)
            End Try
        Next
 
Share this answer
 
Comments
Dale 2012 3-Nov-10 2:58am    
once again I have tried your re-posted code but it is the same and has the same effect in my case....nothing happens
Dale 2012 3-Nov-10 2:59am    
what can I give you, so that you can see the error I have made?
qontary 3-Nov-10 3:02am    
Try to change Console.WriteLine("{0}", ex.Message) with MsgBox(ex.Message). See if any message popped up.
Dale 2012 3-Nov-10 3:26am    
Will you be willing to look at my code in full to help determine where I have went wrong?.... I feel that we have different examples of how to generate and populate and am unsure what you have posted will be easy for me to follow. I am 100% sure your way works and dont doubt it for a min but my code does not create a test directory and does not delete directories. It deletes only the file from that directory.....idk maybe thats what your trying to tell me here but I would like it if you could just skim over my code and make some real simple corrections which otherwise would take me far to long to realize on my own.
Dale 2012 3-Nov-10 3:31am    
also I am not creating a text file or directory to look for. My scan uses the names within the text file to populate the checklistbox items which i wish to delete from the checklistbox and from my computer. So far it is only removing the item from the checklistbox but the actual file remains on the computer in the folder I have placed it in
This is how I generate the text files

VB
Private Sub GenerateTxtFiles()
        System.IO.Directory.CreateDirectory("c:\test")
        For i As Integer = 0 To 99
            Console.WriteLine("Creating: " & "c:\test\filetest" & i & ".txt")
            Dim fs As System.IO.FileStream = System.IO.File.Create("c:\test\filetest" & i & ".txt")
            fs.Close()
        Next
    End Sub



This is how I populate the CheckedBoxList

VB
Private Sub populateList()
        Dim tempDirectory As String = "c:\test"
        If (Directory.Exists(tempDirectory)) Then
            Try
                Dim Files() As String = Directory.GetFiles(tempDirectory)
                'Clear out directory
                For Each Filename As String In Files
                    CheckedListBox1.Items.Add(String.Format("Threat Found:{0}", Filename), True)
                Next
            Catch ex As Exception
            End Try
        End If
    End Sub
 
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