FolderItem.ShowOpenFileDialog

From Xojo Documentation

Shared Method

FolderItem.ShowOpenFileDialog(filter As String) As FolderItem

New in 2019r2

Supported on Desktop.

Shows the standard Open File dialog box and returns the file (as a folderitem) selected by the user.

Usage

result = FolderItem.ShowOpenFileDialog(filter)

Part Type Description
result FolderItem Represents the file that was opened by the user. The result will be Nil if the user clicked the Cancel button.
filter String Semicolon separated list of file types the user can open.

The file types must be defined as file types in your project, either in the IDE in a File Type Set or with the FileType class.

Notes

The ShowOpenFileDialog function displays the standard open-file dialog box for the platform on which the application is running. The FolderItemDialog class has the same purpose but allows for some customization.

The filter parameter is used to limit the types of files that the user can open to one or more of the file types defined via the FileType class or in the File Type Sets Editor in the IDE. The filter is a semicolon-separated list of file type names. For example, if you wanted the user to be able to open only text files and postscript files when making a particular call to the ShowOpenFileDialog function, you would define two file types using either the File Type Sets Editor or the FileType class. You would then pass "application/text; application/postscript" (or the name you chose) as the filter to the ShowOpenFileDialog function.

Only files whose type matches one of the file types passed in the filter will be displayed in the open file dialog box. If you want to display all files, you will need to add a file type to the project that uses "????" as its Mac Type.

You can also pass in actual FileType names; they are converted to their corresponding strings.

The ShowOpenFileDialog function returns a FolderItem that represents the file the user selected. You can then use the FolderItem to access various data about the file such as its name, full path, etc. See the FolderItem class for more information.

If the user clicks the Cancel button in the open file dialog box, the FolderItem will be Nil. You can test for this by comparing the FolderItem with the Nil value. Accessing a Nil FolderItem will cause a NilObjectException error.

Sample Code

This code opens an mp4 file and then opens its movie and assigns it to a MoviePlayer control. The file type was added using the File Types Editor as a custom Common File Type.

Var f As FolderItem
Var m As New Movie

f = FolderItem.ShowOpenFileDialog("video/mp4")
If f<> Nil Then
m.Open(f)
Else
MessageBox("Open failed.")
End If

This code illustrates how to use the FileType class do specify the types of files that can be opened by ShowOpenFileDialog. When you define FileType objects, you must specify the MacType and Extensions properties. The call to ShowOpenFileDialog can combine several FileTypes in the way the example shows.

Var jpegType As New FileType
jpegType.Name = "image/jpeg"
jpegType.Extensions = "jpg;jpeg"

Var pngType As New FileType
pngType.Name = "image/png"
pngType.Extensions = "png"

Var f As FolderItem

// The actual FileTypes are converted to strings automatically for use
// with OpenFileDialog
f = FolderItem.ShowOpenFileDialog( jpegType + pngType )

In the IDE you can create file types for practically all types using the Common File Types button in the File Type Sets editor. It displays a pop-up menu of the most common types and a "More" button that displays a much larger pop-up. Instead of creating your file types in code, you can use the Common File Types feature to set these properties.

The following code uses the built-in file type for WMV movies to open a WMV file, assign the movie to the Movie property of a MoviePlayer, and plays the movie.

Var f As FolderIem
f = Folderitem.ShowOpenFileDialog(FileTypes1.VideoXMsWmv) // Converts FileType1.VideoXMsWmv to a string
If f <> Nil Then
MoviePlayer1.Movie = Movie.Open(f)
MoviePlayer1.Play
End If

See Also

FolderItem.ShowSaveFileDialog, FolderItem.ShowSelectFolderDialog functions; FileType, FolderItem, FolderItemDialog, OpenDialog.