More collections. Part 2.


© Lyapin Ilya
Articles in this Topic    Discussions in this Topic

In the previous article we began to write a simple application. Here is the continuation.

Introduction.

In the previous article (see http://www.suite101.com/article.cfm/delp... we began to write a program that reads a system registry key and all its values, stores key and values into a collection, and lists them to user. The collection can be saved to a file and loaded from it. It consists of two units ILRegCollection and Unit1.

ILRegCollection unit that implements component (TILRegSaver) and collection (TILRegEntryList) classes was discussed in the previous article.

Unit1 is explained below.

Code.

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls, StdCtrls, ilRegCollection;

type
TForm1 = class(TForm)
Memo1: TMemo;
Panel1: TPanel;
btnRead: TButton;
edKeyName: TEdit;
btnList: TButton;
btnLoad: TButton;
btnSave: TButton;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure btnReadClick(Sender: TObject);
procedure btnListClick(Sender: TObject);
procedure btnLoadClick(Sender: TObject);
procedure btnSaveClick(Sender: TObject);
private
{ Private declarations }
FSaver:TILREGSaver;
procedure readFromRegistry(Saver:TILREGSaver);
procedure ListItems(Item:TILRegEntry);
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation
uses Registry;
{$R *.DFM}

procedure TForm1.ListItems(Item:TILRegEntry);
var
i:integer;
begin
Memo1.Lines.Add('EntryName:'#9+Item.EntryName);
if Item.Value<>NULL then
Memo1.Lines.Add('value:'#9#9+item.value);
for i:=0 to item.SubEntries.Count-1 do
ListItems(item.SubEntries[i]);
end;

procedure TForm1.readFromRegistry(Saver: TILREGSaver);
var
r:TRegistry;
item,item1:TILRegEntry;
s:TStringList;
vInfo:TRegDataInfo;
i:integer;
begin
r:=TRegistry.create;
s:=TStringList.Create;
try
r.RootKey:=HKEY_LOCAL_MACHINE;
if not r.OpenKey(edKeyName.text,false) then
raise Exception.Create('Path not found!');
if not r.GetDataInfo(',vInfo) then
raise Exception.Create(');
item:=TILREGENTRY(Saver.RegEntrys.Add);
item.EntryName:=edKeyName.text;
item.EntryType:=rdUnknown;
item.Value:=NULL;
r.GetValueNames(s);
for i:=0 to s.Count-1 do
begin
item1:=TILREGENTRY(item.SubEntries.add);
try
item1.EntryName:=s[i];
if not r.GetDataInfo(item1.EntryName,vInfo) then
raise Exception.Create(');
item1.EntryType:=vInfo.RegData;
case item1.EntryType of
rdString,
rdExpandString:
item1.Value:=r.ReadString(item1.EntryName);
rdInteger:
item1.Value:=r.ReadInteger(item1.EntryName);
else
item1.Value:=NULL;
end;
except
item1.Free;
end;
end;
finally
r.free;
s.free;
end;
end;

// event handlers
procedure TForm1.FormCreate(Sender: TObject);
begin
FSaver:=TILREGSaver.create(nil);
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
FSaver.free;
end;

procedure TForm1.btnReadClick(Sender: TObject);
begin
readFromRegistry(FSAVER);
end;

procedure TForm1.btnListClick(Sender: TObject);
var
i:integer;
begin
for i:=0 to FSaver.RegEntrys.Count-1 do
ListItems(FSaver.RegEntrys[i]);
end;

procedure TForm1.btnLoadClick(Sender: TObject);
begin
FSaver.LoadFromFile(ChangeFileExt(Paramstr(0),'.IL'));
end;

procedure TForm1.btnSaveClick(Sender: TObject);
begin
FSaver.SaveToFile(ChangeFileExt(Paramstr(0),'.IL'));
end;

end.

Code explanation.

The form looks like following:


Important methods:

1. procedure FormCreate(Sender: TObject);
This method is called just after a form is created; it creates an instance of TILRegSaver component and store pointer into FSaver variable.

2. procedure FormDestroy(Sender: TObject);
This method is called just before a form is destoyed; it frees an instance of TILRegSaver component that was created in the FormCreate Method.

3. procedure btnLoadClick(Sender: TObject);
This method is called when user clicks "Load From File" button; it calls LoadFromFile method of FSaver in order to load the collection of registry keys and its values from the file "project1.il".

4. procedure btnSaveClick(Sender: TObject);
This method is called when user clicks "Save To File"

       

Go To Page: 1 2


Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo