1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
|
public class FullPathsTreeview : System.Windows.Forms.Treeview
{
public FullPathsTreeview()
{
}
public void AddStringWithSeparator(string s, char separator, bool considereUNC)
{
bool addedUNC = false ;
string[] sNodes = s.Split(separator);
if(considereUNC)
{
if(s.StartsWith("\\\\"))
{
if(sNodes.Length > 0)
{
while(addedUNC == false)
{
for(int i = 0 ; i < sNodes.Length ; i++)
{
if(sNodes[i].Length > 0)
{
sNodes[i] = "\\\\" + sNodes[i] ;
addedUNC = true ;
break ;
}
}
}
}
}
}
TreeNodeCollection oNodes = this.Nodes;
FullPathConstructionTreeNode oNode = null ;
foreach (string sNode in sNodes)
{
if ((sNode.Length > 0))
{
oNode = FindOrCreateNode(oNodes, sNode, true);
oNodes = oNode.Nodes;
}
}
}
public void FillWithStringsWithSeparatorArray(string[] strings, char separator, bool considereUNC)
{
if (!(strings == null))
{
for (int i = 0; i < strings.Length; i++)
AddStringWithSeparator(strings[i], separator, considereUNC) ;
}
}
public bool SelectNodeByFullPath(string fullPath)
{
TreeNode tn = GetNodeByFullPath(fullPath) ;
if(tn == null)
return false ;
this.SelectedNode = tn ;
return true ;
}
public TreeNode GetNodeByFullPath(string fullPath)
{
// On va parcourir tous les noeuds de l'arbre
TreeNode tn = null ;
TreeNode tnFullPath = null ;
for (int i = 0; i < this.Nodes.Count; i++)
{
tn = this.Nodes[i] ;
tnFullPath = GetNodeByFullPath(tn,fullPath) ;
if(tnFullPath != null)
return tnFullPath ;
}
return null ;
}
private TreeNode GetNodeByFullPath(TreeNode tn, string fullPath)
{
// Si le fullPath est égal au path, seléctionner le noeud et sortir
if(tn.FullPath == fullPath)
return tn ;
// Autrement passer récursivement dans tous les noeuds enfants
else
{
TreeNode tnFullPath = null ;
for (int i = 0; i < tn.Nodes.Count; i++)
{
tnFullPath = GetNodeByFullPath(tn.Nodes[i],fullPath) ;
if(tnFullPath != null)
return tnFullPath ;
}
}
return null ;
}
private FullPathConstructionTreeNode FindOrCreateNode(TreeNodeCollection oNodes,
string sNode, bool create)
{
bool found = false ;
FullPathConstructionTreeNode tn = null ;
foreach (FullPathConstructionTreeNode oNode in oNodes)
{
tn = oNode ;
if ((tn.Text == sNode))
{
found = true;
break;
}
}
if (!found)
{
if(create == true)
{
tn = new FullPathConstructionTreeNode() ;
tn.Text = sNode ;
oNodes.Add(tn);
tn.FullPathConstruction = tn.FullPath ;
}
}
return tn;
}
}
public class FullPathConstructionTreeNode : System.Windows.Forms.TreeNode
{
private string fullPathConstruction = "" ;
public FullPathConstructionTreeNode()
{
}
public string FullPathConstruction
{
get { return this.fullPathConstruction; }
set { this.fullPathConstruction = value; }
}
} |
Partager