Solved

building a treeview

Posted on 1998-07-16
10
209 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.
0
Comment
Question by:timbro
[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
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 2

Expert Comment

by:freter
ID: 1357981
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;

0
 

Author Comment

by:timbro
ID: 1357982
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.
0
 
LVL 3

Expert Comment

by:Matvey
ID: 1357983
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;
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:timbro
ID: 1357984
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...


0
 
LVL 2

Accepted Solution

by:
freter earned 100 total points
ID: 1357985
Ok, here we go:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ComCtrls, registry, StdCtrls;

type
  TForm1 = class(TForm)
    TreeView1: TTreeView;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    procedure EnumKeys(aNode: TTreeNode; aKey:HKEY; aPath: string);
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}


procedure EnumSubKeys(aKey: HKEY; const Path:String; var aValue: TStringList);
var
  aRegistry: TRegistry;
begin
  aRegistry:=TRegistry.Create;
  try
    with aRegistry do
    begin
      RootKey:=aKey;
      OpenKey(Path,True);
      GetKeyNames(aValue);
    end;
  finally
    aRegistry.Free;
  end;
end;

procedure TForm1.EnumKeys(aNode: TTreeNode; aKey:HKEY; aPath: string);
var aReg: TRegistry;
    aSubKeys: TStringList;
    i: integer;
    newNode: TTreeNode;
begin

  aSubKeys := TStringList.Create;
  EnumSubKeys(aKey, aPath,aSubKeys);

  if aSubKeys.Count > 0 then begin
    for i := 0 to aSubKeys.Count - 1 do begin
      newNode := TreeView1.Items.AddChildObject(aNode, aSubKeys[i], Pointer(aPath + '\' + aSubKeys[i]));
      EnumKeys(newNode, aKey, String(newNode.Data));
    end;
    aSubKeys.Free;

  end;

end;


procedure TForm1.Button1Click(Sender: TObject);
begin
  EnumKeys(nil, HKEY_CURRENT_USER, 'Software');
end;

end.

You will need a TreeView named TreeView1 and a button named Button1. Don't forget to assign the correct events!

Have fun,
Freter

0
 
LVL 3

Expert Comment

by:Matvey
ID: 1357986
Took the words out of my mouth:)
Don't mean anything, nevermind...
0
 

Author Comment

by:timbro
ID: 1357987
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
0
 
LVL 2

Expert Comment

by:freter
ID: 1357988
To understand recursion, we must first understand recursion.
;-)
Freter
0
 

Author Comment

by:timbro
ID: 1357989
Could you repeat that? 8^D
0
 

Expert Comment

by:amber45
ID: 1357990
You can say that again!!! hehe
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Magic Software info 18 151
How to convert memory stream to PDF file 6 215
Drag & Drop... Data from one grid to another 2 51
PHP preg_replace code convert to Delphi 14 94
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question