Listview Explorer - 导航目录按钮,如Windows资源管理器?

时间:2016-11-20 11:01:27

标签: vb.net listview

我使用Listview创建自定义资源管理器,我需要包含用于列出目录的导航按钮,例如在Windows资源管理器中。所以,简而言之,我需要它像这样工作:

  1. 双击"文件夹"在Listview中,你进入该目录内的水平(我得到了这个工作)

  2. 执行此操作时,向后导航的按钮应变为活动状态,您可以返回上一个目录位置。如果返回,那么也应该激活向上按钮 - 返回双击"文件夹"时的目录。

  3. 我希望这能用于与用户点击一样多的上/下文件夹路径 - 这就是Windows资源管理器的工作方式。

  4. 我尝试通过声明路径的公共变量(由ListviewItem标记存储),但是你可以只向上/向下移动到目录一次。我希望它可以像用户一样多次工作,如果目录中有文件夹,那么当然。

    这是我最近的尝试(使用字符串列表):

    Dim Navigation as List(of String)
    
     Private Sub ListView1_DoubleClick(sender As Object, e As EventArgs) Handles ListView1.DoubleClick
    
            If ListView1.SelectedItems(0).ImageKey = "folder" Then
    
            Dim ItemDirectory As IO.DirectoryInfo
                ItemDirectory = New IO.DirectoryInfo(ListView1.SelectedItems(0).Tag.ToString)
                'Pass this path to List of strings too 
                Navigation = ItemDirectory
    
                '... I'm adding each subdirectories and files in Listview  here
           Else '... If not folder then just open file by double-clicking
                Process.Start(ListView1.SelectedItems(0).Tag)
           End If
           BtnNavigateBackwards.Enabled=True 'Enable navigating backwards 
     End Sub
    
     Private Sub BtnNavigateForward_Click(sender As Object, e As EventArgs) Handles BtnNavigateForward.Click
    
            If Navigation.Count <> 0 Then
    
            'Get directory that is one level down from where you are in Listview - I was allready in It by clicking on "folder" item
                Dim ItemDirectory As IO.DirectoryInfo
                ItemDirectory = New IO.DirectoryInfo(Navigation. ???? go to path 1 level down each click until last list of string ???)
                '... Adding each subdirectories and files for directory one level down
                '... Also checking for "folder" item in directory - If they are not then disable this button
             End If
    
     End Sub
    
     Private Sub BtnNavigateBackwards_Click(sender As Object, e As EventArgs) Handles BtnNavigateBackwards.Click
            'Get directory one level up from where I am in Listview
            Dim ItemDirectory As IO.DirectoryInfo
            ItemDirectory = New IO.DirectoryInfo(IO.Path.GetDirectoryName(Navigation. ?????? how to go only 1 level up each click ???))
    
            '... Adding each subdirectory and files to Listview
    
     End Sub
    

    这不是我的完整代码,因为它很长,我知道有很多东西缺失。但最重要的问题是 - 我是否可以使用这样的字符串列表向上/向下导航?

1 个答案:

答案 0 :(得分:0)

问题解决了。您可以通过执行一些项目标记循环来在资源管理器中导航。但首先我们必须将项目标签存储在字符串列表中。因此,在我的情况下,当我双击它们时,我存储了“文件夹”项目标签,以便将一个目录放下:

 Private Navigation As New List(Of String) 'We store each item tag in here and use It for navigating in Listview directories
'Handle double click on folders - when clicked you go into folder, as in Explorer
Private Sub ListView1_DoubleClick(sender As Object, e As EventArgs) Handles ListView1.DoubleClick
        If sender Is Nothing OrElse Not TypeOf sender Is System.Windows.Forms.ListView Then Exit Sub
        With CType(sender, System.Windows.Forms.ListView)
            If sender.SelectedItems(0).ImageKey = "folder" Then
                'Get directory of folder
                Dim ItemDirectory As IO.DirectoryInfo
                ItemDirectory = New IO.DirectoryInfo(sender.SelectedItems(0).Tag.ToString)
                'Then we also add folder item to our variable
                Navigation.Add(sender.SelectedItems(0).Tag.ToString)
                sender.Items.Clear()
                'Then we add each subdirectories and files using GetDirectories  and GetFiles 
...
...
...

End Sub
Private Sub Btn_NavigateForward_Click(sender As Object, e As EventArgs) Handles Btn_NavigateForward.Click
        Dim i As Integer = 0
        'When we navigate forward Listview has items, so we need to loop their tags. If loop finds same string that we stored in  Navigation variable than this means we were allready in this directory
        For i = ListView1.Items.Count - 1 To 0 Step -1
            For Each path As String In Navigation
                If ListView1.Items(i).Tag.ToString = path Then
                    Navigation_Forward(ListView1, path)
                    Exit Sub
                End If
            Next
        Next
End Sub

 Private Sub Btn_NavigateBack_Click(sender As Object, e As EventArgs) Handles Btn_NavigateBack.Click
'When navigating back we have two options – either no items in a directory or there are some !
'In both cases we just do correct loops. For no folder we loop backwards in Navigation variable strings
        If ListView1.Items.Count = 0 Then
            For j As Integer = Navigation.Count - 1 To 0 Step -1
                Dim s As String = Navigation.Item(j)
                Navigation_Back(ListView1, s)
                Exit Sub
            Next j
'If items are in Listview then we first do a loop through items, and a backward loop in Navigation variable – looking for a »folder« tag match
        ElseIf ListView1.Items.Count > 0 Then
            Dim i As Integer = 0
            For i = 0 To ListView1.Items.Count - 1
                For j As Integer = Navigation.Count - 1 To 0 Step -1
                    Dim s As String = Navigation.Item(j)
                    If s = IO.Path.GetDirectoryName(ListView1.Items(i).Tag.ToString) Then
                        Navigation_Back(ListView1, s)
                        Exit Sub
                    End If
                Next
            Next
'And here is sub for navigating forward – I'm posting only this one, because Navigation_Back is basically same, you just have to define Directory_item like this: »Directory_Item = New IO.DirectoryInfo(IO.Path.GetDirectoryName(path))« - instead of what you see in this sub here
    Public Sub Navigation_Forward(ByVal sender As Object, ByVal Path As String)

        Dim LView As System.Windows.Forms.ListView = sender

        With CType(sender, System.Windows.Forms.ListView)

            Dim Directory_Item = New IO.DirectoryInfo(Path)

            LView.Items.Clear()

            For Each My_directory As IO.DirectoryInfo In Directory_Item.GetDirectories

                Dim Lvi As New ListViewItem

                Lvi.Tag = My_directory.FullName
                Lvi.ImageKey = CacheShellIcon(My_Directory.FullName)
                Lvi.Text = My_Directory.Name
                Lvi.Items.Add(Directory_Item)

            Next

            'Do same loop for files if desired
        End With

     End Sub

代码适用于仅导航Listview内容的按钮。但是我认为如果需要,它也可以适用于Treeview节点。