We need you!

We're working hard on the next version of Developer Fusion. Let us know what you think we should be up to!

Members

Technology Zones

Articles

Hosted By

MaximumASP

Info

Rated
Read 102,538 times

Contents

Related Categories

Common Dialog Control - Using the Open/Save Dialog

Using the Open/Save Dialog

You can use the open dialog to allow the user to select a file, which your application can then open, or analyse etc. Below are the most common parameters you will need to set before displaying the Open Dialog Box.

Parameter Changes...
DialogTitle the Title displayed in the Dialog Box
Filter the File Types displayed in the file type list.
FilterIndex the default file type
Flags custom settings such as hide read-only files
CancelError whether an error occurs when the dialog box is cancelled.

The following code displays an open Dialog, with read-only files hidden. The extension list is set to allow Word Documents (*.doc) and Excel Spreadsheets (*.xls). The dialog will not allow you to select files that do not exist.

' Sets the Dialog Title to Open File
CommonDialog1.DialogTitle = "Open File"

' Sets the File List box to Word documents and Excel documents
CommonDialog1.Filter = "Word Documents (*.doc)|*.doc|Excel Spreadsheets (*.xls)|*.xls"

' Set the default files type to Word Documents
CommonDialog1.FilterIndex = 1

' Sets the flags - File must exist and Hide Read only
CommonDialog1.Flags = cdlOFNFileMustExist + cdlOFNHideReadOnly

' Set dialog box so an error occurs if the dialogbox is cancelled
CommonDialog1.CancelError = True

' Enables error handling to catch cancel error
On Error Resume Next
' display the dialog box
CommonDialog1.ShowOpen
If Err Then
    ' This code runs if the dialog was cancelled
    Msgbox "Dialog Cancelled"
    Exit Sub
End If
' Displays a message box.
Msgbox "You selected " & CommonDialog1.FileName

The Filter property is a string using the following format:

"FileTypeText1 |FileTypeExtension1 | FileTypeText2 | FileTypeExtension2"

The FileName property stores the selected file's path and name. The FileTitle property stores just the filename, you could use this to set a dialogbox's title:

Form1.Caption = "Text From : " & CommonDialog1.FileTitle

The following code displays an save Dialog. The extension list is set to allow Text File (*.txt) and All Files (*.*). It hides readonly files, prompts if you want to overwrite the existinf files, and only allows the file to be saved in a folder that already exists

' Sets the Dialog Title to Save File
CommonDialog1.DialogTitle = "Save File"

' Sets the File List box to Text File and All Files
CommonDialog1.Filter = "Text File (*.txt)|*."|AllFiles (*.*)|*.*"

' Set the default files type to Text File
CommonDialog1.FilterIndex = 1

' Sets the flags - Hide Read only, prompt to overwrite, and path must exist
CommonDialog1.Flags = cdlOFNHideReadOnly + cdlOFNOverwritePrompt _
+ cdlOFNPathMustExist

' Set dialog box so an error occurs if the dialogbox is cancelled
CommonDialog1.CancelError = True

' Enables error handling to catch cancel error
On Error Resume Next
' display the dialog box
CommonDialog1.ShowSave
If Err Then
    ' This code runs if the dialog was cancelled
    Msgbox "Dialog Cancelled"
    Exit Sub
End If

' Displays a message box.
Msgbox "You saved the files as  " & CommonDialog1.FileName

If you just want to display the open and save dialogs, what's the point of having a control with extra code you don't need? VB Web has the solution! Take a look at our Open/Save Dlg Control (with multi-file select support)

James first started writing tutorials on Visual Basic in 1999 whilst starting this website (then known as VB Web). Since then, the site has grown rapidly, and James has written numerous tutorials, articles and reviews on VB, PHP, ASP and C#. In October 2003, James formed the company Developer Fusion Ltd, which owns this website, and also offers various development services. In his spare time, he's a 3rd year undergraduate studying Computer Science in the UK. He's also a Visual Basic MVP.

Comments

  • Re: Common Dialog Control

    Posted by ffrreeaakk on 20 Feb 2007

    I know what you're thinking about. I have exactly the same problem. I don't know how to make Common Dialog Box show such window: http://Common Dialog Control

    Posted by al_sebie on 26 Jul 2006

    Dear Sir


    Thanks for you help and I like you way that you got it, but my questation is Can I use Common Dialog without use File Type? because I need only open folder without select file "Onl...

  • Posted by takedownca on 20 Apr 2004

    Using the FileName (path) and FileTitle (filename) properties of the dialog control,
    [code]
    Dim FilePathOnly As String

    FilePathOnly = Left(FileName, Len(FileName) - Len(FileTitle))
    'or
    FilePath...

  • Posted by darkwolf on 24 Dec 2003

    [code]OpenPath = App.Path & "\templates\"

    OpenFileWName = TreeView1.SelectedItem

    If OpenFileWName = OpenPath Then
    MsgBox "You can not select a directory. Please select a template file."
    E...

  • path

    Posted by darkwolf on 20 Dec 2003

    is there a way of getting just the path of the file this way, without the file name in it?
    E.G. if i open
    C:\Windows\Wini.ini
    can i get the program to read just
    C:\Windows
    from that?