Hi Amal,
Thanks but that is using a self referencing table.
I found an article written by Kathleen Dollard on 29-12-2001 which showed a
vb.net example as listed below.
----- Begin of snippet -----
Peter,
I think you are creating a special instance of Treeview that knows how to
load certain things, in this case at least an array of delimited strings.
So, I inherited from TreeView to do this. If you similarly inherit a class
from TreeView, and add this code, you should accomplish your objective in a
way that you can reuse in the future. The hard coded string is ugly, but is
assumed to be just a test scenario.
The test for an empty array is needed because this will be a property of the
control that you can set at design time. Since the control attempts to use
this data when it loads, you have to protect against it being empty. The
double slashes lead to the check for a zero length string.
I could not decide whether to make FindNode public, I probably would so
designed it with the optional AddNode flag.
Note that this results in unique entries and does not care what order the
original array strings are in.
If you have any problems, or see a better way to do this, I would be
interested in hearing about it.
Kathleen
(MVP-Visual Basic)
-------------------------------- Tested using
Dim aInput() As String = New String() {"P:\", "P:\\DOCS", "P:\\i386",
"P:\\i386\ASMS", "P:\\i386\ASMS\1000", "P:\\i386\ASMS\1000\MSFT",
"P:\\i386\ASMS\1000\MSFT\WINDOWS",
"P:\\i386\ASMS\1000\MSFT\WINDOWS\GDIPLUS", "P:\\i386\ASMS\5100",
"P:\\i386\ASMS\5100\MSFT", "P:\\i386\ASMS\5100\MSFT\WINDOWS",
"P:\\i386\ASMS\5100\MSFT\WINDOWS\SYSTEM",
"P:\\i386\ASMS\5100\MSFT\WINDOWS\SYSTEM\DEFAULT", "P:\\i386\COMPDATA",
"P:\\i386\DRW", "P:\\i386\DRW\1031", "P:\\i386\DRW\1033", "P:\\i386\LANG",
"P:\\i386\SUPPORT", "P:\\i386\SUPPORT\TOOLS"}
TreeViewPath.NodeStringArray = aInput
-------------------------------- Class code
Option Strict On
Public Class TreeViewPath
Inherits Windows.Forms.TreeView
Public Property NodeStringArray() As String()
Get
' You can cruise the nodes and recreate the string array if you
like
End Get
Set(ByVal Value As String())
If Not Value Is Nothing Then ' Value will be nothing on form load
Dim sNodes() As String
Dim sPath As String
Dim sNode As String
Dim oNodes As TreeNodeCollection
Dim oNode As TreeNode
For Each sPath In Value
sNodes = sPath.Split(CChar("\"))
oNodes = Me.Nodes
For Each sNode In sNodes
If sNode.Length > 0 Then
oNode = FindNode(oNodes, sNode, True)
oNodes = oNode.Nodes
End If
Next
Next
End If
End Set
End Property
Private Function FindNode(ByVal oNodes As TreeNodeCollection, ByVal sNode
As String, Optional ByVal bCreateNode As Boolean = False) As TreeNode
Dim bFound As Boolean
Dim oNode As TreeNode
bFound = False
For Each oNode In oNodes
If oNode.Text = sNode Then
bFound = True
Exit For
End If
Next
If Not bFound Then
If bCreateNode Then ' We need to add it
oNode = oNodes.Add(sNode)
Else
oNode = Nothing
End If
End If
Return oNode
End Function
#Region " Windows Form Designer generated code "
End Class
---- End of snippet -----
which seems to be exactly what I want, although I tried to convert this to
c# as shown below, but it doesn't work. c# code below. The code just adds
the nodes to the root node.
public class TreeViewPath : TreeView
{
public TreeViewPath()
{
//
// TODO: Add constructor logic here
//
}
public string[] NodeStringArray
{
// get
// {
// // You can cruise the nodes and recreate the string array if you like
// }
set
{
if (value != null) // Value will be nothing on form load
{
string[] sNodes;
TreeNodeCollection oNodes;
TreeNode oNode = null;
foreach (string sPath in value)
{
sNodes = sPath.Split('\\');
oNodes = this.Nodes;
foreach (string sNode in sNodes)
{
if (sNode.Length > 0)
{
oNode = FindNode(oNodes, sNode, true);
oNodes = oNode.Nodes;
}
}
}
}
}
}
private TreeNode FindNode(TreeNodeCollection oNodes, String sNode)
{
return FindNode(oNodes, sNode, false);
}
private TreeNode FindNode(TreeNodeCollection oNodes, String sNode, bool
bCreateNode)
{
bool bFound = false;
TreeNode onode = null;
foreach (TreeNode oNode in oNodes)
{
if (oNode.Text == sNode)
{
bFound = true;
break;
}
}
if (! bFound)
{
if (bCreateNode) // We need to add it
onode = oNodes.Add(sNode);
else
onode = null;
}
return onode;
}
}
Not sure where I went wrong.....
"AMALORPAVANATHAN YAGULASAMY (AMAL)"