Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Dynamic creation of nodes in a treeview
Message
 
To
All
General information
Forum:
Visual FoxPro
Category:
ActiveX controls in VFP
Title:
Dynamic creation of nodes in a treeview
Miscellaneous
Thread ID:
00599825
Message ID:
00599825
Views:
56
Well, I started typing this rather long question and in the process solved my problem. I decided to post it anyway in case someone else needed the answer. See the bold lines in the code at the bottom for the solution.

-----

I am dynamically creating the nodes for a treeview in the expand event. When each node is first created, I place a "dummy" node as it's child. When that node is expanded, I remove the dummy, and add all the new child nodes.

My problem is the treeview doesn't move the nodes to the top of the display when I add child nodes. For example: If a node is at the bottom of the treeview and I click on the + to expand, it should scroll the treeview so the node is at the very top, but it just stays where it was when expanded.

It's not happening because the expand happens *before* the code in the expand event runs. The treeview thinks the only child is the "dummy" node, scrolls for that child, and runs the expand code, which removes the dummy and adds many more children.

To test this place a treeview on a form. Change the LineStyle: property to "1 - tvwRootLines". Add the following code to the treeview:
<b>INIT method:</b>** Add the root node
This.nodes.Add(,0 , 'ROOT-KEY-0', 'Test', , )
This.nodes.Add('ROOT-KEY-0', 4, 'DUMMY-ROOT-KEY', 'DUMMY', , )

** Add a property to store the last used level of the treeview so we can
** build a unique key for the expample.
Thisform.AddProperty("treeViewLevel", 1)

<b>Expand event:</b>
*** ActiveX Control Event ***
Lparameters Node

** Increment the level for the node keys
Local tvLevel
tvLevel = Thisform.treeViewLevel + 1
Thisform.treeViewLevel = tvLevel

** Remove the dummy if it's there.
If Node.Child.Text == 'DUMMY'
	This.Nodes.Remove(Node.Child.Index)
Endif

** Add some kids
Local i, newKey
For i = 1 To 10
	newKey = Transform(tvLevel) + "-" + Transform(i)
	This.Nodes.Add(Node.Key, 4, newKey, newKey, , )
	This.Nodes.Add(newKey, 4, 'DUMMY' + "-" + newKey, 'DUMMY', , )
Next


<b>** Here is the fix to the scroll problem.</b>

** Force to scroll properly.
** This makes the bottommost child node visible, which will force
** the treeview to scroll if the node is off the screen.
<b>Node.lastsibling.EnsureVisible</b>
** now make the parent node (the one that was expanded) visible.
<b>Node.EnsureVisible</b>
Roi
'MCP' Visual FoxPro

In Rome, there was a poem.
About a dog, who found two bone.
He lick the one, he lick the other.
He went pyscho, he drop dead!
Reply
Map
View

Click here to load this message in the networking platform