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