Delete highlighted files from list box AND the actual file

Posted on 2009-05-15
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()


        'This call is required by the Windows Form Designer.


        '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


            End If

        End If


    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.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"




        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




        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"




        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"




        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)"




        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




        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




        Me.AcceptButton = Me.btnList

        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)

        Me.ClientSize = New System.Drawing.Size(632, 483)








        Me.Name = "Form1"

        Me.Text = "Form1"



    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


        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


        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

Question by:cpservice
  • 2
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
How about:
Dim selected As New System.Collections.ArrayList(Me.lstFiles.SelectedItems)

For Each o As Object In selected




Open in new window

LVL 74

Accepted Solution

käµfm³d   👽 earned 125 total points
Comment Utility
Sorry.... transposition error :/

Use this instead:
Dim selected As New System.Collections.ArrayList(Me.lstFiles.SelectedItems)


For Each o As Object In selected




Open in new window


Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

It’s quite interesting for me as I worked with Excel using 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 …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now