通用对话框 -XP 风格






3.11/5 (29投票s)
如何实现 XP 风格的通用对话框。
引言
我写这篇文章是为了那些需要通用对话框模式的人。 如果你是新手,你也可以在这个网站找到树形视图对话框。
我们的问题是:微软将文件对话框定义为不可继承的,所以程序员只能使用 "打开" 和 "保存" 按钮。 我们将会改变这一点。
这是一个 XP 风格的通用对话框。 你可以在按钮中放入你自己的图标,改变对话框的颜色和按钮的标题,并且可以添加动画。
必备组件
需要具备语言和列表视图控件的基本知识。 在开始使用它之前,请阅读下面的警告。
Using the Code
将你喜欢的对话框的所有代码文件导入到你的项目中,只需处理 "导入" 按钮代码中的文件,而不是显示消息框。 我不会在这里解释所有内容,只解释要点。
当加载表单时,程序将使用 Directory.GetLogicalDrives
获取计算机中的所有驱动器,并将它们添加到组合框控件中。 这里需要一个图像列表来设置所选驱动器的图标 "以及稍后,所选文件夹的图标"。 初始路径是 'c:\'。 当我们在组合框中选择一个项目时,我们将在列表框中显示所选项的内容。 因此,如果我们选择一个驱动器,我们将在列表中显示它的文件夹,如果我们选择一个文件夹,我们将在列表中显示它的内容。 但是,我们如何选择一个文件夹? 组合框中只有驱动器!
实际上,我们将对 list.selecteditem
的双击事件添加一些操作,因此我们将检查 list.selected
项目是否是一个文件夹,如果是,我们将把它路径添加到组合框中,我们将在组合框附近显示文件夹图标。 然后,我们将组合框的选定项目设置为该文件夹的路径,因此,列表将更新以显示该文件夹的项目(包括文件,仅文件夹的顶层)。 这里需要两个图像列表,因为有两种视图(大图标,小图标),并且您必须将它们绑定到列表控件。 以下是主要过程
'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
) 而不是手动添加。 这将防止组合框中出现重复的项目。 - 我也可以使用树形列表来显示驱动器和文件夹,而不是使用组合框。 当然,我需要每次用户单击“假”组合框时隐藏和显示此列表,但这将使我们能够显示列表中驱动器和文件夹的图标。 我认为这是一个实现一个小要求的漫长道路。
在您确认删除后,删除按钮将永久删除您选择的任何项目! 如果您选择删除“我的文档”文件夹,您可能会损坏您的系统,因此请不要尝试这样做。
历史
- 上次更新:5-5-2007。