SearchBox for Treeview

  • Re: SearchBox for Treeview


    A TreeView does not have an in-built Search function, but you can iterate fairly quickly through the nodes comparing text.


    For an example, add a TreeView, TextBox and ListView to a userform. Leave all control names as default and paste the following code into the form



    Couldn't understand what results you wanted in a textbox so the Caption of any matching node is copied to the listbox. Just type in the textbox to search.

  • Re: SearchBox for Treeview


    Sorry I agree that I will absolutely abide by the "cross post" rule in future. Sorry for this miss from my side and request your help please. Guess I was not thinking,what I'm trying to do is add
    a search box to my project if possible.The example workbook shows what I'm looking for. Thanks for any help I can get.

  • Re: SearchBox for Treeview


    It wasn't meant to work with your project - it was a stand alone example to illustrate. The code only was provided as it was written on an Android Pad and it's a little difficult to test Excel VBA code on one of those.


    Your workbook from Excel Forum is attached with the code added. I do have issues with mismatched Common controls on this machine after a recent 'downgrade' so you may have problems loading the userform. If that should happen, all the changed code is below:


    (Userform Declaration section - at the very top before the first procedure).

    Code
    '// This is required to allow case insensitive text comparisons
    '// If not included, then text compares are case sensitive smd probably
    '// need to be converted to all upper/lower case to compare correctly
    Option Compare Text
    
    
    Dim LastNode As Node
    Dim lngStdBackColor As Long
    Dim lngStdForeColor As Long


    (TextBox8 KeyUp Event handler)


    (TV Enter event handler)


    The way a TreeView works is slightly confusing - especially if HideSelection is set True. This hides the selected item if the TreeView does not have focus which is why the code changes the color of any item found. You need to play around with it and understand the various settings...


    Just as a matter of interest: As well as adding links to any cross posts, it is also considered not the done thing to expect anyone to go to another site to download a file. If you post on a site, you include all relevant info, and attachments, in that post.

  • Re: SearchBox for Treeview


    Thank You for your time, nice job on the code. I will play around and try to get it to populate the textbox1 as it does a search. Not sure if possible,but if not
    it works pretty darn good now. What is the proper way to close cross post?.
    Thank You

  • Re: SearchBox for Treeview


    To 'Click' the TV, replace the Textboxc8_KeyUp event hadler with:


    You should be aware you are using the wrong event - any code to run when a node is clicked should be in the NodeClick event handler, not the TreeView Click event... It makes it easier to manipulate the nodes in code...

  • Re: SearchBox for Treeview


    Trying to add delete button for textbox8,so you can clear searchbox. I get that part but can't make the userform start back to first item in list?
    Another Question what if there is two or three names the same,can it search all?

  • Re: SearchBox for Treeview


    My original sample listed all found items in a list box - It does work if you add it to its own userform as I described - admittedly you then need to work it back into your own userform if you decide to use something like that. That is about the simplest way to report multiple matches. With the code as written at the moment, I'd suggest leaving it as it is, as trying to add the extra functionality is going to hit a lot of the existing code.


    I did try with the delete button, but only ended up clearing the 2 sample comments by clearing TextBox1.


    Sorry, but I can't give any more time to this tonight. Perhaps fresh eyes are needed...

  • Re: SearchBox for Treeview


    Thanks cytop
    Getting a lot closer because of all your help. I will keep pegging away at code and try and make progress. Think this
    could be handy tool for people using Excel. Good way keep track of different codes.All ideas are welcome to make this better.

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!