Fill a treeview

  • Thread starter Thread starter sfauchille
  • Start date Start date
S

sfauchille

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


and i want it like this

|A------B-----C-----D------Var1
| | |--Var2
| |--A---- Var1
|B------A------C----Var2
|--C------D----Var1


Someone have an idea?

It's like having a list of file with the fullpath in a text file and
that you want to create a hierarchy in a treeview

Thanks
 
You need to try a little...

Which part are you having trouble with? Splitting the string, creating the
nodes, coming up with an algorithm?
 
You need to try a little...

Which part are you having trouble with? Splitting the string, creating the
nodes, coming up with an algorithm?












- Afficher le texte des messages précédents -

My problem is not to add node if it already exists, and I have some
problem to find the algorithm!!!
 
If you have split the string into words [], you would have a loop like such:

dim wordindex as integer = 0
dim treenodes as TreeNodeCollection = TreeView1.Nodes()

while (wordindex < words.length)
dim found as boolean = false

' find the word in the current level of nodes
for each (TreeNode tn in treenodes)
if tn.text = words [wordindex] then
treenodes = tn.Nodes()
found = true
break
endif
next

' did not find the node so add a new one
if (not found) then
treenodes.add(new TreeNode(words[wordindex]))
treenodes = treenodes.nodes ' this will be empty
endif
wordindex = wordindex + 1
end while
 
Note, I didn't type this in to Visual Studio, so there are likely typing
errors, but you should get the idea.

Family Tree Mike said:
If you have split the string into words [], you would have a loop like such:

dim wordindex as integer = 0
dim treenodes as TreeNodeCollection = TreeView1.Nodes()

while (wordindex < words.length)
dim found as boolean = false

' find the word in the current level of nodes
for each (TreeNode tn in treenodes)
if tn.text = words [wordindex] then
treenodes = tn.Nodes()
found = true
break
endif
next

' did not find the node so add a new one
if (not found) then
treenodes.add(new TreeNode(words[wordindex]))
treenodes = treenodes.nodes ' this will be empty
endif
wordindex = wordindex + 1
end while


My problem is not to add node if it already exists, and I have some
problem to find the algorithm!!!
 
Note, I didn't type this in to Visual Studio, so there are likely typing
errors, but you should get the idea.



Family Tree Mike said:
If you have split the string into words [], you would have a loop like such:
dim wordindex as integer = 0
dim treenodes as TreeNodeCollection = TreeView1.Nodes()
while (wordindex < words.length)
dim found as boolean = false
' find the word in the current level of nodes
for each (TreeNode tn in treenodes)
if tn.text = words [wordindex] then
treenodes = tn.Nodes()
found = true
break
endif
next
' did not find the node so add a new one
if (not found) then
treenodes.add(new TreeNode(words[wordindex]))
treenodes = treenodes.nodes ' this will be empty
endif
wordindex = wordindex + 1
end while
"(e-mail address removed)" wrote:

- Afficher le texte des messages précédents -

Thank you for your help, I did nearly the same but i used
"treenode.find" function, but it's very slow, i have nearly 20000
lines in my text file.
I try to optimizise it..... Not easy
 
If you sort the strings on each item, so that your example is:

A.B.A.Var1
A.B.C.D.Var1
A.B.C.D.var2
B.A.C.var2
B.C.D.var3

Then you should be able to optimize by just looking at the last node under
the current node. I didn't make the assumption because your example did not
sort, but, if that is acceptable, then I would go for it.



Note, I didn't type this in to Visual Studio, so there are likely typing
errors, but you should get the idea.



Family Tree Mike said:
If you have split the string into words [], you would have a loop like such:
dim wordindex as integer = 0
dim treenodes as TreeNodeCollection = TreeView1.Nodes()
while (wordindex < words.length)
dim found as boolean = false
' find the word in the current level of nodes
for each (TreeNode tn in treenodes)
if tn.text = words [wordindex] then
treenodes = tn.Nodes()
found = true
break
endif
next
' did not find the node so add a new one
if (not found) then
treenodes.add(new TreeNode(words[wordindex]))
treenodes = treenodes.nodes ' this will be empty
endif
wordindex = wordindex + 1
end while
"(e-mail address removed)" wrote:
On 9 nov, 16:46, Family Tree Mike
You need to try a little...
Which part are you having trouble with? Splitting the string, creating the
nodes, coming up with an algorithm?
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
and i want it like this
|A------B-----C-----D------Var1
| | |--Var2
| |--A---- Var1
|B------A------C----Var2
|--C------D----Var1
Someone have an idea?
It's like having a list of file with the fullpath in a text file and
that you want to create a hierarchy in a treeview
Thanks- Masquer le texte des messages pricidents -
- Afficher le texte des messages pricidents -
My problem is not to add node if it already exists, and I have some
problem to find the algorithm!!!- Masquer le texte des messages pricidents -

- Afficher le texte des messages pricidents -

Thank you for your help, I did nearly the same but i used
"treenode.find" function, but it's very slow, i have nearly 20000
lines in my text file.
I try to optimizise it..... Not easy
 
it's very slow, i have nearly 20000 lines in my text file.

And is your user expected to view each and every one of these 20,000!?

I should think not!

When you load the form, scan through the file and add /only/ the root
nodes into the Tree:

- A
- B

etc ...

Then, when the user attempts to /expand/ one of those Nodes, rescan the
file (or whatever storage structure you loaded the file into) and add
/just/ the nodes immediately below the node they clicked on, e.g.:

- A
- B
- A
- C

Because every TreeNode has its own Nodes Collection, it's very easy to
make this routine generic enough that it will work anywhere in the Tree
and the user only loads (and, therefore, waits for) the entries that
they're actually interested in.

HTH,
Phill W.
 
And is your user expected to view each and every one of these 20,000!?

I should think not!

When you load the form, scan through the file and add /only/ the root
nodes into the Tree:

- A
- B

etc ...

Then, when the user attempts to /expand/ one of those Nodes, rescan the
file (or whatever storage structure you loaded the file into) and add
/just/ the nodes immediately below the node they clicked on, e.g.:

- A
- B
- A
- C

Because every TreeNode has its own Nodes Collection, it's very easy to
make this routine generic enough that it will work anywhere in the Tree
and the user only loads (and, therefore, waits for) the entries that
they're actually interested in.

HTH,
Phill W.

Yes it's true

What I have to do, is to fill a tree view and also, create an XML File
looking like this

<Root>A</Root>
<Child>B</Child>

<Root>A.B</Root>
<Child>C</Child>
<Child>A</Child>

<Root>A.B.C</Root>
<Var>Var1</Var>
<Var>Var2</Var>

<Root>A.B.A</Root>
<Var>Var1</Var>

<Root>B</Root>
<Child>A</Child>
<Child>C</Child>

<Root>B.A</Root>
<Child>C</Child>

<Root>B.C</Root>
<Child>D</Child>

<Root>B.A.C</Root>
<Var>Var2</Var>

<Root>B.C.D</Root>
<Var>Var3</Var>
 
Yes it's true

What I have to do, is to fill a tree view and also, create an XML File
looking like this

<snip Xml sample>

Or, perhaps, build the Xml file /first/, then load the Treeview from that!

/If/ I read your data correctly, your Xml should look something like this:

<root>
<A>
<B>
<A var1="var1" />
<C var1="var1" var2="var2" />
</B>
</A>
<B>
<A>
<C var2="var2" />
</A>
<C>
<D var3="var3" />
</C>
<B>
</root>

Load this into an XmlDocument:

Dim xDoc as new Xml.XMlDocument
xDoc.Load( "fileContainingXml" )

Then load all the root nodes into the Tree:

For Each eNode as XmlNode _
In xDoc.SelectNodes( "root/*" )
tv1.Nodes.Add( ...
Next

Then, when the user expands a node in the tree, construct the XPath
needed to get the relevant part of the document, /something/ like:

Dim sXpath as String _
= "root/" & tv1.SelectedNode.FullPath.Replace( "\", "/" )

For Each eNode as Xml.XmlNode _
In xDoc.SelectNodes( sXPath )
tv1.selectedNodes.Nodes.Add( ...
Next

HTH,
Phill W.
 
<snip Xml sample>

Or, perhaps, build the Xml file /first/, then load the Treeview from that!

/If/ I read your data correctly, your Xml should look something like this:

<root>
<A>
<B>
<A var1="var1" />
<C var1="var1" var2="var2" />
</B>
</A>
<B>
<A>
<C var2="var2" />
</A>
<C>
<D var3="var3" />
</C>
<B>
</root>

Load this into an XmlDocument:

Dim xDoc as new Xml.XMlDocument
xDoc.Load( "fileContainingXml" )

Then load all the root nodes into the Tree:

For Each eNode as XmlNode _
In xDoc.SelectNodes( "root/*" )
tv1.Nodes.Add( ...
Next

Then, when the user expands a node in the tree, construct the XPath
needed to get the relevant part of the document, /something/ like:

Dim sXpath as String _
= "root/" & tv1.SelectedNode.FullPath.Replace( "\", "/" )

For Each eNode as Xml.XmlNode _
In xDoc.SelectNodes( sXPath )
tv1.selectedNodes.Nodes.Add( ...
Next

HTH,
Phill W.

Thanks a lot this will help me to read the XML File, but a fist I have
to read 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


put it in a tree view

|A------B-----C-----D------Var1
| | |--Var2
| |--A---- Var1
|B------A------C----Var2
|--C------D----Var1


and after write an XML file with a specific format

<root>
<A>
<B>
<A var1="var1" />
<C var1="var1" var2="var2" />
</B>
</A>
<B>
<A>
<C var2="var2" />
</A>
<C>
<D var3="var3" />
</C>
<B>
</root>
 
Back
Top