Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Delete highlighted files from list box AND the actual file

Posted on 2009-05-15
2
Medium Priority
?
248 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Screencast - Getting to Know the Pipeline

971 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