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:
Private Sub TreeView1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TreeView1.MouseDown
If e.Button = Windows.Forms.MouseButtons.Right Then
Dim node_here As TreeNode = TreeView1.GetNodeAt(e.X, _
e.Y)
TreeView1.SelectedNode = node_here
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:
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.