Emmanuel Lamy wrote:
> I am trying to setup a routine to read into a TMemo the subkeys 
> belonging to registry keys, and I keep getting a message 
> saying: "Control '' has no parent window", which is not true to my 
> knowledge. Here is how I approached the problem:
> 
> procedure ListSubkeys(Key:string; aMemo:TMemo);
> var BaseKey: String;
> aList:TMemo;
> begin
>     if key = '' then
>     exit;
> 
>     Basekey := '\Software\MyApplication\'+key;
>     aList:=TMemo.Create(Application);
>     aList.Visible:=false;
>     aList.Parent:=Application.MainForm;

Never use a visual control unless you have to. Since you just need to 
store a list of strings, and you don't need that list to be visible to 
the user, scroll up and down, be selectable with the mouse, or do 
anything else but hold a bunch of strings in memory, use a TStringList 
instead. (But don't go make any changes just yet. Read on.)

>     with TRegIniFile.Create do
>     begin
>         try
>             rootKey:=HKey_Local_Machine;
>             if not OpenKey(basekey,false) then
>             exit;

Since all you need is read access, not write access, use the 
OpenKeyReadOnly method instead of OpenKey, if it's available in your 
version of Delphi. Always as for the *fewest* privileges you need.

>             GetKeyNames(aList.Lines);

Since GetKeyNames just asks for a TStrings object, and you have a 
perfectly good TStrings object in aMemo.Lines, just pass that to 
GetKeyNames. There's no need to create a separate TMemo (or TStringList) 
when all you're going to do on the very next line is copy the contents 
into the container you really wanted the strings to be in in the first 
place.

GetKeyNames(aMemo.Lines);

>             aMemo.Text:=aList.Text;
>         finally
>             aList.Free;
>             Free;
>         end;
>     end;
> end;
> 
> Anyway I turn this code I get the error message. Can somebody point 
> out what it is I am doing wrong here, please. Thanks in advance.


-- 
Rob

Reply via email to