Click here to Skip to main content
15,881,803 members
Articles / Programming Languages / Visual Basic

Common Dialog Box - XP Style

Rate me:
Please Sign up or sign in to vote.
3.11/5 (29 votes)
10 May 2007CPOL3 min read 46.9K   705   30   7
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:

VB
'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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Comments and Discussions

 
QuestionRefreshing ..need some help here Pin
fox_of_197815-May-07 9:52
fox_of_197815-May-07 9:52 
AnswerRe: Refreshing ..need some help here Pin
Alaa Jubran23-Jun-07 1:12
Alaa Jubran23-Jun-07 1:12 
Generalabout the icons Pin
laura_horowitz10-May-07 6:41
laura_horowitz10-May-07 6:41 
Generalmasterpiece Pin
Arak_usa10-May-07 3:05
Arak_usa10-May-07 3:05 
GeneralI can not thank you enough for what you have done . Pin
Andromeda ..10-May-07 2:06
Andromeda ..10-May-07 2:06 
AnswerRe: I can not thank you enough for what you have done . Pin
Alaa Jubran10-May-07 2:35
Alaa Jubran10-May-07 2:35 
GeneralRe: You Kept Your Promise Pin
Alaa Jubran10-May-07 2:38
Alaa Jubran10-May-07 2:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.