|
-
Nov 8th, 1999, 03:33 PM
#1
Thread Starter
Hyperactive Member
I have a combobox called cboNavigate and a treeview called tvwTree. When I click nodes in the treeview the combobox displays the fullpath. Now I want to implement that the user can type in a treeview path in cboNavigate and press enter and that the treeview will jump to the right possition.
f.i I type: "Sales, Sales dossiers, Communications" (without quotes) and press enter the treeview jumps to the Communications node in the threeview. (The pathseparator is as you can see ", ")
Anybody knows how to do this ??
-
Nov 8th, 1999, 09:13 PM
#2
Sure thing:
Code:
Public Function SplitPathComponents(ByVal pPath As String, ByVal pSeparator As String) As Variant
Dim arrTemp() As String
Dim strTemp As String
Dim i As Integer
Do Until pPath = ""
If InStr(pPath, pSeparator) > 0 Then
strTemp = Left(pPath, InStr(pPath, pSeparator) - 1)
pPath = Mid(pPath, InStr(pPath, pSeparator) + 1)
Else
If pPath <> "" Then
strTemp = pPath
pPath = ""
End If
End If
ReDim Preserve arrTemp(i)
arrTemp(i) = Trim(strTemp)
i = i + 1
Loop
SplitPathComponents = arrTemp
End Function
Private Sub Combo1_KeyPress(KeyAscii As Integer)
Dim arrComp As Variant
Dim i As Integer
Dim iNode As Integer
Dim iIndex As Integer
Dim bFirstChild As Boolean
If KeyAscii = 13 Then
'This will split the path and put it in array
arrComp = SplitPathComponents(Combo1.Text, ",") 'You can specify any delimeter (separator)
With TreeView1
For i = 0 To UBound(arrComp)
If i = 0 Then
For iNode = 1 To .Nodes.Count
If UCase(.Nodes(iNode).Text) = UCase(arrComp(i)) Then
.Nodes(iNode).EnsureVisible
.SetFocus
.Nodes(iNode).Selected = True
Exit For
End If
Next
Else
bFirstChild = True
For iNode = 1 To .Nodes(i).Children
If bFirstChild Then
If UCase(.Nodes(i).Child.Text) = UCase(arrComp(i)) Then
.Nodes(i).Child.EnsureVisible
.SetFocus
.Nodes(i).Child.Selected = True
Exit For
bFirstChild = False
End If
Else
If UCase(.Nodes(i).Child.Next.Text) = UCase(arrComp(i)) Then
.Nodes(i).Child.EnsureVisible
.SetFocus
.Nodes(i).Child.Selected = True
Exit For
End If
End If
Next
End If
Next
End With
End If
End Sub
Just change the TreeView1 and Combo1 to use appropriate names.
Regards,
------------------
Serge
Software Developer
[email protected]
[email protected]
ICQ#: 51055819
[This message has been edited by Serge (edited 11-09-1999).]
-
Nov 8th, 1999, 11:51 PM
#3
Thread Starter
Hyperactive Member
Serge's code does not seem to work with multiple parents and childs. I've been debugging for a while now. It looks to me like SplitPathComponents is working fine.
There seems to be a problem with the comboboxe's keypress event.
Please help....
-
Nov 9th, 1999, 03:07 AM
#4
Thread Starter
Hyperactive Member
Ok, here's an example treeview:
Code:
Private Sub Form_Load()
Dim nodX As Node ' Declare Node variable.
Set nodX = TreeView1.Nodes.Add(, , "r", "Root")
Set nodX = TreeView1.Nodes.Add(, , "s", "Soot")
Set nodX = TreeView1.Nodes.Add("r", tvwChild, "child1", "Child 1")
Set nodX = TreeView1.Nodes.Add("r", tvwChild, "child2", "Child 2")
Set nodX = TreeView1.Nodes.Add("child1", tvwChild, "child3", "Child 3")
Set nodX = TreeView1.Nodes.Add("s", tvwChild, "child4", "Child 4")
End Sub
When I use your code the following nodes can't be reached by typing in the following strings in the combobox:
root, child 1, child 3 (Child 3 was not selected)
root, child 2 (Child 2 was not selected)
soot, child 4 (Child 4 was not selected)
-
Nov 9th, 1999, 12:58 PM
#5
This is strange. It should go through all items in the array, which means all the children for the previous elemt in the array. I've populated TreeView with many children of the children of the children and so on... And it looks like it's working OK. Let me know if you need anynore help.
Regards,
------------------
Serge
Software Developer
[email protected]
[email protected]
ICQ#: 51055819
-
Nov 9th, 1999, 02:55 PM
#6
Thread Starter
Hyperactive Member
plz everybody... All the code is here.. I hope someone can solve this...
-
Nov 9th, 1999, 07:19 PM
#7
Ok, here is a little bit different approach:
Code:
Public Function GetChildNode(pTreeView As TreeView, pParentIndex As Integer, pTextToFind) As Node
Dim i As Integer
With pTreeView
For i = 1 To .Nodes(pParentIndex).Children
If i = 1 Then
If UCase(.Nodes(pParentIndex).Child.Text) = UCase(pTextToFind) Then
Set GetChildNode = .Nodes(pParentIndex).Child
Exit Function
End If
Else
If UCase(.Nodes(pParentIndex).Child.Next.Text) = UCase(pTextToFind) Then
Set GetChildNode = .Nodes(pParentIndex).Child
Exit Function
End If
End If
Next
End With
End Function
Public Function SplitPathComponents(ByVal pPath As String, ByVal pSeparator As String) As Variant
Dim arrTemp() As String
Dim strTemp As String
Dim i As Integer
Do Until pPath = ""
If InStr(pPath, pSeparator) > 0 Then
strTemp = Left(pPath, InStr(pPath, pSeparator) - 1)
pPath = Mid(pPath, InStr(pPath, pSeparator) + 1)
Else
If pPath <> "" Then
strTemp = pPath
pPath = ""
End If
End If
ReDim Preserve arrTemp(i)
arrTemp(i) = Trim(strTemp)
i = i + 1
Loop
SplitPathComponents = arrTemp
End Function
Private Sub Combo1_KeyPress(KeyAscii As Integer)
Dim arrComp As Variant
Dim i As Integer
Dim iNode As Integer
Dim xNode As Node
Dim bChildExist As Boolean
Dim iRootParent As Integer
If KeyAscii = 13 Then
'This will split the path and put it in array
arrComp = SplitPathComponents(Combo1.Text, ",") 'You can specify any delimeter (separator)
With TreeView1
For i = 0 To UBound(arrComp)
If i = 0 Then
For iNode = 1 To .Nodes.Count
If UCase(.Nodes(iNode).Text) = UCase(arrComp(i)) Then
iRootParent = iNode
.Nodes(iNode).EnsureVisible
.SetFocus
.Nodes(iNode).Selected = True
Exit For
End If
Next
Else
If bChildExist = False Then
Set xNode = GetChildNode(TreeView1, .Nodes(iRootParent).Index, arrComp(i))
Else
Set xNode = GetChildNode(TreeView1, xNode.Index, arrComp(i))
End If
If Not xNode Is Nothing Then
bChildExist = True
.SetFocus
xNode.EnsureVisible
xNode.Selected = True
End If
End If
Next
End With
End If
End Sub
Regards,
------------------
Serge
Software Developer
[email protected]
[email protected]
ICQ#: 51055819
[This message has been edited by Serge (edited 11-10-1999).]
-
Nov 9th, 1999, 07:43 PM
#8
Thread Starter
Hyperactive Member
With your new code and my example treeview
"Root, Child 2" can't be reached...
-
Nov 9th, 1999, 10:01 PM
#9
Ok, I think this is the last one you'll need. LoL.
Code:
Public Function FindChildNode(pTreeView As TreeView, pParentIndex As Integer, pTextToFind) As Node
Dim i As Integer
With pTreeView
For i = 1 To .Nodes(pParentIndex).Children
If i = 1 Then 'First child
If UCase(.Nodes(pParentIndex).Child.Text) = UCase(pTextToFind) Then
Set FindChildNode = .Nodes(pParentIndex).Child
Exit Function
End If
Else 'Next child
If UCase(.Nodes(pParentIndex).Child.Next.Text) = UCase(pTextToFind) Then
Set FindChildNode = .Nodes(pParentIndex).Child.Next
Exit Function
End If
End If
Next
End With
End Function
Public Function IsChildExist(pTree As TreeView, pParentIndex As Integer) As Boolean
If pTree.Nodes(pParentIndex).Children Then IsChildExist = True
End Function
Public Function SplitPathComponents(ByVal pPath As String, ByVal pSeparator As String) As Variant
Dim arrTemp() As String
Dim strTemp As String
Dim i As Integer
Do Until pPath = ""
If InStr(pPath, pSeparator) > 0 Then
strTemp = Left(pPath, InStr(pPath, pSeparator) - 1)
pPath = Mid(pPath, InStr(pPath, pSeparator) + 1)
Else
If pPath <> "" Then
strTemp = pPath
pPath = ""
End If
End If
ReDim Preserve arrTemp(i)
arrTemp(i) = Trim(strTemp)
i = i + 1
Loop
SplitPathComponents = arrTemp
End Function
Private Sub Combo1_KeyPress(KeyAscii As Integer)
Dim arrComp As Variant
Dim i As Integer
Dim iNode As Integer
Dim xNode As Node
Dim iRootParent As Integer
Dim bLast As Boolean
If KeyAscii = 13 Then
'This will split the path and put it in array
arrComp = SplitPathComponents(Combo1.Text, ",") 'You can specify any delimeter (separator)
With TreeView1
For iNode = 1 To .Nodes.Count
If UCase(.Nodes(iNode).Text) = UCase(arrComp(0)) _
And .Nodes(iNode).Parent Is Nothing Then
iRootParent = iNode
.Nodes(iNode).EnsureVisible
.SetFocus
.Nodes(iNode).Selected = True
Exit For
End If
Next
'If the root node found
If iRootParent > 0 Then
'Reset the counter
i = 0
bLast = False
Do
If .Nodes(iRootParent).Children Then
i = i + 1
Set xNode = FindChildNode(TreeView1, iRootParent, arrComp(i))
If xNode Is Nothing Then
MsgBox "Path not found"
Exit Sub
Else
iRootParent = xNode.Index
xNode.EnsureVisible
.SetFocus
xNode.Selected = True
If i = UBound(arrComp) Then bLast = True
End If
End If
Loop Until bLast
End If
End With
End If
End Sub
Regards,
------------------
Serge
Software Developer
[email protected]
[email protected]
ICQ#: 51055819
-
Nov 9th, 1999, 10:13 PM
#10
Thread Starter
Hyperactive Member
With your new code and my treeview example
"Root" can't be reached
"Soot" can't be reached
Ain't it a drag...
-
Nov 9th, 1999, 11:00 PM
#11
No it ain't. You would have to add one line of code to the code I gave you and you're all set.
Code:
Public Function FindChildNode(pTreeView As TreeView, pParentIndex As Integer, pTextToFind) As Node
Dim i As Integer
With pTreeView
For i = 1 To .Nodes(pParentIndex).Children
If i = 1 Then 'First child
If UCase(.Nodes(pParentIndex).Child.Text) = UCase(pTextToFind) Then
Set FindChildNode = .Nodes(pParentIndex).Child
Exit Function
End If
Else 'Next child
If UCase(.Nodes(pParentIndex).Child.Next.Text) = UCase(pTextToFind) Then
Set FindChildNode = .Nodes(pParentIndex).Child.Next
Exit Function
End If
End If
Next
End With
End Function
Public Function IsChildExist(pTree As TreeView, pParentIndex As Integer) As Boolean
If pTree.Nodes(pParentIndex).Children Then IsChildExist = True
End Function
Public Function SplitPathComponents(ByVal pPath As String, ByVal pSeparator As String) As Variant
Dim arrTemp() As String
Dim strTemp As String
Dim i As Integer
Do Until pPath = ""
If InStr(pPath, pSeparator) > 0 Then
strTemp = Left(pPath, InStr(pPath, pSeparator) - 1)
pPath = Mid(pPath, InStr(pPath, pSeparator) + 1)
Else
If pPath <> "" Then
strTemp = pPath
pPath = ""
End If
End If
ReDim Preserve arrTemp(i)
arrTemp(i) = Trim(strTemp)
i = i + 1
Loop
SplitPathComponents = arrTemp
End Function
Private Sub Combo1_KeyPress(KeyAscii As Integer)
Dim arrComp As Variant
Dim i As Integer
Dim iNode As Integer
Dim xNode As Node
Dim iRootParent As Integer
Dim bLast As Boolean
If KeyAscii = 13 Then
'This will split the path and put it in array
arrComp = SplitPathComponents(Combo1.Text, ",") 'You can specify any delimeter (separator)
With TreeView1
For iNode = 1 To .Nodes.Count
If UCase(.Nodes(iNode).Text) = UCase(arrComp(0)) _
And .Nodes(iNode).Parent Is Nothing Then
iRootParent = iNode
.Nodes(iNode).EnsureVisible
.SetFocus
.Nodes(iNode).Selected = True
Exit For
End If
Next
If UBound(arrComp) = 0 Then Exit Sub
'If the root node found
If iRootParent > 0 Then
'Reset the counter
i = 0
bLast = False
Do
If .Nodes(iRootParent).Children Then
i = i + 1
Set xNode = FindChildNode(TreeView1, iRootParent, arrComp(i))
If xNode Is Nothing Then
MsgBox "Path not found"
Exit Sub
Else
iRootParent = xNode.Index
xNode.EnsureVisible
.SetFocus
xNode.Selected = True
If i = UBound(arrComp) Then bLast = True
End If
End If
Loop Until bLast
End If
End With
End If
End Sub
THAT'S IT......LoL.
Regards,
------------------
Serge
Software Developer
[email protected]
[email protected]
ICQ#: 51055819
-
Nov 9th, 1999, 11:15 PM
#12
Thread Starter
Hyperactive Member
I still can't get it to work with the treeview I actually want to use it for..
The code only works with the dummy treeview I provided as an example (f.i. add another node to Root and it does not work anymore).
The treeview I want to put it in currently has 147 nodes.
Is there anyway the code can be made compatible for any treeview (that's probably what I need)...
-
Nov 9th, 1999, 11:31 PM
#13
This code should work for any populated TreeView. Because, when I wrote this code I used Northwind database.
Regards,
------------------
Serge
Software Developer
[email protected]
[email protected]
ICQ#: 51055819
-
Nov 10th, 1999, 01:27 AM
#14
ok, I found the problem. Just replace FindChildNode function with this new one:
Code:
Public Function FindChildNode(pTreeView As TreeView, pParentIndex As Integer, pTextToFind) As Node
Dim i As Integer
Dim iIndex As Integer
With pTreeView
For i = 1 To .Nodes(pParentIndex).Children
If i = 1 Then 'First child
iIndex = .Nodes(pParentIndex).Child.Index
If UCase(.Nodes(pParentIndex).Child.Text) = UCase(pTextToFind) Then
Set FindChildNode = .Nodes(pParentIndex).Child
Exit Function
End If
Else 'Next child
If UCase(.Nodes(iIndex).Text) = UCase(pTextToFind) Then
Set FindChildNode = .Nodes(iIndex)
Exit Function
End If
End If
iIndex = iIndex + 1
Next
End With
End Function
That's it......this time I mean it.....LoL
Regards,
------------------
Serge
Software Developer
[email protected]
[email protected]
ICQ#: 51055819
[This message has been edited by Serge (edited 11-10-1999).]
-
Nov 10th, 1999, 07:09 AM
#15
Addicted Member
Hi guys..
So Inhumanoid did it work?
I've been monitoring this 1-2 game .
Serge, I/we thank you for your valuable help, and that what makes ths BBS a great site.
thanx again.
-
Nov 10th, 1999, 08:52 AM
#16
You're welcome. By the way it should work just fine. It's just sometimes when you do it on a fly you don't take some thing under consideration. Anyway, I saved this project (but mine has TreeView populated with database records), so, if anyone wants it, just let me know and I'll email it to you.
Best regards,
------------------
Serge
Software Developer
[email protected]
[email protected]
ICQ#: 51055819
-
Nov 10th, 1999, 12:11 PM
#17
Thread Starter
Hyperactive Member
It won't work with my treeview *crying*...
try this example treeview:
Code:
Private Sub Form_Load()
Dim nodX As Node ' Declare Node variable.
Set nodX = TreeView1.Nodes.Add(, , "r", "Root")
Set nodX = TreeView1.Nodes.Add(, , "s", "Soot")
Set nodX = TreeView1.Nodes.Add("r", tvwChild, "child1", "Child 1")
Set nodX = TreeView1.Nodes.Add("r", tvwChild, "child2", "Child 2")
Set nodX = TreeView1.Nodes.Add("r", tvwChild, "child5", "Child 5")
Set nodX = TreeView1.Nodes.Add("child1", tvwChild, "child3", "Child 3")
Set nodX = TreeView1.Nodes.Add("s", tvwChild, "child4", "Child 4")
End Sub
in this example
"root, child 5" can't be reached...
-
Nov 10th, 1999, 04:10 PM
#18
Thread Starter
Hyperactive Member
No Lyla :-(
Damnit.... It still does not work with my treeview... How can I solve this ??
My treeview is build up from a database (but then it should not be any different)...
Serge, maybe I could e-mail you a small project with that database so you can have a looksy what is wrong ???
-
Nov 10th, 1999, 05:17 PM
#19
Thread Starter
Hyperactive Member
There seems to be a problem with the FindChildNode function.. There a problem with one of the two integers (i or iIndex)..
But I can't find a solution...
-
Nov 10th, 1999, 07:01 PM
#20
Yes, please send me your project. Also, I can send you this small example I wrote and you'll see that the code is working.
Regards,
------------------
Serge
Software Developer
[email protected]
[email protected]
ICQ#: 51055819
-
Nov 10th, 1999, 07:04 PM
#21
Thread Starter
Hyperactive Member
Cool man,
hehe, I'll send you a sample program to show you it's not fully working (at least that's my guess)
It's comming you'r way....
cheers...
-
Nov 10th, 1999, 08:24 PM
#22
Yep.....I found the problem. So, here is the new WORKING code for everyone who would need this in the future. I made it generic enough so any project can use it:
Code:
Public Function FindChildNode(pTreeView As TreeView, pParentIndex As Integer, pTextToFind) As Node
Dim i As Integer
Dim iIndex As Integer
With pTreeView
iIndex = .Nodes(pParentIndex).Child.FirstSibling.Index
Do Until iIndex > .Nodes(pParentIndex).Child.LastSibling.Index
If UCase(.Nodes(iIndex).Text) = UCase(pTextToFind) Then
Set FindChildNode = .Nodes(iIndex)
Exit Function
End If
If iIndex <> .Nodes(pParentIndex).Child.LastSibling.Index Then
iIndex = .Nodes(iIndex).Next.Index
Else
iIndex = iIndex + 1
End If
Loop
End With
End Function
Public Function IsChildExist(pTree As TreeView, pParentIndex As Integer) As Boolean
If pTree.Nodes(pParentIndex).Children Then IsChildExist = True
End Function
Public Function SplitPathComponents(ByVal pPath As String, ByVal pSeparator As String) As Variant
Dim arrTemp() As String
Dim strTemp As String
Dim i As Integer
Do Until pPath = ""
If InStr(pPath, pSeparator) > 0 Then
strTemp = Left(pPath, InStr(pPath, pSeparator) - 1)
pPath = Mid(pPath, InStr(pPath, pSeparator) + 1)
Else
If pPath <> "" Then
strTemp = pPath
pPath = ""
End If
End If
ReDim Preserve arrTemp(i)
arrTemp(i) = Trim(strTemp)
i = i + 1
Loop
SplitPathComponents = arrTemp
End Function
Public Sub FindSpecificNode(pTree As TreeView, pPathToFind As String, pDelimeter As String)
Dim arrComp As Variant
Dim i As Integer
Dim iNode As Integer
Dim xNode As Node
Dim iRootParent As Integer
Dim bLast As Boolean
arrComp = SplitPathComponents(pPathToFind, pDelimeter) 'You can specify any delimeter (separator)
With TreeView1
For iNode = 1 To .Nodes.Count
If UCase(.Nodes(iNode).Text) = UCase(arrComp(0)) _
And .Nodes(iNode).Parent Is Nothing Then
iRootParent = iNode
.Nodes(iNode).EnsureVisible
.SetFocus
.Nodes(iNode).Selected = True
Exit For
End If
Next
If UBound(arrComp) = 0 Then Exit Sub
'If the root node found
If iRootParent > 0 Then
'Reset the counter
i = 0
bLast = False
Do
If .Nodes(iRootParent).Children Then
i = i + 1
Set xNode = FindChildNode(TreeView1, iRootParent, arrComp(i))
If xNode Is Nothing Then
MsgBox "Path not found"
Exit Sub
Else
iRootParent = xNode.Index
xNode.EnsureVisible
.SetFocus
xNode.Selected = True
If i = UBound(arrComp) Then bLast = True
End If
End If
Loop Until bLast
End If
End With
End Sub
Usage: FindSpecificNode TreeView, PathToFind, Delimeter
Example: FindSpecificNode TreeView1, "MyNode,SubNode,SubSubNode", ","
In Inhumanoid's code you would have to put this on Combo1_KeyPress event:
Code:
Private Sub Combo1_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
FindSpecificNode TreeView1, Combo1.Text, ","
End If
End Sub
Inhumanoid I sent your code back to you (with the fix).
Best regards to all,
------------------
Serge
Software Developer
[email protected]
[email protected]
ICQ#: 51055819
[This message has been edited by Serge (edited 11-11-1999).]
-
Nov 10th, 1999, 09:14 PM
#23
Thread Starter
Hyperactive Member
Yep...
Look's like she's workin...
Thank's for a wonderfull experience ;-)
cheers...
-
Nov 10th, 1999, 10:41 PM
#24
You're welcome.
------------------
Serge
Software Developer
[email protected]
[email protected]
ICQ#: 51055819
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|