Click to See Complete Forum and Search --> : TreeView errors


zhshqzyc
March 4th, 2008, 01:19 PM
I follow an example on the book but got errors.
Thanks for help

Error 1 Value of type 'System.Windows.Forms.TreeNode' cannot be converted to 'System.Windows.Forms.TreeView'.
Error 2 Name 'directoryArray' is not declared. FrmListBox


Imports System.Windows.Forms
Imports System.IO
Public Class Form1

Friend WithEvents trdDirectory As TreeView
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
trdDirectory.Nodes.Add("C:")
PopulateTreeView("C:\", trdDirectory.Nodes(0)) ' error 1 here
End Sub

Private Sub PopulateTreeView(ByVal directoryValue As String, ByVal parentNode As TreeView)
Try
Dim treDirectory As String() = Directory.GetDirectories(directoryValue)

If directoryArray.Length <> 0 Then 'error 2 here
Dim currentDirectory As String
For Each currentDirectory In directoryArray
Dim myNode As TreeNode = New TreeNode(currentDirectory)
parentNode.Nodes.Add(myNode)
PopulateTreeView(currentDirectory, myNode)

Next
End If
Catch unauthorized As UnauthorizedAccessException
parentNode.Nodes.Add("Access Denied")
End Try
End Sub
End Class

jshultz
March 4th, 2008, 01:44 PM
Well, you are getting error 1 because you are passing a treenode parameter to a sub that is expecting a treeview. Pass trdDirectory, not trdDirectory.Nodes(0).

You are getting error 2 because, well obviously, directoryArray is not declared, just like the error is telling you. I don't know what directoryArray is supposed to be from your current code, it is only reference in the one line.