Solved

Spurious tree nodes appearing

Posted on 2009-07-03
4
252 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
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 shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

809 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