Zu welcher Fehlermeldung kommt es denn? Befinden sich in den Ordner auch nur Bilder (also bmp, gif, jpg usw.)?
Hier nocheinmal ein kmpl. Bsp., welches bei mir einwandfrei funktioniert:
Imports System
Imports System.Drawing
Imports System.Windows.Forms
Imports System.Collections
Namespace Tree1
Public Class MainForm
Inherits System.Windows.Forms.Form
Public Sub New()
MyBase.New()
InitializeComponent()
End Sub
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
MyBase.Dispose(disposing)
End Sub
Private components As System.ComponentModel.IContainer
Private Sub InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button
Me.Label1 = New System.Windows.Forms.Label
Me.TreeView1 = New System.Windows.Forms.TreeView
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(312, 352)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(150, 34)
Me.Button1.TabIndex = 0
Me.Button1.Text = "Button1"
'
'Label1
'
Me.Label1.AutoSize = True
Me.Label1.Location = New System.Drawing.Point(13, 352)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(38, 14)
Me.Label1.TabIndex = 1
Me.Label1.Text = "Label1"
'
'TreeView1
'
Me.TreeView1.Location = New System.Drawing.Point(13, 13)
Me.TreeView1.Name = "TreeView1"
Me.TreeView1.Size = New System.Drawing.Size(449, 332)
Me.TreeView1.TabIndex = 2
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(467, 393)
Me.Controls.Add(Me.TreeView1)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.Button1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
Me.PerformLayout()
End Sub
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents TreeView1 As System.Windows.Forms.TreeView
Public Shared Sub Main()
Dim fMainForm As New MainForm
fMainForm.ShowDialog()
End Sub
Dim imgList As ImageList
Dim idx As Integer
'// Dim imgRoot As String = "E:\PCS"
Dim imgRoot As String = "D:\Images"
Private Sub Button1Click(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles Button1.Click
Dim root As New MyTreeNode("My Images", _
MyTreeNode.NodeType.IsRootNode, 0)
Me.imgList.Images.Clear()
idx = 0
imgList.Images.Add(New Bitmap(4, 4))
Me.TreeView1.ImageList = imgList
Me.TreeView1.BeginUpdate()
Me.TreeView1.Nodes.Add(root)
Me.SearchImages(imgRoot, root.Nodes)
Me.TreeView1.EndUpdate()
End Sub
Private Sub FillImageListAndNode(ByVal fileName As String, _
ByRef NodeColl As TreeNodeCollection)
Dim img As Image = Image.FromFile(fileName)
imgList.Images.Add(img)
idx += 1
NodeColl.Add(New MyTreeNode(fileName, MyTreeNode.NodeType.IsImage, _
idx))
End Sub
Private Sub GetImages(ByVal Dir As String, _
ByRef NodeColl As TreeNodeCollection)
For Each f As String In IO.Directory.GetFiles(Dir)
FillImageListAndNode(f, NodeColl)
Next
End Sub
Private Sub SearchImages(ByVal Dir As String, _
ByRef NodeColl As TreeNodeCollection)
Dim tn As New MyTreeNode(Dir, MyTreeNode.NodeType.IsFolder, 0)
GetImages(Dir, tn.Nodes)
NodeColl.Add(tn)
For Each d As String In IO.Directory.GetDirectories(Dir)
SearchImages(d, tn.Nodes)
Next
End Sub
Private Sub MainFormLoad(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles MyBase.Load
imgList = New ImageList
End Sub
Private Sub TreeView1AfterSelect(ByVal sender As System.Object, ByVal e _
As System.Windows.Forms.TreeViewEventArgs)
Dim tn As MyTreeNode = CType(Me.TreeView1.SelectedNode, MyTreeNode)
Me.Label1.Text = "INFO: " + tn.ToString()
End Sub
End Class
Public Class MyTreeNode
Inherits TreeNode
Public TreeNodeType As NodeType
Public Sub New(ByVal Text As String, _
ByVal NodeType As NodeType, _
ByVal ImageIndex As Integer)
Me.Text = Text
Me.TreeNodeType = NodeType
Me.ImageIndex = ImageIndex
Me.SelectedImageIndex = Me.ImageIndex
End Sub
Public Enum NodeType
IsFolder
IsImage
IsRootNode
End Enum
Public Overrides Function ToString() As String
Return String.Format( _
"Text: {0} " + _
"ImageIndex: {1} " + _
"NodeType: {2} ", _
Me.Text, _
Me.ImageIndex, _
Me.TreeNodeType.ToString() _
)
End Function
End Class
End Namespace |