Link to home
Start Free TrialLog in
Avatar of RobertoFreemano
RobertoFreemanoFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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

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")
Listbox1.items.add(img)

Open in new window


Also,

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")
            ListBox1.Items.Add(Path.GetFileNameWithoutExtension(fi.Name))
        Next

Open in new window


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

Thanks,
Roberto
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
        Me.Close()
    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
        Application.DoEvents()
    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
        Application.DoEvents()
    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

Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

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

Do you get an error?
Avatar of RobertoFreemano

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
:(
Is that c+ code?
That's C# code but it should be in a DLL and you can use it as any other control.
CodeCruiser,

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
Listbox1.Items.Replacewith("William")
End If
I dont think that's a built in function
Thanks CC