Loading dynamic .ini identifiers

こ雲淡風輕ζ 提交于 2019-12-10 18:25:21

问题


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:

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?


回答1:


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:




回答2:


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.



来源:https://stackoverflow.com/questions/9338283/loading-dynamic-ini-identifiers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!