ple103
ple103

Reputation: 2110

Loading dynamic .ini identifiers

I am creating a virtual version of my student planner which basically lets you make notes what homework you have for what subject.

Here is the interface: enter image description here

The user selects the subject from the combobox and types in some notes in the adjacent memo. When they are done they will click the 'Save' button which will save it into an .ini file. The selected date will become the section name, the subjects will become the identifier and the text in the memo will become the values for each identifier.

Note: There are 7 possible subjects.

My problem is loading the combo boxes and the memos when the date is selected seeing as the identifiers are always different for each date.

For example:

On the 16th of February the user input (interface):

English - Read up to page 127 of novel.
Maths - Complete chapter 6.

For the 16th of February it will look like this in the .ini file:

[16/02/12]
English=Read up to page 127 of novel.
Maths=Complete chapter 6.

On the 20th of February the user inputs (interface):

SOSE - Read textbook.
Legal Studies - Fill in online survey.

For the 20th of February it will look like this in the .ini file:

[20/02/12]
SOSE=Read textbook.
Legal Studies=Fill in online survey.

Now you see if a user selects 16th of February to view what their tasks are, it wouldn't be possible to load because each identifier varies.

Is there a better alternative to the .ini file? How can I go about achieving this?

Upvotes: 3

Views: 1299

Answers (2)

Ken White
Ken White

Reputation: 125669

You can use TIniFile.ReadSections to get the individual dates, and TIniFile.ReadSection to get the individual items within that section. Here's a quick example:

// Sample ini file

[16/02/12]
English=Read up to page 127 of novel.
Maths=Complete chapter 6. 

[20/02/12]
SOSE=Read textbook.
Legal Studies=Fill in online survey. 

Code:

unit Unit2;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, IniFiles;

type
  TForm2 = class(TForm)
    ListBox1: TListBox;
    Memo1: TMemo;
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure ListBox1Click(Sender: TObject);
  private
    { Private declarations }
    FIni: TMemIniFile;
  public
    { Public declarations }
  end;

var
  Form2: TForm2;

implementation

{$R *.dfm}

const
  IniName = 'd:\Temp\SampleNotes.ini';

procedure TForm2.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  FIni.Free;
end;

procedure TForm2.FormCreate(Sender: TObject);
begin
  FIni := TMemIniFile.Create(IniName);
  Memo1.Lines.Clear;
  FIni.ReadSections(ListBox1.Items);
end;

procedure TForm2.ListBox1Click(Sender: TObject);
var
  Section: string;
begin
  if ListBox1.ItemIndex > -1 then
  begin
    Section := ListBox1.Items[ListBox1.ItemIndex];
    FIni.ReadSection(Section, Memo1.Lines);
  end;
end;

end.

The above produces this:

Sample output image Sample output image 2

Upvotes: 10

mghie
mghie

Reputation: 32334

There is no problem with using a TIniFile for this. The class has a method ReadSections() which you can use to read all days with homework, and a method ReadSection() to read all entries for a given day.
You could also use ReadSectionValues() to read complete lines (subject and task) which you would then need to split at the first = char.

Upvotes: 8

Related Questions