使用上下文菜单删除TreeView中的目录 [英] Using Context Menu to Delete Directory in a TreeView

查看:104
本文介绍了使用上下文菜单删除TreeView中的目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目,尝试使用树形视图控件和上下文菜单从父文件夹中删除子文件夹.我已经有了逻辑设置,可以使用给定路径中的所有子文件夹填充treeview控件.现在,我希望能够右键单击treeview节点并删除该文件夹.我将目录信息存储在treeview标记中,因此可以使用它来查找要删除的目录的路径.我还通过删除菜单向项目添加了一个上下文菜单,并将其附加到树视图控件.我遇到的问题是,当我激活contextmenu click事件时,如何查找/传递我所在节点的目录信息?任何帮助将不胜感激.

这是我用于填充树视图的代码:

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:

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

推荐答案

我想我自己想通了.这是我所做的.首先,我从treeview1控件中删除了上下文菜单.然后,我添加了以下mousedown事件,该事件在右键单击该文件夹时显示了上下文菜单:

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

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

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.


这篇关于使用上下文菜单删除TreeView中的目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆