Add icon to Listbox items and remove .TXT extension. VB.NET 2003 (pref)

Posted on 2011-04-23
Last Modified: 2012-05-11
Hi Experts,

I have code from an old project which I've since lost and wish to resurrect. The code works fine, I wish to expand on it a little.

FileSystemWatcher detects if a text file is saved in a directory and lists in a Listbox.

I've been unable to get the icon code to work:
Dim img as Image
img = image.FromFile("c:\images\image.bmp")

Open in new window


I've had code from the legendary Idle_Mind to remove TXT entries:
Dim di As New DirectoryInfo(sourceFolder)
        For Each fi As FileInfo In di.GetFiles("*.txt")

Open in new window

but i cannot get ...GetFileNameWithoutExtension(fi.Name)... line to work anymore in VB.NET 2003.

Original code thanks to graye
Imports System.IO
Public Class Form1

    Dim fsw As FileSystemWatcher

    Private Sub bnQuit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bnQuit.Click
    End Sub

    Private Sub bnDirBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bnDirBrowse.Click
        If FolderBrowserDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            tbDirectory.Text = FolderBrowserDialog1.SelectedPath
        End If
    End Sub

    Private Sub bnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bnStart.Click
        fsw = New System.IO.FileSystemWatcher

        ' set a file filter (like "*.txt")
        fsw.Filter = tbFilter.Text

        ' this is just a demo, so let's not include subdirectories
        fsw.IncludeSubdirectories = False

        ' let's set up the watcher to catch a list of changes
        fsw.NotifyFilter = (NotifyFilters.Size Or NotifyFilters.FileName Or NotifyFilters.DirectoryName)

        ' the path of the directory to watch
        fsw.Path = tbDirectory.Text

        ' Add event handlers.
        AddHandler fsw.Changed, AddressOf fwsChanged
        AddHandler fsw.Created, AddressOf fwsChanged
        AddHandler fsw.Deleted, AddressOf fwsChanged
        AddHandler fsw.Renamed, AddressOf fwsRenamed

        ' Let's go!
        fsw.EnableRaisingEvents = True

        tbMessages.Text &= "Started: " & Now & vbCrLf
    End Sub

    Private Sub bnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bnStop.Click
        ' discontinue watching
        fsw.EnableRaisingEvents = False
        tbMessages.Text &= "Stopped: " & Now & vbCrLf
    End Sub

    ' The "event handler" for the Changed, Created, and Deleted events
    ' This much match an "implied" delegate
    ' Delegate Sub FileSystemEventHandler(sender as Object, e as System.IO.FileSystemEventArgs)
    Private Sub fwsChanged(ByVal source As Object, ByVal e As FileSystemEventArgs)
        ' write to the message window
        tbMessages.Text &= "File '" & e.Name & "' " & [Enum].GetName(GetType(WatcherChangeTypes), e.ChangeType) & vbCrLf
    End Sub

    ' Event Handler for the Renamed event
    ' Delegate Sub RenamedEventHandler(sender as Object, e as System.IO.RenamedEventArgs)
    Private Sub fwsRenamed(ByVal source As Object, ByVal e As RenamedEventArgs)
        tbMessages.Text &= "File '" & e.Name & "' " & [Enum].GetName(GetType(WatcherChangeTypes), e.ChangeType) & vbCrLf
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' this is required because the event handlers are running in a different
        ' thread as the Form.
        [Form].CheckForIllegalCrossThreadCalls = False
    End Sub
End Class

Open in new window

Question by:RobertoFreemano
    LVL 83

    Expert Comment

    >but i cannot get ...GetFileNameWithoutExtension(fi.Name)... line to work anymore in VB.NET 2003.

    Do you get an error?

    Author Comment

    Hi CodeCruiser,

    Thanks for your help... I've managed to resolve the "GetFileNameWithoutExtension(fi.Name)" issue.

    Now all's I need to do is add icons to the list-box.
    LVL 83

    Accepted Solution

    I dont think Listbox supports icons out of the box. Check this custom listbox which does

    Author Comment

    Is that c+ code?
    LVL 83

    Expert Comment

    That's C# code but it should be in a DLL and you can use it as any other control.

    Author Comment


    I don't supposed you know how to make an Alias (i.e. detect & replace listbox item name with alternative)

    e.g. code that doesn't work :(

    If Listbox1.text = "Billy" Then
    End If
    LVL 83

    Expert Comment

    I dont think that's a built in function

    Author Closing Comment

    Thanks CC

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Join & Write a Comment

    Suggested Solutions

    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…
    If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    729 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

    16 Experts available now in Live!

    Get 1:1 Help Now