Back to Devexpress

File and Folder Browser Behaviors

windowsforms-117251-common-features-behaviors-file-and-folder-browser-behaviors.md

latest6.4 KB
Original Source

File and Folder Browser Behaviors

  • Apr 03, 2023
  • 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.

  • 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 the IconSize 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 and SkinnableCompact - these values are available for the Open Folder behavior only and allow you to choose between the Wide and Compact 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.

csharp
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();
}
vb
Imports DevExpress.XtraEditors

Private Sub buttonEdit1_ButtonClick(ByVal sender As Object, ByVal e As DevExpress.XtraEditors.Controls.ButtonPressedEventArgs)
    Dim be As ButtonEdit = TryCast(sender, ButtonEdit)
    If xtraOpenFileDialog1.ShowDialog() = DialogResult.OK Then
        be.EditValue = xtraOpenFileDialog1.FileNames.FirstOrDefault()
    End If
End Sub

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.

csharp
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;
}
vb
Imports DevExpress.XtraEditors

Private Sub buttonEdit1_ButtonClick(ByVal sender As Object, ByVal e As DevExpress.XtraEditors.Controls.ButtonPressedEventArgs)
    Dim be As ButtonEdit = TryCast(sender, ButtonEdit)
    If xtraFolderBrowserDialog1.ShowDialog() = DialogResult.OK Then
        be.EditValue = xtraFolderBrowserDialog1.SelectedPath
    End If
End Sub