Introduction
I am writing this article for the sake of people who asked for a common dialog pattern. If you are new, you can find the tree view dialog in this site too.
Our problem is this: Microsoft defined the files dialog boxes as non-inheritable, so programmers are restricted to just the "open" and "save" buttons. We will change that.
This is an XP style common dialog box. You can put your own icons in the buttons, change the color of the dialog and the caption of the buttons, and may be add animation to it.
Prerequisites
A basic knowledge of the language and the listview control is required. Please read the warning below before you start using it.
Using the Code
Import all the code files of the dialog you like in your project and just handle the files in the code of the "import" button instead of showing the message boxes. I will not explain everything here, but just the headlines.
When you load the form, the program will use Directory.GetLogicalDrives
to get all the drives in your computer and will add them to the combobox control. An image list is needed here to set the icon of the selected drive "and for later, the selected folder". The initial path is 'c:\'. When we select an item in the combo, we will display in the listbox the contents of the selected item. So if we select a drive, we will display its folders in the list, and if we select a folder, we will display its contents in the list. But, how do we select a folder? We only have drives in the combo!
Actually, we will add some action to the double click event of list.selecteditem
, so we will check to see if the list.selected
item is a folder and if that is true, we will add the its path to the combo box and we will display the folder icon near the combo. Then, we will set the selected item of the combo to that folder's path, and because of that, the list will be updated to display the items (including files, folders_only top level) of the folder. Two image lists are needed here because of the two views (large icons, small icons), and you must bind them to the list control. Here are the main procedures:
Private Sub ToolStripComboBox1_SelectedIndexChanged(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles ToolStripComboBox1.SelectedIndexChanged
SetUpListViewColumns()
ComboBox2.Text = "*.*"
Dim dd As String
Dim flag As Byte
If ToolStripComboBox1.SelectedItem.ToString = Nothing Then Exit Sub
dd = ToolStripComboBox1.SelectedItem.ToString
If ToolStripComboBox1.Text.ToLower = "my computer" Then
handle_mc()
back.Push("my computer")
If back.Count > 1 Then
ToolStripButton2.Enabled = True
End If
Exit Sub
End If
If Directory.Exists(dd) Then
ToolStripButton5.BackgroundImage = TreeNodeImageList.Images(15)
End If
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 GoTo outer
End If
Next
outer:
displayfiles_folders(dd)
folders_path = ToolStripComboBox1.Text back.Push(folders_path)
If back.Count > 1 Then
ToolStripButton2.Enabled = True
End If
Me.Text = folders_path
End Sub
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
save_adder(jj)
Else If Directory.Exists(jj) = False Then
Exit Sub
End If save_adder(jj) End If
End Sub
Points of Interest
- I am not responsible for any misuse of this control that will result in any damage to your system.
The delete button will permanently delete any item you select, after you confirm the deletion! If you choose to delete the 'My Documents' folder, you may damage your system, so don't try to do that.
- When adding items to the "
drives_folders
" combobox, use the defined procedure (save_adder
) instead of doing a manual addition. It will prevent duplicated items in the combobox.
- I could use a tree list too to display the drives and folders instead of using the combobox. Of course, I would need to hide and unhide this list each time the user clicks on the 'fake' combo box, but this will give us the ability to display the icons of drives and folders inside the list. I think it is a long way to accomplish a small requirement.
History