删除代码不起作用 [英] Delete Code Not Working

查看:70
本文介绍了删除代码不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我更改了删除代码,但不知道出了什么问题以及为什么它停止工作.

首先,代码是为一个简单的列表框创建的,但随后决定添加复选框,但删除后发现了相同的搜索结果,并且弹出的消息框显示...文件System.windows.forms.checkedlistbox + checkeditemcollection成功删除了吗?

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?

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

推荐答案

Delete 肯定会失败,因为字符串red不再指向有效的文件名.当然,您确实从消息框中意识到了这一点!请记住,CheckedListBox.CheckedItems是一个集合,因此您可能需要遍历该集合并删除每个项目.
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.


我修改了原始代码.

I modified your original code.

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


我再次重新发布了答案.

I re-post my answer again.

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


这篇关于删除代码不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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