> Protected Overrides Sub OnBeforeExpand(ByVal e As System.Windows.Forms.TreeViewCancelEventArgs) > 'MyBase.OnBeforeExpand(e) > If e.Node.ImageIndex = IconIndex.FolderClosed Then > e.Node.ImageIndex = IconIndex.FolderOpen > e.Node.SelectedImageIndex = IconIndex.FolderOpen > End If > MyBase.SelectedNode = e.Node > If e.Node.Tag = "" Then > 'do nothing > ElseIf e.Node.Nodes(0).Text = DUMMY_NODE Then > 'load folders > e.Node.Nodes.Clear() > Try > 'get the full path > Dim fpath As String = e.Node.Tag > 'check for '\' at the end > If Mid(fpath, Len(fpath) - 1, 1) <> "\" Then > fpath += "\" > End If > 'get the sub directories > Dim fls() As String = IO.Directory.GetDirectories(fpath) > 'create an enumerator > Dim en As IEnumerator = fls.GetEnumerator > Cursor = Cursors.WaitCursor > 'move through enumerator > While en.MoveNext > 'add the folder to the treeview > Dim n As CustomTreeNode = New CustomTreeNode(Replace(Mid(en.Current, Len(fpath) + 1), "\", ""), 0, 0) > n.Tag = en.Current > n.isfolder = True > e.Node.Nodes.Add(n) > 'add a 'dummy' node so it can be expanded > n.Nodes.Add(DUMMY_NODE) > ' Application.DoEvents() > End While > 'set the folder to closed > If e.Node.Nodes.Count = 0 Then > e.Node.ImageIndex = IconIndex.FolderClosed > e.Node.SelectedImageIndex = IconIndex.FolderClosed > End If > 'should we show the files? > > ' Used subclassed treenode to store File Property as True > > If _showfiles Then > > 'get the files based on the search pattern > Dim files() As String = IO.Directory.GetFiles(fpath, _spattern) > Dim enf As IEnumerator = files.GetEnumerator > 'move through enumerator > > While enf.MoveNext > 'get the default Icon > > Dim di As DefaultIcon = New DefaultIcon > di.DefaultIcon = "default.ico" > 'trim off any '\' > Dim fname As String = Replace(Mid(enf.Current, Len(fpath) + 1), "\", "") > Dim ico As Drawing.Icon = di.GetIcon(Mid(fname, InStr(fname, ".") + 1)) > Dim Icons As IList = ilImages.Images > Dim index As Integer > Dim i As Integer > 'check if ico exists in Icons, if it does use that, if not add ico > For i = 0 To Icons.Count - 1 > If Icons(i).Equals(ico) Then > index = i > End If > Next > If i = Icons.Count Then > 'didnt find icon, add it > ilImages.Images.Add(ico) > End If > 'add the file to the treeview with the ico > > Dim fn As CustomTreeNode = New CustomTreeNode(fname, i, i) > fn.FileName = enf.Current > > fn.isfolder = False > e.Node.Nodes.Add(fn) > Application.DoEvents() > > End While > > End If > > Catch ex As Exception > MsgBox(ex.Message) > 'if there is an exception clear the node > e.Node.Nodes.Clear() > If e.Node.ImageIndex = IconIndex.FolderOpen Then > e.Node.ImageIndex = IconIndex.FolderClosed > e.Node.SelectedImageIndex = IconIndex.FolderClosed > End If > 'If e.Node.ImageIndex = IconIndex.CDRom Or IconIndex.FixedDrive Or IconIndex.Floppy Or IconIndex.RamDisk Or IconIndex.Remote Or IconIndex.Removable Then > 'e.Node.ImageIndex = IconIndex.ErrorDisk > 'e.Node.SelectedImageIndex = IconIndex.ErrorDisk > 'End If > Finally > Cursor = Cursors.Default > End Try > End If > End Sub > >