Solved

Delete highlighted files from list box AND the actual file

Posted on 2009-05-15
2
241 Views
Last Modified: 2012-05-07
I want to add a buttont hat lets me delete highlighted files from the list box and their hard drive location
Imports System.IO
 
Public Class Form1
    Inherits System.Windows.Forms.Form
 
#Region " Windows Form Designer generated code "
 
    Public Sub New()
        MyBase.New()
 
        'This call is required by the Windows Form Designer.
        InitializeComponent()
 
        'Add any initialization after the InitializeComponent() call
 
    End Sub
 
    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub
 
    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer
 
    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents btnList As System.Windows.Forms.Button
    Friend WithEvents txtDir As System.Windows.Forms.TextBox
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents cboPattern As System.Windows.Forms.ComboBox
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents lstFiles As System.Windows.Forms.ListBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.btnList = New System.Windows.Forms.Button
        Me.txtDir = New System.Windows.Forms.TextBox
        Me.Label1 = New System.Windows.Forms.Label
        Me.Label2 = New System.Windows.Forms.Label
        Me.cboPattern = New System.Windows.Forms.ComboBox
        Me.lstFiles = New System.Windows.Forms.ListBox
        Me.Button1 = New System.Windows.Forms.Button
        Me.SuspendLayout()
        '
        'btnList
        '
        Me.btnList.Anchor = System.Windows.Forms.AnchorStyles.Top
        Me.btnList.Location = New System.Drawing.Point(290, 64)
        Me.btnList.Name = "btnList"
        Me.btnList.Size = New System.Drawing.Size(48, 24)
        Me.btnList.TabIndex = 2
        Me.btnList.Text = "List"
        '
        'txtDir
        '
        Me.txtDir.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.txtDir.Location = New System.Drawing.Point(48, 8)
        Me.txtDir.Name = "txtDir"
        Me.txtDir.Size = New System.Drawing.Size(582, 20)
        Me.txtDir.TabIndex = 0
        '
        'Label1
        '
        Me.Label1.AutoSize = True
        Me.Label1.Location = New System.Drawing.Point(0, 8)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(49, 13)
        Me.Label1.TabIndex = 2
        Me.Label1.Text = "Directory"
        '
        'Label2
        '
        Me.Label2.AutoSize = True
        Me.Label2.Location = New System.Drawing.Point(0, 32)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(41, 13)
        Me.Label2.TabIndex = 3
        Me.Label2.Text = "Pattern"
        '
        'cboPattern
        '
        Me.cboPattern.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.cboPattern.Items.AddRange(New Object() {"*.vb (Code Files)", "*.resx (Resource Files)", "*.sln (Solution Files)", "*.* (All Files)"})
        Me.cboPattern.Location = New System.Drawing.Point(48, 32)
        Me.cboPattern.Name = "cboPattern"
        Me.cboPattern.Size = New System.Drawing.Size(582, 21)
        Me.cboPattern.TabIndex = 1
        Me.cboPattern.Text = "*.* (All Files)"
        '
        'lstFiles
        '
        Me.lstFiles.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                    Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.lstFiles.Location = New System.Drawing.Point(0, 96)
        Me.lstFiles.Name = "lstFiles"
        Me.lstFiles.Size = New System.Drawing.Size(630, 316)
        Me.lstFiles.TabIndex = 3
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(3, 438)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(627, 23)
        Me.Button1.TabIndex = 4
        Me.Button1.Text = "Button1"
        Me.Button1.UseVisualStyleBackColor = True
        '
        'Form1
        '
        Me.AcceptButton = Me.btnList
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(632, 483)
        Me.Controls.Add(Me.Button1)
        Me.Controls.Add(Me.lstFiles)
        Me.Controls.Add(Me.cboPattern)
        Me.Controls.Add(Me.Label2)
        Me.Controls.Add(Me.txtDir)
        Me.Controls.Add(Me.btnList)
        Me.Controls.Add(Me.Label1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)
        Me.PerformLayout()
 
    End Sub
 
#End Region
 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim startup_path As String = Application.StartupPath
        txtDir.Text = startup_path.Substring(0, startup_path.LastIndexOf("\"))
        txtDir.Select(0, 0)
    End Sub
 
    Private Sub btnList_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnList.Click
        ' Get the pattern without stuff in parens.
        Dim pattern As String = cboPattern.Text
        If pattern.IndexOf("(") >= 0 Then
            pattern = pattern.Substring(0, pattern.IndexOf("("))
        End If
 
        lstFiles.Items.Clear()
        Dim dir_info As New DirectoryInfo(txtDir.Text)
 
        ListFiles(lstFiles, pattern, dir_info)
    End Sub
 
    ' Add the files in this directory's subtree to the ListBox.
    Private Sub ListFiles(ByVal lst As ListBox, ByVal pattern As String, ByVal dir_info As DirectoryInfo)
        ' Get the files in this directory.
        Dim fs_infos() As FileInfo = dir_info.GetFiles(pattern)
        For Each fs_info As FileInfo In fs_infos
            lstFiles.Items.Add(fs_info.FullName)
        Next fs_info
        fs_infos = Nothing
 
        ' Search subdirectories.
        Dim subdirs() As DirectoryInfo = dir_info.GetDirectories()
        For Each subdir As DirectoryInfo In subdirs
            ListFiles(lst, pattern, subdir)
        Next subdir
    End Sub
End Class

Open in new window

0
Comment
Question by:cpservice
  • 2
2 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 24399952
How about:
Dim selected As New System.Collections.ArrayList(Me.lstFiles.SelectedItems)
 
For Each o As Object In selected
    System.IO.File.Delete(f)
    Me.lstFiles.Items.Remove(o)
Next

Open in new window

0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 125 total points
ID: 24399962
Sorry.... transposition error :/

Use this instead:
Dim selected As New System.Collections.ArrayList(Me.lstFiles.SelectedItems)
 
For Each o As Object In selected
    System.IO.File.Delete(o.ToString())
    Me.lstFiles.Items.Remove(o)
Next

Open in new window

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question