通用对话框- XP风格

介绍 我写这篇文章是为了那些要求通用对话模式的人。如果你是新手,你也可以在这个站点找到树视图对话框。 我们的问题是:微软将文件对话框定义为不可继承的,因此程序员只能使用“打开”和“保存”按钮。我们将改变这一点。 这是一个XP风格的通用对话框。你可以把你自己的图标在按钮,改变对话框的颜色和按钮的标题,并可能添加动画。 先决条件 基本的语言知识和listview控件是必需的。在开始使用之前,请阅读下面的警告。 使用的代码 在项目中导入您喜欢的对话框的所有代码文件,并在“Import”按钮的代码中处理文件,而不是显示消息框。我不会在这里解释所有的事情,但只是标题。 当您加载表单时,程序将使用目录。获取计算机中的所有驱动器,并将它们添加到combobox控件中。这里需要一个图像列表来设置所选驱动器的图标“以及以后所选文件夹”。初始路径是'c:\'。当我们在组合中选择一个项目时,我们将在列表框中显示所选项目的内容。如果我们选择一个驱动器,我们将在列表中显示它的文件夹,如果我们选择一个文件夹,我们将在列表中显示它的内容。但是,我们如何选择文件夹呢?我们只有开车的套餐! 实际上,我们将向list的双击事件添加一些操作。selecteditem,所以我们将检查列表是否。选中的项目是一个文件夹,如果为true,我们将把它的路径添加到组合框中,并在组合框附近显示文件夹图标。然后,我们将组合中的选定项设置为该文件夹的路径,因此,列表将被更新以显示该文件夹的项(包括文件、folders_only top level)。这里需要两个图像列表,因为有两个视图(大图标和小图标),并且必须将它们绑定到list控件。主要流程如下: 隐藏,收缩,复制Code

'this will handle combobox item selection  
Private Sub ToolStripComboBox1_SelectedIndexChanged(ByVal sender As Object, _
        ByVal e As System.EventArgs) _
        Handles ToolStripComboBox1.SelectedIndexChanged 

    'this will display colomns names with respect 
    'to the case folders or my computer(drives) 
    SetUpListViewColumns()
    'always display everything let user decide the extention after that
    ComboBox2.Text = "*.*"
    Dim dd As String 
    Dim flag As Byte 

    If ToolStripComboBox1.SelectedItem.ToString = Nothing Then Exit Sub
    ''err handler

    dd = ToolStripComboBox1.SelectedItem.ToString
    If ToolStripComboBox1.Text.ToLower = "my computer" Then
    ' the user pressed my computer button
        'it will display drives instead of folders in the list
        handle_mc()
        ' stack back is usful to go back,stors past pathes
        back.Push("my computer")
        If back.Count > 1 Then
        ''' i shouldn,t enable back button if i don,t have any back pathes
            ToolStripButton2.Enabled = True 
        End If
        Exit Sub 
    End If ''

    'the next will display icon according to user selectioon 
    '''determine dd icon '' if it is folder

    If Directory.Exists(dd) Then
        ToolStripButton5.BackgroundImage = TreeNodeImageList.Images(15) 
    End If

    '' if it is drive and what kind of drives is it ?
    Dim i As Integer
    Dim j() As String
    j = Directory.GetLogicalDrives

    For i = 0 To UBound(j)
        Dim cdrive As System.IO.DriveInfo

        cdrive = My.Computer.FileSystem.GetDriveInfo(j(i).ToString)

        If cdrive.ToString = dd Then
            Select Case cdrive.DriveType 
                Case DriveType.Fixed 
                    ToolStripButton5.BackgroundImage = TreeNodeImageList.Images(3)

                Case DriveType.CDRom 
                    ToolStripButton5.BackgroundImage = TreeNodeImageList.Images(11) 

                Case DriveType.Network 
                    ToolStripButton5.BackgroundImage = TreeNodeImageList.Images(5)

                Case DriveType.Removable 
                    ToolStripButton5.BackgroundImage = TreeNodeImageList.Images(12)
            End Select 'MsgBox("")
            GoTo outer
        End If 
    Next 

outer:
    ''''''''''''''''''''now we did set the icon
    'now it is time to display contents in listview
     displayfiles_folders(dd)

    folders_path = ToolStripComboBox1.Text 'global variable to hold the path
    back.Push(folders_path) 'save the path in back stack

    If back.Count > 1 Then
    ' remember to enable the back button if you have recent pathes
        ToolStripButton2.Enabled = True 
    End If

    Me.Text = folders_path 'display the path in the window text

End Sub

''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'this sub will handle item double click in the list view

Private Sub ListView_DoubleClick(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles ListView.DoubleClick 

    Dim jj As String = Me.Text 
    If is_drive(jj) = True Then
    'this happens when i click on a drive 
        save_adder(jj)
        'add the path of it to combobox 
        'and refresh list box according to that 
    Else If Directory.Exists(jj) = False Then
    ' the only possibility for this if i click on file ..well nothing will happen 
        Exit Sub 
    End If ' now we are sure that it is a folder 
    save_adder(jj) 'make the refresh after we add and set selected item in the combo
    End If
End Sub

的兴趣点 我不负责任何滥用此控制将导致任何损害您的系统。删除按钮将永久删除您选择的任何项目,在您确认删除后!如果你选择删除“我的文件”文件夹,你可能会损坏你的系统,所以不要尝试这样做。 当向“drives_folders”组合框添加项目时,使用已定义的过程(save_adder)而不是手动添加。它将防止重复的项目在组合框。我也可以使用树列表来显示驱动器和文件夹,而不是使用组合框。当然,每次用户单击“fake”组合框时,我都需要隐藏和取消隐藏该列表,但这将使我们能够在列表中显示驱动器和文件夹的图标。我认为完成一个小小的要求是一个漫长的过程。 历史 最后更新:5-5-2007。 本文转载于:http://www.diyabc.com/frontweb/news434.html

posted @ 2020-08-07 00:51  Dincat  阅读(216)  评论(0编辑  收藏  举报