?
Solved

Delete highlighted files from list box AND the actual file

Posted on 2009-05-15
2
Medium Priority
?
251 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 500 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Through the video, you can check the migration process of Outlook PST file to PDF. Kernel for Outlook to PDF tool can convert Outlook emails with all attributes like Subject, To, From, Cc, Bcc and other folders such as Inbox, Outbox, Sent Items, Jun…
Did you know PowerShell can save you time with SaaS platforms? Simply leverage RESTfulAPIs to build your own PowerShell modules. These will kill repetitive tickets and tabs, using the command Invoke-RestMethod. Tune into this webinar to learn how…

585 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