static void

DirExplorer

An Explorer-type view with treeview and listview. Uses:

Other classes used by the above:

using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
 
namespace Library.Forms
{
    [ToolboxBitmap(typeof(ListView))]
    [DefaultEvent("DirectoryChanged"), Description("A treeview/listview for directories")]
    public partial class DirExplorer : UserControl
    {
        public event EventHandler<DirectoryClickedEventArgs> DirectoryChanged;
        public event EventHandler<DirListView.FileSelectedEventArgs> Selected;
 
        private DirectoryInfo _currentDirectory;
        private ImageList _imageList = null;
        private int _splitterDistance = 190;
 
        public DirExplorer()
        {
            InitializeComponent();
 
            _currentDirectory = new DirectoryInfo(dirListView1.CurrentDirectory);
            dirTreeView1.CurrentDirectoryInfo = _currentDirectory;
 
            dirTreeView1.NodeClicked += dirTreeView1_NodeClicked;
            dirListView1.DirectoryChanged += dirListView1_DirectoryChanged;
            dirListView1.Selected += dirListView1_Selected;
        }
 
        void dirListView1_Selected(object sender, DirListView.FileSelectedEventArgs e)
        {
            //raise event
            EventHandler<DirListView.FileSelectedEventArgs> handler = Selected;
            if (handler != null)
                handler(this, e);
        }
 
        private void dirTreeView1_NodeClicked(object sender, DirectoryClickedEventArgs e)
        {
            _currentDirectory = e.Directory;
            dirListView1.SetDirectory(_currentDirectory);
            //raise event
            EventHandler<DirectoryClickedEventArgs> handler = DirectoryChanged;
            if (handler != null)
                handler(this, e);
        }
 
        private void dirListView1_DirectoryChanged(object sender, DirectoryClickedEventArgs e)
        {
            _currentDirectory = e.Directory;
            dirTreeView1.CurrentDirectoryInfo = e.Directory;
            //raise event
            EventHandler<DirectoryClickedEventArgs> handler = DirectoryChanged;
            if (handler != null)
                handler(this, e);
        }
 
        [Browsable(true), Description("The pixel distance of the splitter from the left edge"), Category("Layout")]
        public int SplitterDistance
        {
            get { return _splitterDistance; }
            set
            {
                _splitterDistance = value;
                splitContainer1.SplitterDistance = value;
            }
        }
 
        [Browsable(true), Description("The imagelist used by the treenodes and listview"), Category("Behavior")]
        public ImageList ImageList
        {
            get { return _imageList; }
            set
            {
                _imageList = value;
                dirTreeView1.ImageList = _imageList;
                dirListView1.SmallImageList = _imageList;
            }
        }
 
        [Browsable(true), Description("The current directory"), Category("Behavior")]
        public string CurrentDirectory
        {
            get
            {
                return _currentDirectory == null ? string.Empty : _currentDirectory.FullName;
            }
            set
            {
                if (string.IsNullOrEmpty(value))
                    value = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                DirectoryInfo dir = new DirectoryInfo(value);
                if (!dir.Exists) throw new ArgumentException("Directory does not exist");
                dirListView1.SetDirectory(_currentDirectory);
                dirTreeView1.CurrentDirectoryInfo = _currentDirectory;
            }
        }
 
        #region Designer
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;
 
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }
 
        #region Component Designer generated code
 
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.splitContainer1 = new System.Windows.Forms.SplitContainer();
            this.dirTreeView1 = new Library.Forms.DirTreeView();
            this.dirListView1 = new Library.Forms.DirListView();
            this.splitContainer1.Panel1.SuspendLayout();
            this.splitContainer1.Panel2.SuspendLayout();
            this.splitContainer1.SuspendLayout();
            this.SuspendLayout();
            //
            // splitContainer1
            //
            this.splitContainer1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.splitContainer1.Location = new System.Drawing.Point(0, 0);
            this.splitContainer1.Name = "splitContainer1";
            //
            // splitContainer1.Panel1
            //
            this.splitContainer1.Panel1.Controls.Add(this.dirTreeView1);
            //
            // splitContainer1.Panel2
            //
            this.splitContainer1.Panel2.Controls.Add(this.dirListView1);
            this.splitContainer1.Size = new System.Drawing.Size(425, 150);
            this.splitContainer1.SplitterDistance = 190;
            this.splitContainer1.TabIndex = 0;
            //
            // dirTreeView1
            //
            this.dirTreeView1.CurrentDirectory = null;
            this.dirTreeView1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.dirTreeView1.ImageList = null;
            this.dirTreeView1.Location = new System.Drawing.Point(0, 0);
            this.dirTreeView1.Name = "dirTreeView1";
            this.dirTreeView1.Size = new System.Drawing.Size(150, 150);
            this.dirTreeView1.TabIndex = 0;
            //
            // dirListView1
            //
            this.dirListView1.CurrentDirectory = null;
            this.dirListView1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.dirListView1.Location = new System.Drawing.Point(0, 0);
            this.dirListView1.Name = "dirListView1";
            this.dirListView1.Size = new System.Drawing.Size(271, 150);
            this.dirListView1.SmallImageList = null;
            this.dirListView1.TabIndex = 0;
            //
            // DirExplorer
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Controls.Add(this.splitContainer1);
            this.Name = "DirExplorer";
            this.Size = new System.Drawing.Size(425, 150);
            this.splitContainer1.Panel1.ResumeLayout(false);
            this.splitContainer1.Panel2.ResumeLayout(false);
            this.splitContainer1.ResumeLayout(false);
            this.ResumeLayout(false);
 
        }
 
        #endregion
 
        private System.Windows.Forms.SplitContainer splitContainer1;
        private Library.Forms.DirTreeView dirTreeView1;
        private Library.Forms.DirListView dirListView1;
        #endregion
    }
}