Solved

Spurious tree nodes appearing

Posted on 2009-07-03
4
250 Views
Last Modified: 2013-11-27
I have an app in which I have built a tree by searching for files of type exe on the computer.

For some reason, after displaying the tree the way I want, it then adds, spurious nodes (c:\ d:\ e:\) several times at the bottom of the tree.

I have 3 drives on my computer and all three correctly appear in the tree.

How can I get rid of these spurious nodes?
0
Comment
Question by:jindalee
  • 2
  • 2
4 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 24772238
should be something related with your tree-filling algorithm. May be helpful if you post the code.
0
 
LVL 1

Author Comment

by:jindalee
ID: 24775012
As requested, here is the code I am using. I have also attached a jpg showing the error.

The code populates a DataGridView and a TreeView. The TreeView ends up with a bunch of nodes as shown in the attached image.
Dim theFonts As String() = New String(5) {"*.abf", "*.fnt", "*.fon", "*.shx", "*.ttc", "*.ttf"}
 

    Private Sub PerformAuditToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PerformAuditToolStripMenuItem.Click

        ProcessIsRunning = True

        Dim fileType As String = ""

        FilesCounted = 0

        For iLoop As Integer = 0 To theFonts.GetLength(0) - 1

            fileType = theFonts(iLoop).ToString()

            dlgProgress.Text = My.Resources.searchFontFile.ToString() & fileType.ToString()

            dlgProgress.tbFileName.Text = ""

            dlgProgress.tbFilesCounted.Text = FilesCounted.ToString()

            dlgProgress.Show()

            Application.DoEvents()

            GetFiles(fileType.ToString(), dgvFonts)

        Next

        ProcessIsRunning = False

    End Sub
 

    Private Sub AuditToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AuditToolStripButton1.Click

        Dim addsender As Object

        addsender = AuditToolStripButton1

        PerformAuditToolStripMenuItem_Click(AuditToolStripButton1, e)

    End Sub
 

    Private Sub GetFiles(ByVal fileMatch As String, ByVal dg As DataGridView)

        Dim path As String, i As Int32 = 0

        DisableMenus()

        'dlgProgress.Text = My.Resources.prgAudit.ToString()
 

        ' grow the tree

        Me.TreeView1.BeginUpdate()

        For i = 0 To theDrives.Length() - 1

            Try

                path = theDrives(i, 0).ToString()

                If path <> "Nothing" Then

                    Me.TreeView1.Nodes.Add(path.ToString())

                    AddDirectoriesNFiles(Me.TreeView1.Nodes(i), path.ToString(), fileMatch.ToString(), dgvFonts)
 

                End If

            Catch ex As Exception
 

            End Try

        Next

        Me.TreeView1.EndUpdate()

        dgvFonts.Sort(FileName, ComponentModel.ListSortDirection.Ascending)

        dlgProgress.Close()

        EnableMenus()

        Application.DoEvents()

    End Sub
 

    Private Sub AddDirectoriesNFiles(ByVal rootNode As TreeNode, ByVal dir As String, ByVal fileMatch As String, ByVal dg As DataGridView)
 

        Dim node As TreeNode

        Dim theFile As String = ""
 

        'Loop through each folder in the directory

        For Each di As IO.DirectoryInfo In New IO.DirectoryInfo(dir).GetDirectories()
 

            'Add a new node

            node = New TreeNode(di.Name)

            rootNode.Nodes.Add(node)

            Application.DoEvents()
 

            'If there are subfolders, then loop through them

            Try

                AddDirectoriesNFiles(node, di.FullName, fileMatch, dgvFonts)

                dlgProgress.ProgressBar1.PerformStep()

                dlgProgress.tbFileName.Text = di.FullName.ToString()

                If dlgProgress.ProgressBar1.Value = 100 Then

                    dlgProgress.ProgressBar1.Value = 1

                End If

                Application.DoEvents()

            Catch
 

            End Try

        Next
 

        For Each curFile As String In IO.Directory.GetFiles(dir, fileMatch)

            dlgProgress.tbFileName.Text = curFile.ToString()

            theFile = curFile.ToString()

            curFile = curFile.Substring(curFile.LastIndexOf("\") + 1)

            dlgProgress.ProgressBar1.PerformStep()

            If dlgProgress.ProgressBar1.Value = 100 Then

                dlgProgress.ProgressBar1.Value = 1

            End If

            curFile = curFile.Substring(curFile.LastIndexOf("\") + 1)

            rootNode.Nodes.Add(New TreeNode(curFile))

            dg.Rows.Add()

            dg.Rows(FilesCounted).Cells(0).Value = curFile.ToString()

            dg.Rows(FilesCounted).Cells(1).Value = dir.ToString()

            FilesCounted = FilesCounted + 1

            dlgProgress.tbFilesCounted.Text = FilesCounted.ToString()

            Application.DoEvents()

        Next curFile

    End Sub

Open in new window

tree-error.JPG
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 500 total points
ID: 24775072
I haven't analyzed all yet, but line 35 looks suspicious:
If path <> "Nothing" Then
maybe it is:
If path <> Nothing Then

The problem is that you scan all the folder system 6 times (one for each font extension). But every interactions creates a new set of drives.

Also, you can scan for all font file extensions:

For i = 0 To theDrives.Length() - 1
Try
path = theDrives(i, 0).ToString()
   If path <> Nothing Then
      Me.TreeView1.Nodes.Add(path.ToString())
     For Each fileMatch as String in theFonts
        AddDirectoriesNFiles(Me.TreeView1.Nodes(i), path.ToString(), fileMatch.ToString(), dgvFonts)
     End For
  End If
Catch ex As Exception

End Try
Next
0
 
LVL 1

Author Closing Comment

by:jindalee
ID: 31599537
For each ... fixed it for me since I didn't need to scan the entire folder systenm for each font type. Many thanks
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

759 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

20 Experts available now in Live!

Get 1:1 Help Now