TreeNodeCollection & TreeNode not defined

P

Patrick.O.Ige

I have this code below.
All its suppose to do is to EXPAND / COLLAPSE ALL of my treeview.
But when i use ASP.NET WEbmatrix it all works fine ..

But with VS.NET is says TreeNodeCollection not defined
and TreeNode not defined!!!!!!!!!!!!!!arg!!!!

How am i suppose to define it in VS.NET..
OR Does anybody another way of doing it!!
Any help appreciated!


Private Sub expandAll(ByVal sender As Object, ByVal e As EventArgs)
'Call the recursive function with the rootnode collection and a
boolean value indication to expand the nodes.
expandCollapseAllTreeviewNodes(Me.tvFamilyTree.Nodes, True)
End Sub

Private Sub collapseAll(ByVal sender As Object, ByVal e As EventArgs)
'Call the recursive function with the rootnode collection and a
boolean value indication to collapse the nodes.
expandCollapseAllTreeviewNodes(Me.tvFamilyTree.Nodes, False)
End Sub

'Recursive function.
Private Sub expandCollapseAllTreeviewNodes(ByVal nodes As
TreeNodeCollection, ByVal expand As Boolean)
Dim node As TreeNode

'Loop through each node of the collection and expand it.
For Each node In nodes
node.Expanded = expand

'Call the recursive function from itself, just the subnode
collection (this will ensure all nodes either are collapsed or expanded).
expandCollapseAllTreeviewNodes(node.Nodes, expand)

Next
End Sub

ASP.NET
 
M

Mohamed El Ashmawy

Hello Patrick,
I tried the scenario as follows and it worked.
I added a tree view class to my web page in vs.net 2003 and created nodes
in it.
I added 2 buttons and inserted the code you sent in your e-mail (changed
Me.tvFamilyTree.Nodes to Me.TreeView1.Nodes)


I ran the project and pressed the collapse and exapnd buttons. Things
worked as expected and exapnsion and collapsing worked fine.


Is there a special case that you are having in your treeview?
Are the nodes created dynamically? I created my nodes statically here?

Regards
Mohamed El Ashmawy
MEA Developer Support Center
ITWorx on behalf of Microsoft EMEA GTSC
 
P

Patrick.O.Ige

thx Mohammed.
My treeView is populsted using XML
And my code works well in ASP.NET WEMATRIX
Only when i use Visual Studio it says :- TreeNodeCollection and TreeNode
are not defined.
How should i define that..
Can you just post your full code i guess it will help me!
Thanks!!
 
P

Patrick.O.Ige

thx Mohammed.
My treeView is populsted using XML
And my code works well in ASP.NET WEMATRIX
Only when i use Visual Studio it says :- TreeNodeCollection and TreeNode
are not defined.
How should i define that..
Can you just post your full code i guess it will help me!
Thanks!!
 
P

Patrick.O.Ige

Thx Mohammed....
The Expand Collapse is working good now.
But i don't know whether u noticed someting funny about it.
When u EXPAND the Nodes and then select a node and then Press Collapse it
gives Error:-

"SelectedNodeIndex cannot be set to an undisplayed node (all parents of a
selected node must be expanded)"

Let me kow how you go!!!
 
M

Mohamed El Ashmawy

Hello Patrick,
I am sorry for the delay in my reply.
I am happy that the exapnd/collapse problem is over.
As for the error you get. It means that you are trying to set the selection
for a node that is under collapsed node (i.e. the node you are selecting is
actually not apparent on the screen).
Do you have code in your pageload event that selects a specific node? If
so, then you need to check that all the node hierarchy (from that node to
the root) as expanded. If not, then don't select the node.

The problem you described is not apparent on my side because I don't have
any code that selects a node.
The code I am using is taken from the code you posted and here it is:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
expandCollapseAllTreeviewNodes(Me.TreeView1.Nodes, True)

End Sub


Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
expandCollapseAllTreeviewNodes(Me.TreeView1.Nodes, False)
End Sub

Private Sub expandCollapseAllTreeviewNodes(ByVal nodes As
TreeNodeCollection, ByVal expand As Boolean)
Dim node As TreeNode

'Loop through each node of the collection and expand it.
For Each node In nodes
node.Expanded = expand

'Call the recursive function from itself, just the subnode
collection (this will ensure all nodes either are collapsed or expanded).
expandCollapseAllTreeviewNodes(node.Nodes, expand)

Next
End Sub


As for the treeview contents, they are all statically added using the
VS.Net IDE

Regards
Mohamed El Ashmawy
MEA Developer Support Center
ITWorx on behalf of Microsoft EMEA GTSC
 
P

Patrick.O.Ige

Thx for the reply mohammed!
I will check my code again and get back to you...
What my question is again is that when i collapse why does it collapse
totally..
I want it to collpapse to top nodes only and cot collapse to the root node.
Thx
Patrick
 
N

naija naija

Hi Mohamed El Ashmawy,
Thx for the response!
I have looked through the code again but i still get the same error(I
get the error only when i select a node and i click to collapse!And
again when i select ROOT it collapse with no error but IT COLLAPSE
TOTALLY TO THE ROOT LEVEL):-

"The node at index 0.1 is not currently displayed in the tree.
SelectedNodeIndex cannot be set to an undisplayed node (all parents of a
selected node must be expanded)."

And i don't have any code in the Page_Load.
the code 'm using looks like this:-

Private Sub expandCollapseAllTreeviewNodes(ByVal nodes As
TreeNodeCollection, ByVal expand As Boolean)
Dim node As TreeNode

'Loop through each node of the collection and expand it.
For Each node In nodes
node.Expanded = expand

'Call the recursive function from itself, just the subnode
collection (this will ensure all nodes either are collapsed or
expanded).
expandCollapseAllTreeviewNodes(node.Nodes, expand)

Next
End Sub


Sub Button2_Click(ByVal sender As Object, ByVal e As
System.EventArgs)
Me.expandCollapseAllTreeviewNodes(tvFamilyTree.Nodes, False)
End Sub 'Button2_Click

Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs)
Me.expandCollapseAllTreeviewNodes(tvFamilyTree.Nodes, True)
End Sub 'Button2_Click

Thx Mohammed and let me read from you.
 
M

Mohamed El Ashmawy

Hello Patrick,
The function that you have "expandCollapseAllTreeviewNodes" expands or
collapses all the nodes. This function iterates through all the nodes and
collapses them all. i.e. This is the behavior expected of your code.
If you want to collapse the selected node only you should write code like
this one:
Dim node As TreeNode =
Me.TreeView1.GetNodeFromIndex(Me.TreeView1.SelectedNodeIndex)
node.Expanded = False

Since your code collapses all node to the root. If you have a node
selected, then this would give the error you get.
Thus, you should specify the needed behavior and re-implement the collapse
method according to your requirements.

Regards
Mohamed El Ashmawy
MEA Developer Support Center
ITWorx on behalf of Microsoft EMEA GTSC
 
N

naija naija

Mohamed El Ashmawy,
Thx for the reply.
What i just want is just for the Node to expand all the nodes and
Collapse(Not to the root but to the Sub Nodes)
Can you pls POST me the full code or forward me the resources to do
this!
Thx alot!
 
M

Mohamed El Ashmawy

Dear Patrick,
I am not sure I understand your requirement totally, but this code:
Dim node As TreeNode =
Me.TreeView1.GetNodeFromIndex(Me.TreeView1.SelectedNodeIndex)
node.Expanded = False

would collapse the selected node. i.e. if you select a node and put this
code in the event handler for a button, then the selected node will be
collapsed (all its children will not bi displayed).
Is that what you need?

Regards
Mohamed El Ashmawy
MEA Developer Support Center
ITWorx on behalf of Microsoft EMEA GTSC
 
P

Patrick.O.Ige

Yes thats what i want but where is the code supposed to be placed?
I explained to you that i just want it to EXPAND totally and collapse to
nodes(Not totally to the ROOT).With my code below just specify how i should
do it!
Thx (My code below)

My code is as follows:-
Private Sub expandCollapseAllTreeviewNodes(ByVal nodes As
TreeNodeCollection, ByVal expand As Boolean)
Dim node As TreeNode
'Loop through each node of the collection and expand it.
For Each node In nodes
If expand Then
node.Expanded = True
expandCollapseAllTreeviewNodes(node.Nodes, expand)
Else
node.Expanded = False
expandCollapseAllTreeviewNodes(node.Nodes, expand)
End If

Next
End Sub

Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Me.expandCollapseAllTreeviewNodes(tvFamilyTree.Nodes, False)
End Sub 'Button2_Click

Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Me.expandCollapseAllTreeviewNodes(tvFamilyTree.Nodes, True)
End Sub 'Button2_Click
 
M

Mohamed El Ashmawy

Hello Patrick,
If you want to collapse the selected node when button2 is clicked, then you
should put inside its click event:
Dim node As TreeNode =
Me.TreeView1.GetNodeFromIndex(Me.TreeView1.SelectedNodeIndex)
node.Expanded = False

instead of :
Me.expandCollapseAllTreeviewNodes(tvFamilyTree.Nodes, False)


i.e. your code would be as follows:


Private Sub expandCollapseAllTreeviewNodes(ByVal nodes As
TreeNodeCollection, ByVal expand As Boolean)
Dim node As TreeNode
'Loop through each node of the collection and expand it.
For Each node In nodes
If expand Then
node.Expanded = True
expandCollapseAllTreeviewNodes(node.Nodes, expand)
Else
node.Expanded = False
expandCollapseAllTreeviewNodes(node.Nodes, expand)
End If

Next
End Sub

'This is what I changed
Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim node As TreeNode =
Me.TreeView1.GetNodeFromIndex(Me.TreeView1.SelectedNodeIndex)
node.Expanded = False
End Sub 'Button2_Click

Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Me.expandCollapseAllTreeviewNodes(tvFamilyTree.Nodes, True)
End Sub 'Button2_Click


Regards
Mohamed El Ashmawy
MEA Developer Support Center
ITWorx on behalf of Microsoft EMEA GTSC
 
P

Patrick.O.Ige

Hi Mohammed!
Thx for the explanation!.
Patrick
** If i have more Questions i'll be here**
 
M

Mohamed El Ashmawy

Anytime, :)
If you have any further questions or problems, plesae do not hesitate to
post them

Regards
Mohamed El Ashmawy
MEA Developer Support Center
ITWorx on behalf of Microsoft EMEA GTSC
 
S

sjbeskur

Not sure if this is what you're looking for but perhaps this can help
with the
"SelectedNodeIndex cannot be set to an undisplayed..." error


private void ExpandFromRoot(string nodeID){
string[] nodeIDs = nodeID.Split(new char[] {'.'});
string tmpId = "";
foreach(string id in nodeIDs){
tmpId += id;
treeView.GetNodeFromIndex(tmpId).Expanded = true;
tmpId += ".";
}
}

Regards,
S Beskur
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,222
Members
46,810
Latest member
Kassie0918

Latest Threads

Top