Solved

# TreeView Control

Posted on 2003-02-26
Medium Priority
292 Views
I'm working with the treeview control and use this procedure to move the nodes within the treeview:

Private Sub MoveNode(tvw As TreeView, nodX As Node, Direction As String)
Dim nodN As Node
Dim strKey As String
Dim AntKey As String
Dim ActKey As String

With tvw
Select Case Direction
Case "UP"
If Not nodX.Previous Is Nothing Then
Set nodN = .Nodes.Add(nodX.Previous, tvwPrevious, , nodX.Text)
Else
Exit Sub
End If
Case "DOWN"
If Not nodX.Next Is Nothing Then
Set nodN = .Nodes.Add(nodX.Next, tvwNext, , nodX.Text)
Else
Exit Sub
End If
End Select

nodN.Selected = True

If nodX.Children <> 0 Then
GetChildren tvw, nodX, nodN
End If

strKey = nodX.Key
.Nodes.Remove nodX.Index
Set nodX = Nothing
nodN.Key = strKey
End With

End Sub

The problem is that I want to know the actual node Index once I move it.  By example, I have a treeview control with one parent and three children nodes.  I use the function MoveNode() and move the node number 3 (index = 3) to the first position, but it still have the index = 3, when the value that I need is 1, and the second node the value = 2 and the third = 3, and so on.

Could anyone help me!!!!

0
Question by:alexl22
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 2

Accepted Solution

CD-Softy earned 100 total points
ID: 8028653
I don't think that is possible. Once a Node object is created and added to the Nodes collection, it is assigned the next available Index number, which exists for the life span of that that node. This can be prooven by setting the Sorted property to True or False, and it won't change the Nodes' index value. I think the approach to take would be to remove all the sibling nodes and then re-add then in the new order you want.

This is similar to the techniqued used to 'move' items up and down a listbox - by removing them first, then re-adding them.
0

LVL 1

Assisted Solution

sazhagianambi earned 100 total points
ID: 8031159
In Tree View,

If U Rearrange The Childs For One Parent, Then Definetely Those Child Node's Index are in Placing Order Only.

ie, If Root Have Childs Ch1,Ch2,Ch3 as One By One. Then Index Valus are 2,3,4.

If U Change these as Ch2,Ch3,Ch1 Then also Index Valus are
Likely 2,3,4.

Suppose U Change make any one as a SubNode To another Node then Index Value is assigned inthe Order of,
Root,
ChildNodes,
SubNodesToChildNodes.

So If You Move around within One Parent U can get Index as like u want.

Try Out This Code For More Clarity,

Make One TreeView into the Form

Place One Button ,
Private Sub cmdIndex_Click()
Dim i As Node
j = 1
For Each i In TreeView1.Nodes
MsgBox "Text= " & TreeView1.Nodes(j).Text & "         Index= " & TreeView1.Nodes(j).Index
j = j + 1
Next
End Sub

This Code used to Find out the Index Valus Of the Tree. Try This after u Rearrange Your Nodes.

Cheers,
Nambi

0

Expert Comment

ID: 8901066
alexl22:
Experts: Post your closing recommendations!  Who deserves points here?
0

LVL 49

Expert Comment

ID: 8983452
Moderator, my recommended disposition is:

Split points between: CD-Softy and sazhagianambi

DanRollins -- EE database cleanup volunteer
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
###### Suggested Courses
Course of the Month9 days, 13 hours left to enroll