|
|||||||||||||||||||||
|
|||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionOne of the things that is really missing in the VS controls is a file browser control, like the Explorer control in Windows, to navigate through folders and files, to select files, etc. in a modeless way. The Quick Start (for the ones that don’t have time)
Getting startedIn order to create our control, we will need a First Step - Creating the controlCreate a new class, derived from /// <summary>
/// Represents a ListBox control with file names as items.
/// </summary>
public class FilesListBox : ListBox
{
...
...
//// <summary>
/// Intializes a new instance of the FilesListBox class, to view a list of
/// files inside a ListBox control.
/// </summary>
public FilesListBox()
{
SelectionMode = System.Windows.Forms.SelectionMode.MultiExtended;
DrawMode = DrawMode.OwnerDrawFixed;
}
....
....
}
The default Second Step - The Events/// <summary>
/// Occures whenever a file is selected by a double click on the control.
/// </summary>
public event FileSelectedEventHandler FileSelected;
The Third Step - The properties// Gets or sets the directory name that the files should relate to
public string DirectoryName { get; set; }
// Gets or sets a value indicating wheater
// to show directories on the control
public bool ShowDirectories { get; set; }
// Gets or sets a value indicating wheater to show the back
// directory icon on the control
// (when the IsToShowDirectories property is true)
public bool ShowBackIcon { get; set; }
// Gets an array containting the currently selected files
// (without directories) in the FilesListBox.
The properties are pretty self explanatory and you can add lots of properties such as Fourth Step - Populating the ListBox with filesThe /// <summary>
/// Populate the list box with files and directories according to the
/// directoryName property
/// </summary>
private void PopulatingItems()
{
// Ignore when in desing mode
if (DesignMode)
return;
this.Items.Clear();
// Shows the back directory item (
if (_showBackIcon && _directoryName.Length > 3)
{
Items.Add("..");
}
try
{
// Fills all directory items
if (_showDirectories)
{
string[] dirNames = Directory.GetDirectories(_directoryName);
foreach (string dir in dirNames)
{
string realDir = Path.GetFileName(dir);
Items.Add(realDir);
}
}
// Fills all list items
string[] fileNames = Directory.GetFiles(_directoryName);
foreach (string file in fileNames)
{
string fileName = Path.GetFileName(file);
Items.Add(fileName);
}
}
catch
{
// eat this - back is still optional
}
Refresh();
}
First of all, we check that we are not in The next step is to add the 'back' directory (..) if specified, and all directories in the Note that I only take the folder name for the display and not the full path using The next step required is to add all the files in the Fifth Step - Painting file icons and namesAs mentioned above, we handle the listbox drawing in protected override void OnDrawItem(DrawItemEventArgs e)
{
e.DrawBackground();
e.DrawFocusRectangle();
Rectangle bounds = e.Bounds;
if (e.Index > -1 && e.Index < Items.Count)
{
Size imageSize;
string fileNameOnly = Items[e.Index].ToString();
string fullFileName = GetFullName(fileNameOnly);
Icon fileIcon = null;
Rectangle imageRectangle = new Rectangle( bounds.Left + 1,
bounds.Top +1 , ItemHeight - 2,ItemHeight - 2);
if (fileNameOnly.Equals(".."))
{
// When .. is the string - draws directory icon
fileIcon =
IconExtractor.GetFileIcon(Application.StartupPath, _fileIconSize);
e.Graphics.DrawIcon(fileIcon,imageRectangle);
}
else
{
fileIcon =
IconExtractor.GetFileIcon(fullFileName, _fileIconSize);
// Icon.ExtractAssociatedIcon(item);
e.Graphics.DrawIcon(fileIcon,imageRectangle);
}
imageSize = imageRectangle.Size;
fileIcon.Dispose();
Rectangle fileNameRec = new Rectangle(bounds.Left +
imageSize.Width + 3, bounds.Top,
bounds.Width - imageSize.Width - 3, bounds.Height);
StringFormat format = new StringFormat();
format.LineAlignment = StringAlignment.Center;
e.Graphics.DrawString(fileNameOnly, e.Font, new SolidBrush(e.ForeColor),
fileNameRec, format);
//bounds.Left + imageSize.Width + 3, bounds.Top + 2);
}
base.OnDrawItem(e);
}
Remark: In .NET 2.0, you can extract a file icon by using the method: Icon.ExtractAssociatedIcon(string filePath);
But this method knows to extract only file icons. The extractor uses API calls, but I will not go there, you can use it 'as-is' or learn more on a related article on that class on the CodeProject. Sixth Step - Handling File/Directory selectionFile/Directory selection occurs when an item is double-clicked. This is obtained by overriding the protected override void OnMouseDoubleClick(System.Windows.Forms.MouseEventArgs e)
{
string selectedFile = _directoryName + SelectedItem.ToString();
// .. ---> go back one level
if (selectedFile.EndsWith(".."))
{
// Removes the \ in the end, so that the parent will return the real parent.
if (_directoryName.EndsWith("\\"))
_directoryName = _directoryName.Remove(_directoryName.Length - 1, 1);
_directoryName = Directory.GetParent(_directoryName).FullName;
PopulatingItems();
}
// go inside the directory
else if (Directory.Exists(selectedFile))
{
_directoryName = selectedFile + "\\";
PopulatingItems();
}
else
{
OnFileSelected(new FileSelectEventArgs(selectedFile));
}
base.OnMouseDoubleClick(e);
}
ConclusionThe control described here is just a start, you can add features to it such as - deleting the file when clicking on the Del key, applying context menu commands such as create new folder; the sky is the limit. The class is light and simple for the purpose - for you to dive in quickly to the code and customize it to your exact needs. Use it well… | ||||||||||||||||||||