Click here to Skip to main content
15,891,828 members
Articles / Programming Languages / C#

Dual Pane File Manager

Rate me:
Please Sign up or sign in to vote.
4.64/5 (28 votes)
21 Dec 2010CPOL8 min read 140.7K   2.3K   119  
A dual pane file manager for Windows XP.
using System;
using System.Collections;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace JGTreeView
{
	public class JTreeViewBase : System.Windows.Forms.TreeView
	{
		public delegate void MessageSenderEventHandler(object sender, string Message, bool Error);
		public event MessageSenderEventHandler OnMessageSender;

		public void RaiseMessageSenderEventHandler(object sender, string Message, bool Error)
		{
			if (OnMessageSender != null)
			{
				OnMessageSender(sender, Message, Error);
			}
		}

		public JTreeViewBase()
			: base()
		{
			this.HideSelection = false;
		}

		private TreeNode DoGetNodeByName(string nodeName, bool searchAllChildren)
		{
			TreeNode[] tnFound = this.Nodes.Find(nodeName, searchAllChildren);

			if (tnFound.Length > 0)
				return tnFound[0];
			return null;
		}

		private TreeNode DoGetNodeByName(TreeNode tnStart, string nodeName, bool searchAllChildren)
		{
			if (tnStart != null)
			{
				tnStart.Collapse();
				tnStart.Expand();

				TreeNode[] tnFound = tnStart.Nodes.Find(nodeName, searchAllChildren);

				if (tnFound.Length > 0)
					return tnFound[0];
			}
			return null;
		}

		private TreeNode DoSetNodeToName(string nodeName, bool isFilePath, bool searchAllChildren)
		{
			if (nodeName == "")
				return null;

			this.BeginUpdate();

			TreeNode tnFind = null;

			string searchPath = "";

			//	Let's see if it already exists
			TreeNode[] tnArray = this.Nodes.Find(nodeName, true);
			if (tnArray.Length > 0)
				tnFind = tnArray[0];

			if (tnFind != null)
			{
				this.SelectedNode = tnFind;
				tnFind.Collapse();
				tnFind.Expand();
			}
			else
			{
				string[] nameArray = nodeName.Split(Path.DirectorySeparatorChar);

				foreach (string current in nameArray)
				{
					if (current != "")
					{
						searchPath = Path.Combine(searchPath, current);
						if (isFilePath && searchPath.Length == 2)
							searchPath += Path.DirectorySeparatorChar.ToString();
						tnFind = (TreeNode)GetNodeByName(searchPath, true);
						if (tnFind != null)
						{
							this.SelectedNode = tnFind;
							tnFind.Collapse();
							tnFind.Expand();
						}
					}
				}
			}

			this.EndUpdate();

			return tnFind;
		}

		private TreeNode DoSetNodeToName(TreeNode tnStart, string nodeName, bool isFilePath, bool searchAllChildren)
		{
			if (nodeName == "")
				return null;

			this.BeginUpdate();

			TreeNode tnFind = null;
			string searchPath = "";

			//	Let's see if it already exists
			TreeNode[] tnArray = this.Nodes.Find(nodeName, true);
			if (tnArray.Length > 0)
				tnFind = tnArray[0];

			if (tnFind != null)
			{
				this.SelectedNode = tnFind;
				tnFind.Collapse();
				tnFind.Expand();
			}
			else
			{
				string[] nameArray = nodeName.Split(Path.DirectorySeparatorChar);

				foreach (string current in nameArray)
				{
					if (current != "")
					{
						searchPath = Path.Combine(searchPath, current);
						if (isFilePath && searchPath.Length == 2)
							searchPath += Path.DirectorySeparatorChar.ToString();
						tnFind = (TreeNode)GetNodeByName(tnStart, searchPath, searchAllChildren);
						if (tnFind != null)
						{
							this.SelectedNode = tnFind;
							tnFind.Collapse();
							tnFind.Expand();
						}
					}
				}
			}

			this.EndUpdate();

			return tnFind;
		}

		
		/// <summary>
		/// Searches the complete TreeView for a TreeNode with the Name 'name'
		/// </summary>
		/// <param name="name">The Name of the TreeNode to search for</param>
		/// <param name="searchAllChildren">Flag to indicate if child nodes should be searched</param>
		/// <returns>The requested node or null if not found</returns>
		public TreeNode GetNodeByName(string name, bool searchAllChildren)
		{
			return DoGetNodeByName(name, searchAllChildren);
		}

		/// <summary>
		/// Searches the TreeNode for a child TreeNode with the Name 'name'
		/// </summary>
		/// <param name="tnStart">Starting TreeNode for the search</param>
		/// <param name="name">The Name of the TreeNode to search for</param>
		/// <param name="searchAllChildren">Flag to indicate if child nodes should be searched</param>
		/// <returns>The requested node or null if not found</returns>
		public TreeNode GetNodeByName(TreeNode tnStart, string name, bool searchAllChildren)
		{
			return DoGetNodeByName(tnStart, name, searchAllChildren);
		}

		/// <summary>
		/// Searches for the TreeNode with the Name 'name' and selects it if found
		/// </summary>
		/// <param name="name">The Name of the TreeNode to search for</param>
		/// <param name="isFilePath">Flag to indicate if it is a file path so that drive paths can be adjusted</param>
		/// <param name="searchAllChildren">Flag to indicate if child nodes should be searched</param>
		/// <returns>The requested node or null if not found</returns>
		public TreeNode SetNodeToName(string name, bool isFilePath, bool searchAllChildren)
		{
			return DoSetNodeToName(name, isFilePath, searchAllChildren);
		}

		/// <summary>
		/// Searches for the TreeNode with the Name 'name' and selects it if found
		/// </summary>
		/// <param name="tnStart">Starting TreeNode for the search</param>
		/// <param name="name">The Name of the TreeNode to search for</param>
		/// <param name="isFilePath">Flag to indicate if it is a file path so that drive paths can be adjusted</param>
		/// <param name="searchAllChildren">Flag to indicate if child nodes should be searched</param>
		/// <returns>The requested node or null if not found</returns>
		public TreeNode SetNodeToName(TreeNode tnStart, string name, bool isFilePath, bool searchAllChildren)
		{
			return DoSetNodeToName(tnStart, name, isFilePath, searchAllChildren);
		}

	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Retired
United Kingdom United Kingdom
I have been a keen hobbyist programmer since getting my first computer - a Vic 20 (you had to be able to write programs then since few programs were available and all were expensive).
Retired and now living in Pewsey, Wiltshire, where I spend (far too much of) my time writing computer programs to keep my mind active.

Comments and Discussions