?
Solved

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

Posted on 2011-03-06
8
Medium Priority
?
397 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 1000 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

Industry Leaders: 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 …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses
Course of the Month10 days, 10 hours left to enroll

765 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