清单框项目删除 [英] Checklistbox Items Delete

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

问题描述

我尝试过用我编写的代码发布问题,以便获得有关我哪里出错了的反馈,但也许这完全是错误的……我只想问一个问题希望有人发布一个检查列表删除方法的工作版本,该方法可将检查列表框连同计算机上的实际文件一起删除.我还想知道是否有不止一种方法通过使用复选框从您的计算机删除文件?

I have tried before to post my question with the code I have made in order to get some feed back as to where I have gone wrong but maybe its all wrong so......I will simply ask a question in hopes someone will post a working version of a checklistbox delete method that removes the item withing the checklistbox along with the actual file on the computer. I would also like to know if there is more than one method to delete files from your computer by using a checklistbox?

推荐答案

Dim filelist As New List(Of String)
    Private Sub AddFile(ByVal Item As String)
        Dim F As New FileInfo(Item)
        CheckedListBox1.Items.Add(F.Name)
        filelist.Add(F.FullName)
    End Sub

    Private Sub RemoveFile(ByVal Index As Integer)
        File.Delete(filelist.Item(Index))
        CheckedListBox1.Items.RemoveAt(Index)
        filelist.RemoveAt(Index)
    End Sub

    Private Sub FillList(ByVal Path As String)
        Me.CheckedListBox1.Items.Clear()
        filelist = New List(Of String)
        For Each filepath As String In Directory.GetFiles(Path, "*.*", SearchOption.AllDirectories)
            AddFile(filepath)
        Next
    End Sub

    'Change all instances of CheckedListBox1 to your list box

    'To get all the files you need use FillList with the path as your directory tog et files from
    'This should populate the checkboxlist and it's sister list

    'The sister list contains the full file path where as the checkbox list only contains the name of the file

    'To remove checked files:

    Private Sub DeleteSelectedIndices()
        For Each i As Integer In CheckedListBox1.SelectedIndices
            RemoveFile(i)
        Next
    End Sub



确保导入File.IO



Be sure to Import File.IO

Imports File.IO

(在类上方)


好吧,我假设您正在读取文件并将其名称添加到复选框或类似的名称中?

如果是这种情况,那么对于您添加的每个项目,还需要跟踪与之关联的文件的完整路径.

然后,当一个复选框被选中,并选择要删除的,您检索所选择的项目和使用的完整路径:

File.delete(#path#)

其中#path#是文件的完整路径名.
Well I''m assuming you are reading files and adding their name to a checkbox or something like that?

If this is the case, then for each item you add, you also need to keep track of the full path of the file it is associated with.

Then when a checkbox is checked and chosen to be deleted, you retrieve the full path of the selected item and use:

File.delete(#path#)

Where #path# is the full path name of the file.


这篇关于清单框项目删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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