Results 1 to 36 of 36

Thread: searching a treeview

  1. #1
    scoutt
    Guest

    searching a treeview

    is it possible to do a search in a treewview like you do in notepad. say I click on search and an input box comes up and you type in your word/s and push search and have it highlight the searched word in the treeview then hit f3 to keep searching?
    if so how would I proceed? I can do the input box but not sure on how or if I can do the f3 part of it.

    thanks in advance

  2. #2
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    search sample :
    Code:
    Private Sub Command1_Click()
        Dim l As Long
        Dim n As Node
        Dim sSearchString As String
        sSearchString = "test"
    
        For l = 1 To TreeView1.Nodes.Count
            Set n = TreeView1.Nodes(l)
            If UCase(n.Text) = UCase(sSearchString) Then
                n.Selected = True
                'or call the click event : TreeView1_NodeClick n
                Exit For
            End If
        Next l
    End Sub
    -= a peet post =-

  3. #3
    Frenzied Member Mark Sreeves's Avatar
    Join Date
    Nov 1999
    Location
    UK
    Posts
    1,845
    you can half search time by UCASEing the search string only once:

    Code:
    Private Sub Command1_Click()
        Dim l As Long
        Dim n As Node
        Dim sSearchString As String
        sSearchString= UCase("test")
    
        For l = 1 To TreeView1.Nodes.Count
            Set n = TreeView1.Nodes(l)
            If UCase(n.Text) = sSearchString Then
                n.Selected = True
                'or call the click event : TreeView1_NodeClick n
                Exit For
            End If
        Next l
    End Sub



    and you can half the search time again by using
    Option Compare Text



    Code:
    Option Compare Text
    
    Private Sub Command1_Click()
        Dim l As Long
        Dim n As Node
        Dim sSearchString As String
        sSearchString= test"
    
        For l = 1 To TreeView1.Nodes.Count
            Set n = TreeView1.Nodes(l)
            If n.Text = sSearchString Then
                n.Selected = True
                'or call the click event : TreeView1_NodeClick n
                Exit For
            End If
        Next l
    End Sub
    Mark
    -------------------

  4. #4
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    Mark, what does Option Compare Text do ?
    -= a peet post =-

  5. #5
    Frenzied Member Mark Sreeves's Avatar
    Join Date
    Nov 1999
    Location
    UK
    Posts
    1,845
    Option Compare Text makes text comparisons case insensitive
    Mark
    -------------------

  6. #6
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    thanks, I feel a bit stupid now
    -= a peet post =-

  7. #7
    scoutt
    Guest
    thnaks guys I will play with that and let you know.

  8. #8
    scoutt
    Guest
    ok, I played with it and found out that I have to have the exact text case to get any results. plus how do I make it search child nodes instead of parent ones?

    also what/where do I put that option campare text, can it co-exist with option explicit

  9. #9
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    doesnt the sample search the child nodes ?
    -= a peet post =-

  10. #10
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    and yes

    Code:
    Option Explicit
    Option Compare Text
    is not a porblem
    -= a peet post =-

  11. #11
    scoutt
    Guest
    I put it under option explicit and I get a compile error saying
    "Expected: Base or Compare or Explicit or Private."
    and it highlights Compare

    well I guess it does search child nodes. forgot that my treeview was under a parent. but how do I make it search the child of the child then.

  12. #12
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    Originally posted by scoutt
    ...that option campare text, can it co-exist with option explicit
    think you just misspelled compare ?

    anyway that would create the error you reported.
    -= a peet post =-

  13. #13
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    well I guess it does search child nodes. forgot that my treeview was under a parent. but how do I make it search the child of the child then.
    the code sample should search for all nodes, child and child of a child and child of childs child.... I'd better stop
    -= a peet post =-

  14. #14
    scoutt
    Guest
    well crap I did misspell it. thanks.
    ok I can search child of the child of the child, but the reason I didn't was becasue it had to be spelled exactly like the text I was searching for. I tried

    VB Code:
    1. If UCase(n.Text) = UCase("'*sSearchString*'")
    I don't get any errors but then again it didn't do anything different
    what other way can I do it so it doesn't matter what the text that I am searching for it goes to it.

    then when I found something, how do I make it continue with the same work like the f3 does in notepad search

  15. #15
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    hmm try this for the *sdfasdf search

    Code:
    Option Compare Text
    
    Private Sub Command1_Click()
        Dim l As Long
        Dim n As Node
        Dim sSearchString As String
        sSearchString= test"
    
        For l = 1 To TreeView1.Nodes.Count
            Set n = TreeView1.Nodes(l)
            If InStr(1, n.Text, sSearchString, vbUseCompareOption) Then
                n.Selected = True
                'or call the click event : TreeView1_NodeClick n
                Exit For
            End If
        Next l
    End Sub
    -= a peet post =-

  16. #16
    scoutt
    Guest
    I get an variable not defind error on the vbUseCompareOption

  17. #17
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    oops.. sorry about that, use vbTextCompare instead of vbUseCompareOption

    -= a peet post =-

  18. #18
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    Ok my friend ... this is it:

    Create a new project,
    Add a treeview and a commandbutton
    paste this code into your form:

    Code:
    Option Explicit
    Private s As String
    Private lLastNodeFound As Long
    
    Private Sub Command1_Click()
    If Command1.Caption = "Find" Then s = InputBox("What do want to search for?")
        If s <> "" Then
            If SearchTree(s) Then
                Command1.Caption = "Find next"
            Else
                MsgBox "Reached the end..."
                Command1.Caption = "Find"
            End If
        End If
    End Sub
    
    Private Sub Form_Load()
        Dim n As Node
        Set n = TreeView1.Nodes.Add(, , , "Settings...")
        TreeView1.Nodes.Add n, tvwChild, "Desktop", "Desktop settings"
        TreeView1.Nodes.Add n, tvwChild, "Mouse", "Mouse pointer settings"
        TreeView1.Nodes.Add n, tvwChild, "Misc", "Misc"
        Set n = TreeView1.Nodes.Add(, , , "Test...")
        TreeView1.Nodes.Add n, tvwChild, "Desktop2", "Desktop settings"
        TreeView1.Nodes.Add n, tvwChild, "Mouse2", "Mouse pointer settings"
        TreeView1.Nodes.Add n, tvwChild, "Misc2", "Misc"
        Command1.Enabled = True
        Command1.Caption = "Find"
    End Sub
    Private Function SearchTree(sSearchString As String) As Boolean
        Dim l As Long
        Dim n As Node
        'Dim sSearchString As String
        For l = lLastNodeFound + 1 To TreeView1.Nodes.Count
            Set n = TreeView1.Nodes(l)
            If InStr(1, n.Text, sSearchString, vbTextCompare) Then
                n.Selected = True
                SearchTree = True
                lLastNodeFound = l
                'or call the click event : TreeView1_NodeClick n
                Exit Function
            End If
        Next l
        SearchTree = False
        lLastNodeFound = 0
    End Function
    *phuuu...* that should do it
    -= a peet post =-

  19. #19
    PowerPoster eiSecure's Avatar
    Join Date
    Jul 2000
    Location
    Texas
    Posts
    2,209
    This is some pretty handy code.

  20. #20
    scoutt
    Guest
    I took out the vbUseCompareOption variable and it works pretty good. but after it finds something then it just quits searching. how can i continue it?

    I was thinking of just looping back to the begining but wouldn't that just keep going by itself?

  21. #21
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    try the sample...
    -= a peet post =-

  22. #22
    PowerPoster eiSecure's Avatar
    Join Date
    Jul 2000
    Location
    Texas
    Posts
    2,209
    are you telling that to me or to scoutt?

  23. #23
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    Originally posted by eiSecure
    This is some pretty handy code.
    what? no slapping me in the head with heavy books ?? you scare me eiSec

    -= a peet post =-

  24. #24
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    Originally posted by eiSecure
    are you telling that to me or to scoutt?
    scoutt
    -= a peet post =-

  25. #25
    PowerPoster eiSecure's Avatar
    Join Date
    Jul 2000
    Location
    Texas
    Posts
    2,209
    lol...
    you just gave me an oppurtunity to slap you though...

    ahem...(clears throat)...

    ITS eiSecure, not eiSec!!!!!!!!
    and be sure to make the first e lowercase.

    for future reference.


  26. #26
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    uh.. my ears...

    sorry eiSec...ure its my rheumatism preventing me from writing those loooong nicks
    -= a peet post =-

  27. #27
    PowerPoster eiSecure's Avatar
    Join Date
    Jul 2000
    Location
    Texas
    Posts
    2,209
    lol...
    sure....

  28. #28
    scoutt
    Guest
    sorry peet, I didn't see your post. blind as a bat I guess. I wil try that and let you know.

    eiSecure: yes it is pretty handing if I can get my hands and eyes to work together,

  29. #29
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    ...pretty handing if I can get my hands and eyes to work
    oooops.... careful or eiSec will smack you as well
    -= a peet post =-

  30. #30
    PowerPoster eiSecure's Avatar
    Join Date
    Jul 2000
    Location
    Texas
    Posts
    2,209
    can you not read the size 72 text I wrote????

    its eiSecur!!!!!!!!!!

  31. #31
    PowerPoster eiSecure's Avatar
    Join Date
    Jul 2000
    Location
    Texas
    Posts
    2,209
    wait, sorry. Typo.

    its: eiSecure!!!

  32. #32
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    Originally posted by eiSecure
    can you not read the size 72 text I wrote????

    its eiSecur!!!!!!!!!!
    heeh.... got you !!

    eiSecur eh ??
    -= a peet post =-

  33. #33
    PowerPoster eiSecure's Avatar
    Join Date
    Jul 2000
    Location
    Texas
    Posts
    2,209
    no no no no no!

    ....now you're gonna haunt me with that the rest of my live on this forum, aren't you??

  34. #34
    scoutt
    Guest


    ok you guys, I did noticed that I made a boo boo, didn't feel like editing it.

    I want to thank you peet that last sample worked almost like I wanted. I can play with it and get it exact.

    thanks peet and eiSecure spelled right too

  35. #35
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    yes yes yes ...
    -= a peet post =-

  36. #36
    PowerPoster eiSecure's Avatar
    Join Date
    Jul 2000
    Location
    Texas
    Posts
    2,209
    man, you're getting too excited over this...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width