RobertoFreemano
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:
Also,
I've had code from the legendary Idle_Mind to remove TXT entries:
but i cannot get ...GetFileNameWithoutExten sion(fi.Na me)... line to work anymore in VB.NET 2003.
Thanks,
Roberto
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)
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
but i cannot get ...GetFileNameWithoutExten
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
ASKER
Hi CodeCruiser,
Thanks for your help... I've managed to resolve the "GetFileNameWithoutExtensio n(fi.Name) " issue.
Now all's I need to do is add icons to the list-box.
Thanks for your help... I've managed to resolve the "GetFileNameWithoutExtensio
Now all's I need to do is add icons to the list-box.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
:(
Is that c+ code?
Is that c+ code?
That's C# code but it should be in a DLL and you can use it as any other control.
ASKER
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 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
End If
I dont think that's a built in function
ASKER
Thanks CC
Do you get an error?