Click to See Complete Forum and Search --> : Expanding Tree View


azwildcat4ever
July 31st, 2003, 07:01 AM
I have a tree view in my application. I already have a sub which collapses the entire tree. I need a sub which expand all the codes in the tree view. I am fairly new to vb.. Help greatly appreciate.. please ask any questions u have.

here is the code to the Collapse sub

Private Sub RepExpTV_Click()

For k = 1 To TreeView1.Nodes.Count
TreeView1.Nodes(k).Expanded = False

Next k

End Sub

womalley
July 31st, 2003, 07:11 AM
Question:
Are you collapsing nodes on the server side or on the client side?
The reason that I ask is because there is a difference with how it would be handled.

Will

azwildcat4ever
July 31st, 2003, 07:14 AM
The database file is on my computer. Its more like a TreeView (TV) I am playing about it. I have a Report Menu under which is the following choice. -> Collapse all entries in TreeView. I need to have a choice which Expands the entire treeview.

Thank you

azwildcat4ever
July 31st, 2003, 07:15 AM
However when the application is used..ppl will go on the network to find the database file which is used to make the tree nodes

womalley
July 31st, 2003, 07:31 AM
Edited:
Sorry I did not understand the question.

womalley
July 31st, 2003, 07:43 AM
When the Menu option is clicked you need to fire a Server Side event that will load all the data for each node?

Just to make sure... Do you have AutoPostBack set to true?

Pinky98
July 31st, 2003, 08:44 AM
maybe I am missing something, but don't you just do the same thing:


Private Sub cmdExpand_Click()
For k = 1 To TreeView1.Nodes.Count
TreeView1.Nodes(k).Expanded = True
Next k
End Sub

womalley
July 31st, 2003, 10:23 AM
Originally posted by Pinky98
maybe I am missing something, but don't you just do the same thing:


Private Sub cmdExpand_Click()
For k = 1 To TreeView1.Nodes.Count
TreeView1.Nodes(k).Expanded = True
Next k
End Sub


I think what azwildcat4ever needs to do is a little more complex then that. Think of a Bill Of Material.
Node
|_______ Node
|______ Node
|________Node
|________Node
|________Node
|______Node
|______Node
|_______ Node

ect...
He would like to expand ALL the nodes from what I understand so you have to drill down through all the child elements for each node.

You will have to create a stored procedure that returns all children for each node then populate the tree from that. This will take a while to do tho, I would think about maybe scrapping this idea of expanding all the nodes. I like the idea of closing all nodes tho. very cool...

Will

azwildcat4ever
July 31st, 2003, 10:32 AM
here is the code i think for expanding only a selected node. because expanding the entire tree takes alot of time and might not be need. My code for expanding at one point of the tree works. It works if i specify the node in the actual code.for example Set nodes(1) = Treeview1.nodes(3) but it doesnt work when want it to go from the selected item

Private Sub RepExpTV_Click()
Dim nodes(1 To 100) As Object
Dim node As Object

If TreeView1.SelectedItem Is Nothing Then
Exit Sub
End If
Level = 1
Set nodes(1) = TreeView1.nodes(1).Child
While (Level > 0)
If nodes(Level) Is Nothing Then
Level = Level - 1
If Level = 1 Then Exit Sub
Set nodes(Level) = nodes(Level).Next
Else
Set node = nodes(Level)
node.Expanded = True
If node.Children > 0 Then
Level = Level + 1
Set nodes(Level) = node.Child
Else
Set nodes(Level) = nodes(Level).Next
End If
End If


Wend

womalley
July 31st, 2003, 10:47 AM
Originally posted by azwildcat4ever
here is the code i think for expanding only a selected node. because expanding the entire tree takes alot of time and might not be need. My code for expanding at one point of the tree works. It works if i specify the node in the actual code.for example Set nodes(1) = Treeview1.nodes(3) but it doesnt work when want it to go from the selected item

Private Sub RepExpTV_Click()
Dim nodes(1 To 100) As Object
Dim node As Object

If TreeView1.SelectedItem Is Nothing Then
Exit Sub
End If
Level = 1
Set nodes(1) = TreeView1.nodes(1).Child
While (Level > 0)
If nodes(Level) Is Nothing Then
Level = Level - 1
If Level = 1 Then Exit Sub
Set nodes(Level) = nodes(Level).Next
Else
Set node = nodes(Level)
node.Expanded = True
If node.Children > 0 Then
Level = Level + 1
Set nodes(Level) = node.Child
Else
Set nodes(Level) = nodes(Level).Next
End If
End If


Wend

I am guessing that you are using the SelectedIndexChange event to figure out what node was selected. I ran into a problem with this aswell. Try this. Make the first node in your tree empty so it is not seen by the user. Make sure that you set the selectednodeindex for the tree to 0. Now when you select a node in the tree the SelectedIndexChange event will fire and I think all will be well. Oh one quick question. is this menu done oncontext of the tree node or how is the menu being shown?

on the tree view control put the following
oncontextmenu="javascript: showMenu(this); return false;"

now here is the function
function showMenu(Item)
{

var NodeSelected = document.getElementById(Item.id).getTreeNode(event.treeNodeIndex);
var NodeLevel = new Number(event.treeNodeIndex)
var ParentNode = document.getElementById(Item.id).getTreeNode(event.treeNodeIndex).getParent();
var NodeType = "";
var TreeNodeIndex = 0;

TreeNodeIndex = new String(NodeSelected.getNodeIndex());
NodeType= new String(NodeSelected.getAttribute("type") );
NodeType= NodeType.toUpperCase();

}

Pinky98
July 31st, 2003, 10:48 AM
Okay, so then just implement a tree search algorithm which, instead of finding an element, expands the tree!

azwildcat4ever
July 31st, 2003, 10:54 AM
where would i made the changes.. i am doing the change from a menu item// NOT Within by clicking the tree

azwildcat4ever
July 31st, 2003, 10:58 AM
Is this the tree view control

Private Sub TreeView1_DblClick()

On Error GoTo fokop

If TreeView1.SelectedItem.Image = "Arrow" Then
TreeView1.StartLabelEdit
End If
fokop:

End Sub

womalley
July 31st, 2003, 11:16 AM
Originally posted by azwildcat4ever
where would i made the changes.. i am doing the change from a menu item// NOT Within by clicking the tree

So the event is onRightClick or OnContextMenu of the treeview node. In other words when you right click on a node you want to expand that node.

on the tree view control put the following
oncontextmenu="java script: showMenu(this); return false;"

now when the user right clicks on the treeview your menu will show up that part you have done... When the user clicks a menu option you get the NodeIndex that was selected from the following function

function showMenu(Item)
{

var NodeSelected = document.getElementById(Item.id).getTreeNode(event.treeNodeIndex);
var NodeLevel = new Number(event.treeNodeIndex)
var ParentNode = document.getElementById(Item.id).getTreeNode(event.treeNodeIndex).getParent();
var NodeType = "";
var TreeNodeIndex = 0;

TreeNodeIndex = new String(NodeSelected.getNodeIndex());
NodeType= new String(NodeSelected.getAttribute("type") );
NodeType= NodeType.toUpperCase();

}

You could even make the variables global by putting then outside the function

var NodeSelected = 0;
var NodeLevel = 0;
var ParentNode = 0;
var NodeType = "";
var TreeNodeIndex = 0;

function showMenu(Item)
{

this.NodeSelected = document.getElementById(Item.id).getTreeNode(event.treeNodeIndex);
this.NodeLevel = new Number(event.treeNodeIndex)
this.ParentNode = document.getElementById(Item.id).getTreeNode(event.treeNodeIndex).getParent();
this.NodeType = "";
this.TreeNodeIndex = 0;

this.TreeNodeIndex = new String(NodeSelected.getNodeIndex());
this.NodeType= new String(NodeSelected.getAttribute("type") );
this.NodeType= NodeType.toUpperCase();

}

Now when you click on your menu option you will know what the index of the node selected was. If you need to read this from a Server side function, then take the values that you need for that function and put them in a textbox or label. Make sure that visible is set to true or you will not be able to write to it from the client.
Once you have the values in the textbox or label you can read them from the server side function/method or sub. I would use a textbox just because it is easier.

will

azwildcat4ever
July 31st, 2003, 11:30 AM
when I right click on my node, I already have a pulldown menu which gives me the option to add items and delete and rename them too. How do I add this new function (of expanding that point on of the node). Thank very much