Solved

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

Posted on 2011-03-06
8
379 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
 
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
This video discusses moving either the default database or any database to a new volume.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

705 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now