?
Solved

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

Posted on 2011-04-23
8
Medium Priority
?
576 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

0
Comment
Question by:RobertoFreemano
  • 4
  • 4
8 Comments
 
LVL 83

Expert Comment

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

Do you get an error?
0
 

Author Comment

by:RobertoFreemano
ID: 35452601
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.
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 2000 total points
ID: 35452607
I dont think Listbox supports icons out of the box. Check this custom listbox which does

http://www.codeproject.com/KB/combobox/glistbox.aspx
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:RobertoFreemano
ID: 35452640
:(
Is that c+ code?
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 35452651
That's C# code but it should be in a DLL and you can use it as any other control.
0
 

Author Comment

by:RobertoFreemano
ID: 35452734
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
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 35452739
I dont think that's a built in function
0
 

Author Closing Comment

by:RobertoFreemano
ID: 35497646
Thanks CC
0

Featured Post

Independent Software Vendors: 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

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Screencast - Getting to Know the Pipeline
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

569 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