Consultancy and Programming
Advice, tips, techniques and downloads for Visual Foxpro developers.
Visual FoxPro applications sometimes need to prompt the user for a file or directory name. Usually, the best way to do that will be with a combination of textbox and command button. If the user already knows the required name, he or she can simply type it into the textbox. If not, they can click on the command button to bring up the standard Select Directory or File Open dialogue, from where they can navigate to the file or directory in question.
Creating that kind of mechanism is not particularly difficult. But it's even easier if you use Barbara Peisch's cmpSelDir class, which does the whole job for you.
The cmpSelDir class is a composite class, containing the textbox and command button described above. To use it, just drop it on a form. By default, the button will open the Select Directory dialogue. To make it open the File Open dialogue instead, change the WhichGet property to the word File (note that this is case-sensitive). With both types of dialogue, the file or directory name which the user selects will be inserted into the textbox, overwriting any text that was there before. If the user cancels out of the dialogue, any existing text will be preserved.
The class offers several other properties which let you customise the dialogues in various ways. For example, you can set PromptText to a short prompt which will appear in the dialogues, or OpenButtonCaption to the text that is to appear on the OK button in the File Open dialogue. More interestingly, the FileExtensions property can be used to populate the Files of Type control in the File Open dialogue. You can set this to a list of file extensions, with optional wildcard characters, and optionally accompanied by short descriptions (Example: "Tables:DBF; Text Files:TXT,CSV").
All of these properties have default settings, so you can ignore any that you are not interested in. For more information, see the class's ZReadMe method.
And that's all there is to it. cmpSelDir is an elegant class, easy to use, and an excellent example of a tool which does one job and does it well.
Barbara Peisch runs Peisch Custom Software Inc., a busy VFP consultancy in Carlsbad, CA (USA). She was the technical editor for the book, "WebRAD: Building Database Websites with Visual FoxPro and Web Connection", and was co-author for the "Customers vs Code: Keeping Your Cool with the Essential Component" column in CoDe magazine. Barbara was the original author of the KitBox column in FoxTalk, and is a Microsoft MVP as well as an active member of the Microsoft Application Developers Forum on Compuserve. You can contact her at firstname.lastname@example.org, or visit www.peisch.com.
Click the link below to download CMPSELDIRFILE.ZIP. This zip file contains a class library, which in turn contains the cmpSelDir class. It also contains another class, called cmdFileFind, this being the parent class of the command button used in the composite class. The total download size is just 5 KB.
Mike Lewis Consultants Ltd. April 2002
FoxStuff is maintained by Mike Lewis Consultants Ltd. as a service to the VFP community. Feel free to download and use any code or components, and to pass around copies of the articles (but please do not remove our copyright notices or disclaimers).
The information given on this site has been carefully checked and is believed to be correct, but no legal liability can be accepted for its use. Do not use code, components or techniques unless you are satisfied that they will work correctly in your applications.
© Copyright Mike Lewis Consultants Ltd.