加载动态.ini标识符 [英] 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:
用户选择组合框中的主题,并在相邻的备忘录中键入一些注释。完成后,他们将点击保存按钮,将其保存到.ini文件中。所选日期将成为部分名称,主题将成为标识符,而备忘录中的文本将成为每个标识符的值。
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.
注意:有7个可能的主题。
我的问题是加载选择日期时的组合框和记事,因为每个日期的标识符总是不同的。
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.
例如:
2月16日,用户输入(界面):
On the 16th of February the user input (interface):
英语-阅读小说的第127页。
数学-完成第6章。
English - Read up to page 127 of novel.
Maths - Complete chapter 6.
2月16日在.ini文件中将如下所示:
For the 16th of February it will look like this in the .ini file:
[16/02/12]
英语=阅读小说的第127页。
数学=完成第6章。
[16/02/12]
English=Read up to page 127 of novel.
Maths=Complete chapter 6.
2月20日,用户输入(界面):
On the 20th of February the user inputs (interface):
SOSE-阅读教科书。
法律研究-填写在线调查。
SOSE - Read textbook.
Legal Studies - Fill in online survey.
在2月20日,.ini文件中的内容将如下所示:
For the 20th of February it will look like this in the .ini file:
[20/02/12]
SOSE =阅读教科书。
法律研究=填写在线调查。
[20/02/12]
SOSE=Read textbook.
Legal Studies=Fill in online survey.
现在,您看到用户是否选择2月16日查看他们的任务,因为每个任务都无法加载标识符有所不同。
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.
.ini文件是否有更好的替代方法?我该如何实现呢?
Is there a better alternative to the .ini file? How can I go about achieving this?
推荐答案
您可以使用 TIniFile.ReadSections
获取单个日期,而 TIniFile.ReadSection
获取该部分中的单个项目。这是一个简单的示例:
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.
代码:
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.
上面是这样的:
< img src = https://i.stack.imgur.com/1mKkR.png alt =样本输出图像>
这篇关于加载动态.ini标识符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!