Jump to content


Photo

Bind ProperyView to an IniFile


  • Please log in to reply
No replies to this topic

#1 wvd_vegt

wvd_vegt

    Master Member

  • Honorable Members
  • PipPipPipPipPip
  • 710 posts
  • Gender:Male
  • Location:the Netherlands

Posted 18 March 2005 - 10:48 PM

Hi,

Here is some code to show and edit an inifile. Note that adding & deleting keys and/or sections is not implenented. Substitue the AppIniFile with your ini filename and don't forget to add PropertyItemClasses to the uses clause.

CODE
procedure TForm1.PropertiesView1Change(Sender: TObject;

 Item: TCustomPropertyItem; Value: WideString);

var

 section, key      : string;

 ini               : TIniFile;

begin

 key := Item.Caption;

 section := Item.ParentItem.Caption;



 if section[1] = '[' then Delete(section, 1, 1);

 if section[Length(Section)] = ']' then Delete(section, Length(Section), 1);



 ini := TIniFile.Create(AppIniFile);

 ini.WriteString(section, key, Value);

 ini.UpdateFile;

 ini.Free;

end;



procedure TForm1.Button1Click(Sender: TObject);

var

 ini               : TIniFile;

 keys,

   sections        : TStringList;

 i, j              : Integer;

 Change            : TChangeEvent;

begin

 //We don't want false alarm during filling.  

 Change := PropertiesView1.OnChange;

 PropertiesView1.OnChange := nil;



 PropertiesView1.Categories.Clear;



 ini := TIniFile.Create(AppIniFile);

 sections := TStringList.Create;



 //Read and process all sections.

 ini.ReadSections(sections);

 for i := 0 to Pred(sections.Count) do

   with PropertiesView1.Categories.Add(TTextualItem, '[' + sections[i] + ']', '') do

     begin

     //Add the keys & values of a section as properties.

       keys := TStringList.Create;

       ini.ReadSection(Sections[i], keys);

       for j := 0 to Pred(keys.Count) do

         Items.Add(TTextualItem, keys[j], ini.ReadString(sections[i], keys[j], ''));

       keys.Free;

     end;



 sections.Free;

 ini.Free;



 //Now we're in bussiness.

 PropertiesView1.OnChange := Change;

end;

G.W. van der Vegt




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users