Click here to Skip to main content
Click here to Skip to main content

BrowseField and BrowseList - file and folder browser/lister

, 31 Jan 2005
Rate this:
Please Sign up or sign in to vote.
File and folder browser/lister controls

Introduction

The great thing about OOP and .NET is that you should never have to find yourself coding the same thing over and over again. Reusability is the name of the game. So when I recently found myself in the vicious cycle of re-coding the same interface component, I thought that surely there was a better way.

Background

I frequently need to create little "wizards" and other applications that work with files and folders. The "industry standard" way of locating these resources is to put a TextBox on the page, and a little icon of a folder next to that, and use the icon folder to launch the browse dialog, storing the results in the TextBox. Surely, you've seen this a thousand times, and if you were building a web application, there is already an object to do this. Not so in Windows Forms, however.

Every time I needed to use this functionality, I had to:

  • Put some Panels on the page to host the TextBox and PictureBox (this makes it possible to dock them correctly).
  • Add the TextBox, Label and PictureBox controls.
  • Align the PictureBox correctly (it never lines up correctly with the TextBox, by default), and the Label as well if it was placed to the left.
  • Locate the folder image.
  • Add the FileDialog and FolderDialog objects.
  • Wire up the FileDialog and FolderDialog to the TextBox and PictureBox as needed.
  • Recode the Drag-n-Drop for each TextBox.
  • Recode the function to strip the filename from the path if I needed it.

Well, that's a lot of coding to do over and over. And I got tired of it. So I created the BrowseField.

Features of the BrowseField

  • Allows you to drop a single control on the page, eliminating all of the steps above.
  • Has a property called BrowseType that allows you to specify whether it should spawn a File or Folder dialog.
  • CaptionStyle property determines if the Label should appear above, to the left, or out-dented (Wizard 97 style) from the TextBox.
  • Is already setup to accept drag-n-drop from Windows Explorer.
  • Can simply enter the path by hand.
  • Properly docks and resizes without any extra work.
  • If entering files, you can specify the FilenameOnly() option which will truncate the file path down to just the filename.
  • You can query the FileName() property, which will also give you filename while leaving the full path intact.
  • You have full control over the appearance - set the text and/or Label fonts as needed, or change the border style of the TextBox, etc.
  • Exposes the TextChanged() event.

Using the BrowseField control is as simple as dropping it onto a page, setting the properties up the way you like them in the IDE, and then querying the Text value for the result. Or, you could set the properties programmatically like this:

BrowseField1.Caption = "This is my label text"
BrowseField1.CaptionStyle = BrowseField.CaptionStyles.Top
BrowseField1.FileDialogFilter = "All Files (*.*)|*.*"
BrowseField1.FileDialogFilterIndex = 1
BrowseField1.BrowseType = BrowseField.BrowseTypes.File
BrowseField1.BorderStyle = BorderStyle.FixedSingle

Note that you do not need to setup any code to display the dialog boxes, handle drag and drop operations, or handle resizing operations. For the most part, any changes you make are purely cosmetic. It just works.

BrowseList

Shortly after releasing the BrowseField control, I realized that I often used a ListBox control in much the same way - to gather multiple folder and file names. This is even more of a hassle than using the TextBox, as I usually add several PictureBox controls used to Add, Delete and Edit the paths entered. Checks for duplicate paths are needed as well. And in my opinion, some critical events were left out of the ListBox control - ItemAdded(), ItemRemoved(), and ItemChanged(). These needed to be implemented too.

Features of BrowseList

  • Has all the features of BrowseField (except for the FileName property).
  • Adds the ItemAdded(), ItemRemoved() and ItemChanged() events.
  • Allows for multiple items to be added (either using the dialogue or drag-n-drop), by default.
  • Option to allow hand editing of list items (enabled by default).

I think I should also mention that both controls are written in VB.NET. However, as you already know, that won't stop it from working with C# or any other .NET compliant language. There isn't a great deal of commenting in the code - as this is really more of a tool than a tutorial, I didn't see the need for it. Also, since these are composite controls, they are very simple in nature.

Licensing

I always like to list my projects as "ThankYouWare". That is, if you use them, please send me a "Thank You" email letting me know if you like them, and any features or improvements you'd like to see. The controls are open source, you may modify them at will (just don't ask me to fix your modified code). If you redistribute the original or modified code, please credit me somewhere, even if it's just in the code comments.

Breaking Changes

While I hate to make "breaking changes" in controls, I thought it was necessary with this control. The original versions of these controls had a minimal interface, and instead of providing access to the child controls through shadowed properties, I simply exposed the child controls directly. This worked, and certainly exposed every aspect of the child controls to the programmer, but it allowed the programmer to "break" the functionality of the code, and wasn't VS.NET-IDE friendly. (Changes made in the properties grid of the IDE tended to disappear randomly.) Frankly, it was just poor programming.

In version 2, all the important properties and events have been exposed directly. Now the programmer cannot "break" the control's built-in validation and security. It also means that the controls play nice with the IDE. As well, the Caption (Label) is entirely new.

Known Problems

There is a funny little "bug" in the resizing of the controls sometimes. For example, if you open the sample project and change drag the form's width so that it is very small, then re-expand it, the right side of the browse controls goes off the edge of the form. I'm not sure why this is taking place. Also, while very minor, there are icons associated with the controls, but they don't appear to be showing up. If anyone knows of a fix to these problems, I'll be happy to update the code and credit you in the code as well. Thanks.

History

  • Oct. 15 2004 - Added the BrowseList control.
  • Jan. 25 2005 - Both controls rewritten and released as version 2.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Todd Davis
Systems Engineer Virtual RadioLogic
United States United States
Todd Davis has been working in web and application development for several years, using Silverlight, ASP.NET, VB.NET, C#, C++ and Javascript, as well as a great deal of work with SQL server and IIS.
 
He currently works for Virtual Radiologic in Eden Prairie, MN, however he is better known for his varied work in the open source community, especially the DotNetNuke project for which he provided several world-renowned training videos and modules. A huge advocate of open source and open knowledge sharing, everything on his website (www.SeaburyDesign.com) is always offered for free.
 
Whenever he is not actively coding at his laptop (a rarity to be sure), he can be found woodworking, walking with his wife and kids, or motoring along the back roads of MN on his Harley Davidson Fatboy.

Comments and Discussions

 
GeneralWeb Application Pinmembermalhotrm2-May-07 16:09 
GeneralRe: Web Application Pinmemberjagadishdandin17-May-07 20:58 
QuestionIs it possible to have the buttons as a separate control? Pinmembermlyons25-Apr-07 8:23 
AnswerRe: Is it possible to have the buttons as a separate control? PinmemberTodd Davis25-Apr-07 8:30 
QuestionTextChanged Event not visible PinmemberSkipSnyder6-Apr-07 17:12 
QuestionSo... how do I use it? PinmemberPerry213-Mar-07 12:00 
AnswerRe: So... how do I use it? PinmemberTodd Davis13-Mar-07 15:05 
GeneralThanks Pinmemberdatlq2003@yahoo.com11-Apr-06 22:10 
GeneralCurrentDirectory Changed PinmemberJosema8-Mar-06 6:04 
QuestionDiscussion: Shall the ListBox1 be public? PinmemberFauthD20-Oct-05 3:49 
GeneralAdditional event for BrowseList PinmemberFauthD20-Oct-05 3:45 
GeneralBug and fix in BrowseList Delete_Click PinmemberFauthD20-Oct-05 3:39 
QuestionTextChanged is not working here PinmemberFauthD18-Oct-05 6:07 
Generalpreloading current path/file PinmemberFauthD18-Oct-05 6:01 
GeneralSweet Pinmemberskitzallykat3-Aug-05 15:27 
GeneralCaption fix, tooltip, and file validation. PinmemberRobert Freeman7-Mar-05 4:34 
GeneralRe: Caption fix, tooltip, and file validation. PinmemberRobert Freeman7-Mar-05 6:55 
GeneralThe control's icons ... PinmemberW. Kleinschmit2-Feb-05 2:31 
GeneralRe: The control's icons ... PinmemberTodd Davis2-Feb-05 2:35 
GeneralThe resizing bug ... PinmemberW. Kleinschmit2-Feb-05 1:48 
GeneralFew questions Pinmembersdcougar13-Jan-05 9:03 
GeneralRe: Few questions PinmemberTodd Davis13-Jan-05 9:11 
GeneralRe: Few questions Pinmembersdcougar13-Jan-05 9:50 
GeneralRe: Few questions Pinmembersdcougar13-Jan-05 15:15 
GeneralDragDrop registration failed Pinmemberzoomba5-Nov-04 9:55 
GeneralRe: DragDrop registration failed PinmemberTodd Davis5-Nov-04 10:56 
GeneralRe: DragDrop registration failed Pinmemberzoomba5-Nov-04 11:04 
GeneralRe: DragDrop registration failed PinmemberTommy Visic11-Feb-05 6:55 
GeneralAlso great... PinmemberHumanOsc23-Oct-04 3:58 
GeneralRe: Also great... PinmemberTodd Davis23-Oct-04 4:47 
GeneralThanx... PinmemberHumanOsc14-Oct-04 22:04 
GeneralRe: Thanx... Pinmembermeuri7-Feb-05 6:50 
GeneralC# PinsussAnonymous14-Oct-04 18:08 
GeneralRe: C# PinsussAnonymous14-Oct-04 18:09 
GeneralRe: C# PinsussAnonymous14-Oct-04 18:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.141015.1 | Last Updated 31 Jan 2005
Article Copyright 2004 by Todd Davis
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid