Click here to Skip to main content
15,888,113 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a project where I am trying to delete sub folders from a parent folder using a tree view control and a context menu. I already have the logic setup that populates the treeview control with all the sub folders from a given path. Now I would like to be able to right click on the treeview node and delete that folder. I have the directoryInfo stored in the treeview tag so I can use that to find the path of the directory I want to delete. I've also added a contextmenu to the project with a delete menu and attached that to the treeview control. The problem I'm having is how do I find/pass the directory info for the node I'm sitting on when I activate the contextmenu click event? Any help would be appreciated.

Here is the code I have for populating the treeview:

VB
Private Sub PopulateTreeView()
        Dim rootNode As TreeNode

        Dim info As New DirectoryInfo("C:\Temp")
        If info.Exists Then
            rootNode = New TreeNode(info.Name)
            rootNode.Tag = info
            GetDirectories(info.GetDirectories(), rootNode)
            TreeView1.Nodes.Add(rootNode)
        End If

    End Sub

    Private Sub GetDirectories(ByVal subDirs() As DirectoryInfo, _
        ByVal nodeToAddTo As TreeNode)

        Dim aNode As TreeNode
        Dim subSubDirs() As DirectoryInfo
        Dim subDir As DirectoryInfo
        For Each subDir In subDirs
            aNode = New TreeNode(subDir.Name, 0, 0)
            aNode.Tag = subDir
            aNode.ImageKey = "folder"
            subSubDirs = subDir.GetDirectories()
            If subSubDirs.Length <> 0 Then
                GetDirectories(subSubDirs, aNode)
            End If
            nodeToAddTo.Nodes.Add(aNode)
        Next subDir

    End Sub
Posted

1 solution

I think I figured it out myself. Here is what I did. First I removed the context menu from the treeview1 control. I then added the following mousedown event which displayed the context menu when the folder was right clicked:

VB
Private Sub TreeView1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TreeView1.MouseDown

       ' See if this is the right mouse button.
       If e.Button = Windows.Forms.MouseButtons.Right Then
           ' Select this node.
           Dim node_here As TreeNode = TreeView1.GetNodeAt(e.X, _
               e.Y)
           TreeView1.SelectedNode = node_here

           'If found, display the context menu
           If Not IsNothing(TreeView1) Then
               ContextMenuStrip1.Show(TreeView1, New Point(e.X, e.Y))

           End If
       End If

   End Sub



I then added the following code to the context menu click event which deleted the selected folder:

VB
Private Sub DeleteToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DeleteToolStripMenuItem.Click


        Dim DeleteNode As TreeNode
        Dim SubDirInfo As DirectoryInfo
        Dim FolderPath As String


        DeleteNode = TreeView1.SelectedNode
        SubDirInfo = DeleteNode.Tag
        FolderPath = SubDirInfo.FullName


        If My.Computer.FileSystem.DirectoryExists(FolderPath) = True Then

            Dim DeleteResult As MsgBoxResult

            DeleteResult = MsgBox("Do you really want to delete the folder " & FolderPath & "?", MsgBoxStyle.YesNo, "Confirmation needed")

            If DeleteResult = MsgBoxResult.Yes Then
                My.Computer.FileSystem.DeleteDirectory(FolderPath, FileIO.DeleteDirectoryOption.DeleteAllContents)
                TreeView1.Nodes.Remove(DeleteNode)

            End If

        End If

    End Sub



Hopefully this will be useful for someone in the future.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900