Solved

Treeview scan folder from TXT files and display numer total in node (brackets) VB.NET 2003

Posted on 2011-03-06
8
392 Views
Last Modified: 2012-05-11
Hi Experts,

I've spent a couple of hours trawling the net searching through TREEVIEW mind fields of code for my answer.

I have a winform with a treeview and a Listbox (blank) & button.

If button is clicked, treeview will scan a folder for TXT files and display total in brackets...

Treeview1
+ FOLDER A (10) ' files in that folder
+ FOLDER B (?) ' files in that folder

If Sub-Node is clicked, then loads name contents into Listbox.

I've requested something similar before with a listbox, but this seems a little trickier.

Thanks,
Roberto
Public Class Form1
    Inherits System.Windows.Forms.Form

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadFiles()
    End Sub

    Private Sub LoadFiles()
        Dim sourceFolder As String = "c:\"
        ListBox1.BeginUpdate()
        ListBox1.Items.Clear()
        ListBox1.Items.AddRange((New System.IO.DirectoryInfo(sourceFolder)).GetFiles("*.txt"))
        ListBox1.EndUpdate()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Not (ListBox1.SelectedItem Is Nothing) Then
            TextBox1.Text = CType(ListBox1.SelectedItem, System.IO.FileInfo).OpenText.ReadToEnd
        End If
    End Sub

End Class

Open in new window

0
Comment
Question by:RobertoFreemano
  • 5
  • 2
8 Comments
 
LVL 24

Expert Comment

by:jimyX
ID: 35045953
Before I start, let me confirm, do you want Delphi code?
0
 

Author Comment

by:RobertoFreemano
ID: 35045972
VB.NET 2003
0
 

Author Comment

by:RobertoFreemano
ID: 35110282
Can anyone help?
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 35173992

Here, try this
It however seems to be one click behind, must be a VB bug
Imports System.IO

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        LoadDirectories()
    End Sub

    Private Sub LoadDirectories()
        Dim sourceFolder As String = "c:\"
        Dim dirs = From folder In Directory.EnumerateDirectories(sourceFolder)
        For Each folder In dirs
            Try
                TreeView1.Nodes.Add(folder.ToString() & "(" & (New DirectoryInfo(folder.ToString()).EnumerateFiles("*.txt").Count.ToString()) & ")")
            Catch UAEx As UnauthorizedAccessException

            End Try
        Next
    End Sub

    Private Sub LoadFiles(ByVal folder As String)
        Dim formatfolder As String
        formatfolder = folder.Substring(0, folder.IndexOf("("))

        Try
            ListBox1.BeginUpdate()
            ListBox1.Items.Clear()
            ListBox1.Items.AddRange((New System.IO.DirectoryInfo(formatfolder)).GetFiles("*.txt"))
            ListBox1.EndUpdate()
        Catch UAEx As UnauthorizedAccessException

        End Try
    End Sub

    Private Sub TreeView1_NodeMouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeNodeMouseClickEventArgs) Handles TreeView1.NodeMouseClick
        If Not TreeView1.SelectedNode Is Nothing Then
            LoadFiles(TreeView1.SelectedNode.Text.ToString())
        End If

    End Sub
End Class

Open in new window

0
 

Author Comment

by:RobertoFreemano
ID: 35216334
Sorry ewangoya,

I will test this tonight ;)
0
 

Author Comment

by:RobertoFreemano
ID: 35235217
Hi,

There seems to be a problem with:
"Dim dirs = From folder In Directory.EnumerateDirectories(sourceFolder)
        For Each folder In dirs"
0
 
LVL 32

Accepted Solution

by:
Ephraim Wangoya earned 250 total points
ID: 35237486

What kind of error do you get.
I might have to use a different method since I'm using VS2010, I'm not really sure whether VS2003 supports it and that may be the problem
0
 

Author Closing Comment

by:RobertoFreemano
ID: 35386601
I'm sure you're right :(
0

Featured Post

Technology Partners: 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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

696 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