SonicAudioTools/Source/CsbBuilder/Extensions.cs

87 lines
2.2 KiB
C#
Raw Normal View History

2017-01-26 21:40:46 +01:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace CsbBuilder
{
public static class Extensions
{
public static TreeNode CreateNodesByFullPath(this TreeView treeView, string path)
{
string fullPath = string.Empty;
TreeNode currentNode = null;
foreach (string node in path.Split('/'))
{
fullPath += node;
TreeNode treeNode = treeView.FindNodeByFullPath(fullPath);
bool notFound = treeNode == null;
if (notFound)
{
treeNode = new TreeNode(node);
treeNode.Name = treeNode.Text;
}
if (currentNode != null && notFound)
{
currentNode.Nodes.Add(treeNode);
}
else if (currentNode == null && notFound)
{
treeView.Nodes.Add(treeNode);
}
currentNode = treeNode;
fullPath += '/';
}
return currentNode;
}
public static TreeNode FindNodeByFullPath(this TreeNode treeNode, string path)
{
foreach (TreeNode node in treeNode.Nodes)
{
if (node.FullPath == path)
{
return node;
}
TreeNode childNode = node.FindNodeByFullPath(path);
if (childNode != null)
{
return childNode;
}
}
return null;
}
public static TreeNode FindNodeByFullPath(this TreeView treeView, string path)
{
foreach (TreeNode node in treeView.Nodes)
{
if (node.FullPath == path)
{
return node;
}
TreeNode childNode = node.FindNodeByFullPath(path);
if (childNode != null)
{
return childNode;
}
}
return null;
}
}
}