65.9K
CodeProject 正在变化。 阅读更多。
Home

文件选择对话框/树形视图

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.19/5 (17投票s)

2007 年 5 月 8 日

CPOL

2分钟阅读

viewsIcon

56302

downloadIcon

1136

一个树形视图对话框。

Screenshot - Article.gif

引言

我们需要解决的问题是:在 VB.NET 2005 中,程序员不允许继承文件对话框,并且有两种类型:打开和保存。

我曾经做过一个加密程序,它像一个个人数据的保险箱一样工作。 在我的程序中,有“导入”和“导出”按钮,我认为使用带有“保存”、“打开”按钮标题的对话框看起来很丑陋。 有时候我们需要在某些情况下更改按钮标题。 这个项目适合那些想要设计自定义文件对话框的人。 它包含三个“树形视图”文件对话框的实现。 第一个使用 Microsoft 文件和文件夹工具。 第二个是一个简单的对话框,它将显示您选择的任何文件的文件描述。 第三个是一个丰富的对话框,带有进度条、可移动菜单、状态栏和多种视图选择:详细信息、列表、大图标……

背景

对该语言和树形列表视图控件的基本了解。

使用代码

将您喜欢的对话框的所有代码文件导入到您的项目中,只需处理“确定”按钮的代码中的文件,而不是显示消息框。 我不会在这里解释所有内容,而只介绍标题:当您加载表单时,程序将使用 Directory.GetLogicalDrives 获取计算机中的所有驱动器,并将它们添加到树形视图控件中。 这里需要一个图像列表来设置驱动器列表的小图标。 现在我们需要一个过程来获取文件夹级别以处理对任何节点的单击(在选择事件之后)。 此过程将获取节点名称(驱动器或文件夹),并根据驱动器或文件夹的嵌套文件夹名称显示该节点的子节点,并在列表视图控件中显示文件。 由于有两种视图(大图标、小图标),因此需要两个图像列表,并且必须将它们绑定到列表控件。 以下是主要过程

' this will generate one level of folders in the tree node
' itree is the mother node and path is logical path
' on hard disk that this node stands for.
 
Sub printfilesfolders_here(ByVal path As String, ByRef itree As TreeNode) 
    Dim foldername As String 
    Dim filename As String 
    itree.Nodes.Clear() 
    On Error GoTo eee 

    For Each foldername In Directory.GetDirectories(path)
        ''take the name of folders in the folder or drive
        On Error GoTo eee 
        Dim jj = foldername.LastIndexOf("\") 
        Dim jj1 = foldername.Length - jj 
        'separate the name from the path
        Dim foldr = foldername.Substring((jj + 1), (jj1 - 1))
        itree.Nodes.Add(foldr, foldr) 'add only the name
    Next
    Exit Sub
eee: 
End Sub

'this will display files in the list view according to the path of the mother node

Sub displayfiles(ByVal path As String)
' it uses for each  on Directory.GetFiles 
' to take the pathes of files and then it gets 
' only the names and adds them in the list
' we use AddRange instead of add to gain the details view in the list

关注点

如果您在添加 USB 闪存盘或新的网络位置后启动对话框,它们将不会出现,因此您需要计划添加刷新按钮。

历史

  • 最后更新:2007-4-2。
© . All rights reserved.