Solved

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

Posted on 2011-03-06
8
387 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

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 Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
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…

821 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