473,387 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to fill a TreeView ?

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
4 7565
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: JohnnyB via DotNetMonster.com | last post by:
Hello, I dont know, if this has already been asked before, I'm sorry if so. VisualBasic: My problem (happens with TreeView, not with AxWebBrowser): treeview uses (no matter, if dock = left,...
42
by: lauren quantrell | last post by:
So many postings on not to use the treeview control, but nothing recently. Is it safe to swim there yet with Access 2000-Access 2003?
2
by: Ole Baba | last post by:
In Form1.cs I have defined the "InstituteAdministrator" class which contains ListView "listViewAddress". In a second file (FormTreeView.cs) I have the class FormTreeView with the method...
2
by: PawelR | last post by:
Hello Group, I have DataTable dt with 3 Columns (id, GroupName, ItemName). How group in TreeView items by GroupName? EX: myTable: id GroupName ItemName 1 Group1 ...
2
by: Adrien Reboisson | last post by:
I'm trying to build a basic DB explorer using C# & Visual Studio 2005. I installed SQL Server 2005 Express, created a blank project, dropped a TreeView, a ListView and a DataGridView : DB objects...
1
by: Luqman | last post by:
I am using Oracle Database Scott.Emp table with Parent/Child relation keys. How can I fill the ASP.Net 2.0 TreeView Control using Sql Data Source. As the TreeView just uses xmldatasource, is it...
8
by: sfauchille | last post by:
Hello, I have to fill a treeview with data in a text file text file A.B.C.D.Var1 A.B.C.D.var2 A.B.A.Var1 B.A.C.var2 B.C.D.var3
2
by: RP | last post by:
I have an Access Table with following columns: GID (auto number) PID number TreeID Text ItemName Text This table consists of records in the following manner: GID PID TreeID ...
0
by: mdevenney | last post by:
My company doesn't allow for the use of .pst files. So to archive email we have to select the email(s) and click save as or drag them to a folder. I want to write a c# app that will show your...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.