Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Common Dialog Box - XP Style

0.00/5 (No votes)
10 May 2007 1  
How to implement a Common dialog box - XP style.

Screenshot - Article.gif

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:

'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

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

  • Last updated: 5-5-2007.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here