By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,127 Members | 1,127 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,127 IT Pros & Developers. It's quick & easy.

How to fill a TreeView ?

P: n/a
Hi
I've got objects in an sorted ArrayList like:
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
I should fill this objects in a TreeView.
After the filling the TreeView should shows expanded like:
P:\
DOCS
- i386
- ASMS
- 1000
- MSFT
- WINDOWS
GDIPLUS
- 5100
- MSFT
- WINDOWS
- SYSTEM
DEFAULT
COMPDATA
- DRW
- 1031
- 1033
LANG
SUPPORT
- TOOLS

How can I solve this problem?

TIA
Ian
Nov 16 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Refer the URL
http://www.microsoft.com/india/msdn/articles/198.aspx

Regards,
Amal

"Ian Powell" wrote:
Hi
I've got objects in an sorted ArrayList like:
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
I should fill this objects in a TreeView.
After the filling the TreeView should shows expanded like:
P:\
DOCS
- i386
- ASMS
- 1000
- MSFT
- WINDOWS
GDIPLUS
- 5100
- MSFT
- WINDOWS
- SYSTEM
DEFAULT
COMPDATA
- DRW
- 1031
- 1033
LANG
SUPPORT
- TOOLS

How can I solve this problem?

TIA
Ian

Nov 16 '05 #2

P: n/a
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\DEFAUL T", "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)"
<AM****************************@discussions.micros oft.com> wrote in message
news:77**********************************@microsof t.com...
Refer the URL
http://www.microsoft.com/india/msdn/articles/198.aspx

Regards,
Amal

"Ian Powell" wrote:
Hi
I've got objects in an sorted ArrayList like:
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
I should fill this objects in a TreeView.
After the filling the TreeView should shows expanded like:
P:\
DOCS
- i386
- ASMS
- 1000
- MSFT
- WINDOWS
GDIPLUS
- 5100
- MSFT
- WINDOWS
- SYSTEM
DEFAULT
COMPDATA
- DRW
- 1031
- 1033
LANG
SUPPORT
- TOOLS

How can I solve this problem?

TIA
Ian

Nov 16 '05 #3

P: n/a
Hi Ian, your problem should be relatively easy.
Ideally you will need to write some code which returns a seperated directory
path for each path string.
I would return an array ...
e.g. from P:\\i386\ASMS\5100
you would have a[0] = i386, a[1] = asms, a[2] = 5100

from that array you could use it to build and navigate the treeview for that
directory path.
You would recurse thru each item of the array and if not already there then
you would add it.

With regards to the TreeView, you will be using the following methods and
properties

treeViewName.Nodes.Add(node) //to add a directory if tree is empty -will
only happen once
node.Nodes.Add(node) //to add a directory if parent has been found in
tree and navigated to
node.Parent //to assign a reference to a parent node of a node (for use
in navigating up the tree) -note that the reference may be null if a parent
doesnot exist
node.Nodes[x] //to assign a reference to a child node within the node's
node collection -to find is node for directory has already been added
node.Nodes.Count //for use with above

etc.etc.

It really is quite simple and you will probably be able to write some very
efficient code.

Hope this helps!

Br,

Mark.
"Ian Powell" <ju**@de7.co.uk> wrote in message
news:V%************@newsfe3-gui.ntli.net...
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\DEFAUL T", "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)"
<AM****************************@discussions.micros oft.com> wrote in message news:77**********************************@microsof t.com...
Refer the URL
http://www.microsoft.com/india/msdn/articles/198.aspx

Regards,
Amal

"Ian Powell" wrote:
Hi
I've got objects in an sorted ArrayList like:
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
I should fill this objects in a TreeView.
After the filling the TreeView should shows expanded like:
P:\
DOCS
- i386
- ASMS
- 1000
- MSFT
- WINDOWS
GDIPLUS
- 5100
- MSFT
- WINDOWS
- SYSTEM
DEFAULT
COMPDATA
- DRW
- 1031
- 1033
LANG
SUPPORT
- TOOLS

How can I solve this problem?

TIA
Ian


Nov 16 '05 #4

P: n/a
Unfortunately this doesn't help.

This has been driving me crazy for awhile now.

Anyone smart enough to help me with the code needed to perform the task???

Thanks in advance

Ian
"Mark Broadbent" <no************@no-spam-please.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Hi Ian, your problem should be relatively easy.
Ideally you will need to write some code which returns a seperated directory path for each path string.
I would return an array ...
e.g. from P:\\i386\ASMS\5100
you would have a[0] = i386, a[1] = asms, a[2] = 5100

from that array you could use it to build and navigate the treeview for that directory path.
You would recurse thru each item of the array and if not already there then you would add it.

With regards to the TreeView, you will be using the following methods and
properties

treeViewName.Nodes.Add(node) //to add a directory if tree is empty -will only happen once
node.Nodes.Add(node) //to add a directory if parent has been found in
tree and navigated to
node.Parent //to assign a reference to a parent node of a node (for use
in navigating up the tree) -note that the reference may be null if a parent doesnot exist
node.Nodes[x] //to assign a reference to a child node within the node's
node collection -to find is node for directory has already been added
node.Nodes.Count //for use with above

etc.etc.

It really is quite simple and you will probably be able to write some very
efficient code.

Hope this helps!

Br,

Mark.

Nov 16 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.