We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

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

RobertoFreemano
on
Medium Priority
683 Views
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")
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

Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

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

Do you get an error?

Author

Commented:
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.
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview

Author

Commented:
:(
Is that c+ code?
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

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

Author

Commented:
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
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
I dont think that's a built in function

Author

Commented:
Thanks CC
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.