Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating a Directory Tree 1

Status
Not open for further replies.

Viruland

Programmer
Dec 6, 2000
61
BE
I'm trying to create a directory tree.
I only want to show the name of the directory and not the full pathname. Can someone tell me how to do so.
Below is my code


Imports System.IO

Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents TreeView1 As System.Windows.Forms.TreeView
Friend WithEvents Button1 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.TreeView1 = New System.Windows.Forms.TreeView
Me.Button1 = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'TreeView1
'
Me.TreeView1.ImageIndex = -1
Me.TreeView1.Location = New System.Drawing.Point(8, 8)
Me.TreeView1.Name = &quot;TreeView1&quot;
Me.TreeView1.Nodes.AddRange(New System.Windows.Forms.TreeNode() {New System.Windows.Forms.TreeNode(&quot;My Computer&quot;)})
Me.TreeView1.SelectedImageIndex = -1
Me.TreeView1.Size = New System.Drawing.Size(576, 184)
Me.TreeView1.TabIndex = 0
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(608, 16)
Me.Button1.Name = &quot;Button1&quot;
Me.Button1.Size = New System.Drawing.Size(80, 40)
Me.Button1.TabIndex = 1
Me.Button1.Text = &quot;Button1&quot;
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(704, 213)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.TreeView1)
Me.Name = &quot;Form1&quot;
Me.Text = &quot;Form1&quot;
Me.ResumeLayout(False)

End Sub

#End Region

Public blnload As Boolean = True
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

ShowAllDrives()

End Sub

Sub ShowAllDrives()
Dim drives() As String
Dim cnode As TreeNode
cnode = TreeView1.Nodes(0)
drives = Directory.GetLogicalDrives()
Dim aDrive As String

Dim di As DirectoryInfo
For Each aDrive In drives
di = New DirectoryInfo(aDrive)
cnode.Nodes.Add(di.Name)
Next

End Sub

Private Sub TreeView1_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect

If TreeView1.SelectedNode Is Nothing Then Exit Sub
If TreeView1.SelectedNode Is TreeView1.Nodes(0) Then Exit Sub

Dim folders() As String
Dim cnode As TreeNode
cnode = TreeView1.SelectedNode

folders = Directory.GetDirectories(TreeView1.SelectedNode.Text)
Dim f As String
For Each f In folders
cnode.Nodes.Add(f)
Next

TreeView1.SelectedNode.Expand()


End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim myFolder As String

myFolder = TreeView1.SelectedNode.Text
MsgBox(myFolder)
End Sub
End Class


Live fast, die young and leave a beautiful corpse behind.
 
You can just trip up until the last &quot;\&quot;

Code:
For Each f In folders
    If f.LastIndexOf(&quot;\&quot;) <> -1 Then
        f = f.Substring(f.LastIndexOf(&quot;\&quot;) + 1)
    End If

    cnode.Nodes.Add(f)
Next
 
Rosenk,

I've already try that but it doesn't work that way.
From the moment I click on the second node the program crashes. Err.Message = illegal path

Live fast, die young and leave a beautiful corpse behind.
 
Forgive me. I didn't notice exactly what you were doing. Specifically, I didn't notice that you were populating on [tt]AfterSelect[/tt]

Change it to:
Code:
folders = Directory.GetDirectories(CStr(TreeView1.SelectedNode.Tag))

For Each f In folders
    Dim newName as String

    If f.LastIndexOf(&quot;\&quot;) <> -1 Then
        newName = f.Substring(f.LastIndexOf(&quot;\&quot;) + 1)
    End If

    Dim newNode as TreeNode(newName)
    newNode.Tag = f
    cnode.Nodes.Add(newNode)
Next

You'll also need to change your initial population with drives to create the node with the [tt].Tag[/tt] property set.
 
One more correction.

Code:
Dim newNode as TreeNode(newName)
should be
Code:
[COLOR=red]Dim newNode as New TreeNode(newName)[/color]
 
Rosenk,

I've still got a problem but now with the loading. I've got the following error Object reference not set.

I don't know what I'm doing wrong.


This is the code that I've edited with your suggestions

Sub ShowAllDrives()
Dim folders() As String
Dim f As String

Dim cnode As TreeNode
cnode = TreeView1.Nodes(0)
Try
folders = Directory.GetDirectories(CStr(TreeView1.SelectedNode.Tag))

For Each f In folders
Dim newName As String

If f.LastIndexOf(&quot;\&quot;) <> -1 Then
newName = f.Substring(f.LastIndexOf(&quot;\&quot;) + 1)
End If

Dim newNode As New TreeNode(newName)
newNode.Tag = f
cnode.Nodes.Add(newNode)
Next
Catch ex As Exception
MsgBox(Err.Description)
End Try


End Sub

Private Sub TreeView1_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect

If TreeView1.SelectedNode Is Nothing Then Exit Sub
If TreeView1.SelectedNode Is TreeView1.Nodes(0) Then Exit Sub


Dim folders() As String
Dim f As String
Dim cnode As TreeNode
cnode = TreeView1.SelectedNode

folders = Directory.GetDirectories(CStr(TreeView1.SelectedNode.Tag))

For Each f In folders
Dim newName As String

If f.LastIndexOf(&quot;\&quot;) <> -1 Then
newName = f.Substring(f.LastIndexOf(&quot;\&quot;) + 1)
End If

Dim newNode As New TreeNode(newName)
newNode.Tag = f
cnode.Nodes.Add(newNode)
Next

End Sub

Live fast, die young and leave a beautiful corpse behind.
 
Rosenk,

I found the solution and I'm wanne thanx you for your help.

Live fast, die young and leave a beautiful corpse behind.
 
Here's a way to show the plus sign and handle clicking the plus sign or the nodes text:
Code:
    Sub ShowAllDrives()
        Dim drives() As String
        Dim cnode As TreeNode
        cnode = TreeView1.Nodes(0)
        drives = Directory.GetLogicalDrives()
        Dim aDrive As String

        Dim di As DirectoryInfo
        For Each aDrive In drives
            di = New DirectoryInfo(aDrive)
            Dim nd As TreeNode = New TreeNode(di.Name)
            nd.Tag = di.Name
            nd.Nodes.Add(&quot;&quot;)
            cnode.Nodes.Add(nd)
        Next

    End Sub

    Private Sub TreeView1_AfterSelect(ByVal sender As System.Object, _
                                      ByVal e As System.Windows.Forms.TreeViewEventArgs) _
                                      Handles TreeView1.AfterSelect, TreeView1.AfterExpand
        If e.Node Is Nothing Then Exit Sub
        If e.Node Is TreeView1.Nodes(0) Then Exit Sub
        If Not e.Node.FirstNode Is Nothing Then
            If e.Node.FirstNode.Text Is &quot;&quot; Then
                e.Node.FirstNode.Remove()

                Dim folders() As String
                Dim cnode As TreeNode
                cnode = e.Node
                Try
                    folders = Directory.GetDirectories(e.Node.Tag)
                    Dim f As String
                    For Each f In folders
                        Dim nd As TreeNode = New TreeNode()
                        nd.Text = System.IO.Path.GetFileName(f)
                        nd.Tag = f
                        Dim iDirs As Integer = System.IO.Directory.GetDirectories(f).Length
                        If iDirs > 0 Then nd.Nodes.Add(&quot;&quot;)
                        cnode.Nodes.Add(nd)
                    Next
                    e.Node.Expand()
                Catch exc As Exception
                    MsgBox(exc.ToString)
                End Try
            End If
        End If

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, _
                              ByVal e As System.EventArgs) _
                              Handles Button1.Click
        Dim myFolder As String
        myFolder = TreeView1.SelectedNode.Tag
        MsgBox(myFolder)
    End Sub

-Stephen Paszt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top