Solved

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

Posted on 2011-03-06
8
388 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 32

Expert Comment

by:ewangoya
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:
ewangoya 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

861 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