We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

building a treeview

timbro
timbro asked
on
Medium Priority
229 Views
Last Modified: 2010-04-04
Heres an EZ 1: all I want to do is take a registry key and
all its subkeys and represent them in a Ttreeview.
I just need a snippit or a relative example of recursion
I could incorporate.
Please dont just point me to some crummy component.
Comment
Watch Question

Commented:
Hi!
The following code fills a node with it's subkeys. It is NOT recommendable to browse the entire registry tree at once (you know how large the REG is!!!). So, one should call this piece of code every time the user expands a node, for example in the OnExpand event.

procedure TForm1.FillSubTree(aPath: string; aNode: TTreeNode);
var aReg: TRegistry;
    aSubKeys: TStringList;
    i: integer;
begin

  aReg := TRegistry.Create;
  try
    with aReg do begin
      RootKey := HKEY_LOCAL_MACHINE;
      OpenKey(aPath);
      aSubKeys := TStringList.Create;
      GetValueName(aSubKeys);
    end;
  finally
    aReg.Free;
  end;

  if aSubKeys.Count > 0 then begin
    for i := 0 to aSubKeys.Count - 1 do
      TreeView1.Items.AddChildObject(aNode, aSubKeys[i], nil)
  end;
  aSubKeys.Free;

end;

Author

Commented:
Thanks, freter for your response!
I guess this would be fine for displaying a key and its
value names but I would like to display a key with its
subkeys and their subkeys etc... initially fully expanded
hence the need for a looping recursion type procedure.
I suppose one could use the GetKeyNames in the same example
but I absolutely need the recursion. The reg keys I'm
working with only go a few levels deep so theres no fear of
a long consuming process. Even though, one should be able to
treeview an entire root key if wanted. regedit does it...
I really need a recursion example.

Commented:
For another question I wrote a recursive procedure that fills a treeview with directory structure and files. You can replace it with registry - very easy. (it lists PCX files in the given directory)



    procedure TForm1.ListPCX(path: AnsiString; Node: TTreeNode);
    var
      srRes : TSearchRec;
      iFound : Integer;
    begin
      if path[Length(path)] <> '\' then path := path +'\';

        iFound := FindFirst( path + '*.*', faAnyfile, srRes );
        while iFound = 0 do
        begin
          if ( srRes.Name <> '.' ) and ( srRes.Name <> '..' ) then
            if srRes.Attr and faDirectory > 0 then
              ListPCX(path+srRes.Name, TreeViewGroupsAndFiles.Items.AddChild(Node, {path+}srRes.Name));
          iFound := FindNext(srRes);
        end;
        FindClose(srRes);

      iFound := FindFirst(path+'*.bmp', faAnyFile-faDirectory, srRes);
      while iFound = 0 do
      begin
        if ( srRes.Name <> '.' ) and ( srRes.Name <> '..' ) and ( srRes.Name <> '' ) then
          TreeViewGroupsAndFiles.Items.AddChild(Node,srRes.Name);
        iFound := FindNext(srRes);
      end;
      FindClose( srRes );
    end;

Author

Commented:
Thanx, Matvey, that gets me a little closer! I can see how the recursion works only I cant use any FindNext functions with the
registry. and you cant use any GetKeyNames.count because the
count var gets mixed up in the recurse. What was that about being
'very easy'? I'm sorry for being dense but I need a version of
your code aimed at the registry. I didnt think it would be this
tough! In-between sessions of self-induced hair extraction I have
to stop and think - with something this common there should be
something in a textbook somewhere! But alas, another Delphi mystery. What the hey, heres another 50 points...


Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview

Commented:
Took the words out of my mouth:)
Don't mean anything, nevermind...

Author

Commented:
Thanx freter!
Precisely, exactly what I needed. I can regrow my hair now!
Your code is an absolutely textbook-quality example!
I really appreciate your time and efforts!

Actually your first response did answer my question (as it
was worded) and I could have used it as-is, only I knew
I could do it all at once thru recursion. And it was driving me
batty that I couldnt apply to the registry!

I've seen recursive procedures and functions used before with
file stuff like in the example by Matvey (thank-you!) but am
not real familiar with them. [anybody know what having to
declare a recursive procedure 'forward' is all about?]

I really appreciate your help guys!
Good luck and thanks again all!
-Tim

Commented:
To understand recursion, we must first understand recursion.
;-)
Freter

Author

Commented:
Could you repeat that? 8^D

Commented:
You can say that again!!! hehe
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.