File and Folder Browser Behaviors
- 4 minutes to read
This document gathers multiple similar behaviors that solve various tasks related to browsing and selecting files and folders.
Note
Each behavior inherits the preceding behavior’s features on this list. Do not attach, for instance, a File Path Behavior to ButtonEdit if this editor already has the Open File Behavior attached.
File Icon Behavior
This behavior checks the displayed text of a target control and if this text is a path to an existing file or folder, adds its icon to the control. The following figure illustrates multiple label controls with this behavior attached to them.
Supported controls:
Behavior options include:
ShowIcon
- enables or disables the behavior.IconSize
- allows you to select the required size for file (folder) images: small, medium, large or extra large.The default height of text box editors equals 20 pixels, which allows them to display only small icons. To display icons of other sizes, turn the RepositoryItem.AutoHeight property off and increase the editor’s height by using the Size property.
InvalidPathImage
- an icon that will be displayed when the target control does not display a valid file (folder) path. The assigned image is shown as is and does not scale in accordance to theIconSize
setting.
File Path Behavior
Provides an auto-complete feature for the editor this behavior is attached to. Using this feature end-users can quickly enter file and/folder paths. Once a valid path is entered, its icon is displayed next to the editor’s text.
To navigate through proposed auto-complete options, end-users can press Tab.
Supported controls:
Behavior options include:
IconSize
,InvalidPathImage
- same as for the File Icon Behavior.Mode
- specifies whether the auto-complete feature proposes only folder paths, only file paths or both of them.Filter
- allows you to narrow auto-complete hints to a specific folder or file. For example, if this property is set to “Windows\Globalization\Time Zone”, browsing the folder hierarchy on a system disc drive will instantly bring end-users to this folder.
Open File and Open Folder Behaviors
These behaviors instantly turn your editor into a file or a folder selector. The editor button invokes a dialog that allows you to select a file or a folder, depending on which behavior you have attached. The path and the icon of the object selected in this dialog are then shown by the editor.
Supported controls:
Behavior options include:
Mode
- specifies whether the auto-complete feature proposes only folder paths, only file paths or both of them in the attached editor. To navigate through proposed auto-complete options, end-users can press Tab.ShowIcon
,IconSize
,InvalidPathImage
- same as for the File Icon Behavior.DialogStyle
- allows you to choose the dialog type and style:Standard
- the target control invokes default Visual Studio FolderBrowser dialogs;Skinnable
- the target control invokes skinnable DevExpress dialogs;SkinnableWide
andSkinnableCompact
- these values are available for the Open Folder behavior only and allow you to choose between theWide
andCompact
dialog styles (see the XtraFolderBrowserDialog.DialogStyle property);
Specify Initial Directory
Solution 1: Open File Dialog
Attach the File Path Behavior to the button editor. Create a new XtraOpenFileDialog and specify its InitialDirectory property. Handle the ButtonEdit.ButtonClick event to display the XtraOpenFileDialog.
using DevExpress.XtraEditors;
private void buttonEdit1_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e) {
ButtonEdit be = sender as ButtonEdit;
if(xtraOpenFileDialog1.ShowDialog() == DialogResult.OK)
be.EditValue = xtraOpenFileDialog1.FileNames.FirstOrDefault();
}
Solution 2: Folder Browser Dialog
Attach the File Path Behavior to the button editor. Create a new XtraFolderBrowserDialog and specify its SelectedPath property. Handle the ButtonEdit.ButtonClick event to display the XtraFolderBrowserDialog.
using DevExpress.XtraEditors;
private void buttonEdit1_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e) {
ButtonEdit be = sender as ButtonEdit;
if(xtraFolderBrowserDialog1.ShowDialog() == DialogResult.OK)
be.EditValue = xtraFolderBrowserDialog1.SelectedPath;
}