Home

VCL Controls:
The Browse For Folder Dialog Box

 

Introduction

The Visual Component Library ships with other dialog boxes useful for tasks that involve files, such as letting the user browse the hard drive to select a folder. This can be done using the Browse For Folder dialog box:

The VCL provides a convenient dialog box used to browse the drives of the user’s computer or a computer on the network to locate a folder or a mapped drive.

 

Creation of a Browse for Folder Dialog Box

The Browse For Folder dialog box is made available through the SelectDirectory() function. Its syntax is:

bool __fastcall SelectDirectory(const AnsiString Caption,
				const WideString Root,
				AnsiString &Directory);

This function takes three arguments and returns two values. The Caption parameter displays under the title bar but above the tree view of the dialog box. The Root value is a string that represents the name of the root drive. The Directory string is the default path folder. This function returns a Boolean value upon exiting. Here is an example of using the SelectDiretory() function:

//---------------------------------------------------------------------------
void __fastcall TForm1::btnDirectoryClick(TObject *Sender)
{
	AnsiString Caption = "Select a Directory";
	const WideString Root = "C:\"";
	AnsiString Directory = "C:\\Program Files";
	SelectDirectory(Caption, Root, Directory);
}
//---------------------------------------------------------------------------

When the dialog box opens, it displays two buttons: OK and Cancel. The OK button is disabled because no directory would have been selected. To use the Browse For Folder dialog box, the user clicks the + button to expand a folder, and the – (if any) button to collapse a folder. Once the user locates the desired folder, he must click it to highlight it, which enables the OK button.

After using the Browse For Folder dialog box, if the user clicks Cancel or presses Esc, whatever change was made would be dismissed and the function would return false. If the user clicks OK or presses Enter, the function would return the full path of the folder the user had selected. This path is the returned Directory argument. You can use a conditional statement to find out what button the user had clicked then use the returned Directory string as you see fit. Here is an example:

//---------------------------------------------------------------------------
void __fastcall TForm1::btnDirectoryClick(TObject *Sender)
{
	AnsiString Caption = "Select a Directory";
	const WideString Root = "C:\"";
	AnsiString Directory = "C:\\Program Files";

	if( SelectDirectory(Caption, Root, Directory) == True )
		edtInstallation->Text = Directory;
}
//---------------------------------------------------------------------------

 

Home Copyright © 2005-2012, FunctionX, Inc.