在 TListBox 中存储、检索、保存和加载数据的更好方法?
Better approach for storing, retrieving, saving and loading data in a TListBox?
在Delphi 10.4.2 win-32 VCL Application Windows 10 中,我使用简单的TListBox
将记录存储在ListBox 项中。当用户单击某个项目时,将检索关联记录的数据:
type
PResizeSettingsRec = ^TResizeSettingsRec;
TResizeSettingsRec = record
TopValue: Integer;
LeftValue: Integer;
RightValue: Integer;
BottomValue: Integer;
Color: TColor;
Opacity: Integer;
end;
procedure TformMain.btnAddCurrentSettingsClick(Sender: TObject);
var
P: PResizeSettingsRec;
begin
New(P);
P.TopValue := 5;
P.LeftValue := 5;
P.RightValue := 5;
P.BottomValue := 5;
P.Color := clRed;
P.Opacity := 255;
listboxMultiResize.AddItem('Step 1', TObject(P));
New(P);
P.TopValue := 9;
P.LeftValue := 9;
P.RightValue := 9;
P.BottomValue := 9;
P.Color := clBlue;
P.Opacity := 127;
listboxMultiResize.AddItem('Step 2', TObject(P));
end;
procedure TformMain.FormDestroy(Sender: TObject);
begin
for var i := 0 to listboxMultiResize.Items.Count - 1 do
Dispose(PResizeSettingsRec(listboxMultiResize.Items.Objects[i]));
end;
procedure TformMain.listboxMultiResizeClick(Sender: TObject);
var
P: PResizeSettingsRec;
begin
if listboxMultiResize.ItemIndex < 0 then EXIT;
P := PResizeSettingsRec(listboxMultiResize.Items.Objects[listboxMultiResize.ItemIndex]);
CodeSite.Send('TformMain.listboxMultiResizeClick: P.TopValue', P.TopValue);
CodeSite.Send('TformMain.listboxMultiResizeClick: P.LeftValue', P.LeftValue);
CodeSite.Send('TformMain.listboxMultiResizeClick: P.RightValue', P.RightValue);
CodeSite.Send('TformMain.listboxMultiResizeClick: P.BottomValue', P.BottomValue);
CodeSite.Send('TformMain.listboxMultiResizeClick: ColorToString(P.Color)', ColorToString(P.Color));
CodeSite.Send('TformMain.listboxMultiResizeClick: P.Opacity', P.Opacity);
end;
这种方法故意简单,以使其稳定且万无一失。或者是否有更好、更现代、更简单的方法来实现这一点?
有没有一种简单的方法可以将 ListBox 项及其数据一起存储到一个文件中,然后再从该文件中重新加载它们? (目前,我使用的是 INI 文件)。
编辑: 按照 Andreas 的建议,我现在使用此代码:
type
TResizeSettings = class
TopValue: Integer;
LeftValue: Integer;
RightValue: Integer;
BottomValue: Integer;
Color: TColor;
Opacity: Integer;
end;
procedure TformMain.btnAddCurrentSettingsClick(Sender: TObject);
var
ThisResizeSettings: TResizeSettings;
begin
ThisResizeSettings := TResizeSettings.Create;
try
ThisResizeSettings.TopValue := 5;
ThisResizeSettings.LeftValue := 5;
ThisResizeSettings.RightValue := 5;
ThisResizeSettings.BottomValue := 5;
ThisResizeSettings.Color := clRed;
ThisResizeSettings.Opacity := 255;
listboxMultiResize.AddItem('Step 1', ThisResizeSettings);
finally
ThisResizeSettings.Free;
end;
ThisResizeSettings := TResizeSettings.Create;
try
ThisResizeSettings.TopValue := 9;
ThisResizeSettings.LeftValue := 9;
ThisResizeSettings.RightValue := 9;
ThisResizeSettings.BottomValue := 9;
ThisResizeSettings.Color := clBlue;
ThisResizeSettings.Opacity := 127;
listboxMultiResize.AddItem('Step 2', ThisResizeSettings);
finally
ThisResizeSettings.Free;
end;
end;
procedure TformMain.FormDestroy(Sender: TObject);
begin
for var i := 0 to listboxMultiResize.Items.Count - 1 do
begin
TResizeSettings(listboxMultiResize.Items.Objects[i]).Free;
end;
end;
procedure TformMain.listboxMultiResizeClick(Sender: TObject);
var
ThisResizeSettings: TResizeSettings;
begin
if listboxMultiResize.ItemIndex < 0 then EXIT;
ThisResizeSettings := TResizeSettings(listboxMultiResize.Items.Objects[listboxMultiResize.ItemIndex]);
CodeSite.Send('TformMain.listboxMultiResizeClick: TopValue', ThisResizeSettings.TopValue);
CodeSite.Send('TformMain.listboxMultiResizeClick: LeftValue', ThisResizeSettings.LeftValue);
CodeSite.Send('TformMain.listboxMultiResizeClick: RightValue', ThisResizeSettings.RightValue);
CodeSite.Send('TformMain.listboxMultiResizeClick: BottomValue', ThisResizeSettings.BottomValue);
CodeSite.Send('TformMain.listboxMultiResizeClick: ColorToString(P.Color)', ColorToString(ThisResizeSettings.Color));
CodeSite.Send('TformMain.listboxMultiResizeClick: Opacity', ThisResizeSettings.Opacity);
end;
但是,我在单击列表框项目时得到随机结果!
EDIT2: 在 Andreas 的回答之后,我现在不再释放创建的对象:
procedure TformMain.btnAddCurrentSettingsClick(Sender: TObject);
//
var
ThisResizeSettings: TResizeSettings;
begin
ThisResizeSettings := TResizeSettings.Create;
ThisResizeSettings.TopValue := 5;
ThisResizeSettings.LeftValue := 5;
ThisResizeSettings.RightValue := 5;
ThisResizeSettings.BottomValue := 5;
ThisResizeSettings.Color := clRed;
ThisResizeSettings.Opacity := 255;
listboxMultiResize.AddItem('Step 1', ThisResizeSettings);
ThisResizeSettings := TResizeSettings.Create;
ThisResizeSettings.TopValue := 9;
ThisResizeSettings.LeftValue := 9;
ThisResizeSettings.RightValue := 9;
ThisResizeSettings.BottomValue := 9;
ThisResizeSettings.Color := clBlue;
ThisResizeSettings.Opacity := 127;
listboxMultiResize.AddItem('Step 2', ThisResizeSettings);
end;
我还更改了 FormDestroy
中 ListBox 对象的释放:
procedure TformMain.FormDestroy(Sender: TObject);
begin
//
for var i := listboxMultiResize.Items.Count - 1 downto 0 do
begin
listboxMultiResize.Items.Objects[i].Free;
listboxMultiResize.Items.Objects[i] := nil;
end;
现在看来可以了。谢谢你,安德烈亚斯!
使用对象代替记录。然后就不需要丑陋的 TObject
转换了,因为你实际上提供了一个对象!这就是(主要)应该使用它的方式。
记住:class 不需要有 getter 和 setter,默认情况下基于 TObject
的 class 具有成员可见性 public
。如果不需要,则不需要带有 getter 和 setter 的私有字段和 public 属性。此外,记录是值类型,而对象是引用类型(您需要创建和释放它们),但在您的情况下,引用类型更方便。此外,您已经在进行分配和释放,尽管是以一种古老的方式!
这是一个简单的例子:
type
TPerson = class
Name: string;
Age: Integer;
end;
TForm1 = class(TForm)
lbPersons: TListBox;
eName: TEdit;
lblName: TLabel;
eAge: TEdit;
lblAge: TLabel;
btnAddUpd: TButton;
procedure FormDestroy(Sender: TObject);
procedure btnAddUpdClick(Sender: TObject);
procedure lbPersonsClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
implementation
{$R *.dfm}
procedure TForm1.btnAddUpdClick(Sender: TObject);
begin
var idx := lbPersons.Items.IndexOf(eName.Text);
var Person: TPerson;
if idx <> -1 then
Person := lbPersons.Items.Objects[idx] as TPerson
else
Person := TPerson.Create;
Person.Name := eName.Text;
Person.Age := StrToInt(eAge.Text);
if idx = -1 then
lbPersons.Items.AddObject(Person.Name, Person);
if eName.CanFocus then
eName.SetFocus;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
for var i := lbPersons.Items.Count - 1 downto 0 do
begin
lbPersons.Items.Objects[i].Free;
lbPersons.Items.Objects[i] := nil;
end;
end;
procedure TForm1.lbPersonsClick(Sender: TObject);
begin
if lbPersons.ItemIndex = -1 then
begin
eName.Text := '';
eAge.Text := '';
end
else
begin
var Person := lbPersons.Items.Objects[lbPersons.ItemIndex] as TPerson;
eName.Text := Person.Name;
eAge.Text := Person.Age.ToString;
end;
end;
在Delphi 10.4.2 win-32 VCL Application Windows 10 中,我使用简单的TListBox
将记录存储在ListBox 项中。当用户单击某个项目时,将检索关联记录的数据:
type
PResizeSettingsRec = ^TResizeSettingsRec;
TResizeSettingsRec = record
TopValue: Integer;
LeftValue: Integer;
RightValue: Integer;
BottomValue: Integer;
Color: TColor;
Opacity: Integer;
end;
procedure TformMain.btnAddCurrentSettingsClick(Sender: TObject);
var
P: PResizeSettingsRec;
begin
New(P);
P.TopValue := 5;
P.LeftValue := 5;
P.RightValue := 5;
P.BottomValue := 5;
P.Color := clRed;
P.Opacity := 255;
listboxMultiResize.AddItem('Step 1', TObject(P));
New(P);
P.TopValue := 9;
P.LeftValue := 9;
P.RightValue := 9;
P.BottomValue := 9;
P.Color := clBlue;
P.Opacity := 127;
listboxMultiResize.AddItem('Step 2', TObject(P));
end;
procedure TformMain.FormDestroy(Sender: TObject);
begin
for var i := 0 to listboxMultiResize.Items.Count - 1 do
Dispose(PResizeSettingsRec(listboxMultiResize.Items.Objects[i]));
end;
procedure TformMain.listboxMultiResizeClick(Sender: TObject);
var
P: PResizeSettingsRec;
begin
if listboxMultiResize.ItemIndex < 0 then EXIT;
P := PResizeSettingsRec(listboxMultiResize.Items.Objects[listboxMultiResize.ItemIndex]);
CodeSite.Send('TformMain.listboxMultiResizeClick: P.TopValue', P.TopValue);
CodeSite.Send('TformMain.listboxMultiResizeClick: P.LeftValue', P.LeftValue);
CodeSite.Send('TformMain.listboxMultiResizeClick: P.RightValue', P.RightValue);
CodeSite.Send('TformMain.listboxMultiResizeClick: P.BottomValue', P.BottomValue);
CodeSite.Send('TformMain.listboxMultiResizeClick: ColorToString(P.Color)', ColorToString(P.Color));
CodeSite.Send('TformMain.listboxMultiResizeClick: P.Opacity', P.Opacity);
end;
这种方法故意简单,以使其稳定且万无一失。或者是否有更好、更现代、更简单的方法来实现这一点?
有没有一种简单的方法可以将 ListBox 项及其数据一起存储到一个文件中,然后再从该文件中重新加载它们? (目前,我使用的是 INI 文件)。
编辑: 按照 Andreas 的建议,我现在使用此代码:
type
TResizeSettings = class
TopValue: Integer;
LeftValue: Integer;
RightValue: Integer;
BottomValue: Integer;
Color: TColor;
Opacity: Integer;
end;
procedure TformMain.btnAddCurrentSettingsClick(Sender: TObject);
var
ThisResizeSettings: TResizeSettings;
begin
ThisResizeSettings := TResizeSettings.Create;
try
ThisResizeSettings.TopValue := 5;
ThisResizeSettings.LeftValue := 5;
ThisResizeSettings.RightValue := 5;
ThisResizeSettings.BottomValue := 5;
ThisResizeSettings.Color := clRed;
ThisResizeSettings.Opacity := 255;
listboxMultiResize.AddItem('Step 1', ThisResizeSettings);
finally
ThisResizeSettings.Free;
end;
ThisResizeSettings := TResizeSettings.Create;
try
ThisResizeSettings.TopValue := 9;
ThisResizeSettings.LeftValue := 9;
ThisResizeSettings.RightValue := 9;
ThisResizeSettings.BottomValue := 9;
ThisResizeSettings.Color := clBlue;
ThisResizeSettings.Opacity := 127;
listboxMultiResize.AddItem('Step 2', ThisResizeSettings);
finally
ThisResizeSettings.Free;
end;
end;
procedure TformMain.FormDestroy(Sender: TObject);
begin
for var i := 0 to listboxMultiResize.Items.Count - 1 do
begin
TResizeSettings(listboxMultiResize.Items.Objects[i]).Free;
end;
end;
procedure TformMain.listboxMultiResizeClick(Sender: TObject);
var
ThisResizeSettings: TResizeSettings;
begin
if listboxMultiResize.ItemIndex < 0 then EXIT;
ThisResizeSettings := TResizeSettings(listboxMultiResize.Items.Objects[listboxMultiResize.ItemIndex]);
CodeSite.Send('TformMain.listboxMultiResizeClick: TopValue', ThisResizeSettings.TopValue);
CodeSite.Send('TformMain.listboxMultiResizeClick: LeftValue', ThisResizeSettings.LeftValue);
CodeSite.Send('TformMain.listboxMultiResizeClick: RightValue', ThisResizeSettings.RightValue);
CodeSite.Send('TformMain.listboxMultiResizeClick: BottomValue', ThisResizeSettings.BottomValue);
CodeSite.Send('TformMain.listboxMultiResizeClick: ColorToString(P.Color)', ColorToString(ThisResizeSettings.Color));
CodeSite.Send('TformMain.listboxMultiResizeClick: Opacity', ThisResizeSettings.Opacity);
end;
但是,我在单击列表框项目时得到随机结果!
EDIT2: 在 Andreas 的回答之后,我现在不再释放创建的对象:
procedure TformMain.btnAddCurrentSettingsClick(Sender: TObject);
//
var
ThisResizeSettings: TResizeSettings;
begin
ThisResizeSettings := TResizeSettings.Create;
ThisResizeSettings.TopValue := 5;
ThisResizeSettings.LeftValue := 5;
ThisResizeSettings.RightValue := 5;
ThisResizeSettings.BottomValue := 5;
ThisResizeSettings.Color := clRed;
ThisResizeSettings.Opacity := 255;
listboxMultiResize.AddItem('Step 1', ThisResizeSettings);
ThisResizeSettings := TResizeSettings.Create;
ThisResizeSettings.TopValue := 9;
ThisResizeSettings.LeftValue := 9;
ThisResizeSettings.RightValue := 9;
ThisResizeSettings.BottomValue := 9;
ThisResizeSettings.Color := clBlue;
ThisResizeSettings.Opacity := 127;
listboxMultiResize.AddItem('Step 2', ThisResizeSettings);
end;
我还更改了 FormDestroy
中 ListBox 对象的释放:
procedure TformMain.FormDestroy(Sender: TObject);
begin
//
for var i := listboxMultiResize.Items.Count - 1 downto 0 do
begin
listboxMultiResize.Items.Objects[i].Free;
listboxMultiResize.Items.Objects[i] := nil;
end;
现在看来可以了。谢谢你,安德烈亚斯!
使用对象代替记录。然后就不需要丑陋的 TObject
转换了,因为你实际上提供了一个对象!这就是(主要)应该使用它的方式。
记住:class 不需要有 getter 和 setter,默认情况下基于 TObject
的 class 具有成员可见性 public
。如果不需要,则不需要带有 getter 和 setter 的私有字段和 public 属性。此外,记录是值类型,而对象是引用类型(您需要创建和释放它们),但在您的情况下,引用类型更方便。此外,您已经在进行分配和释放,尽管是以一种古老的方式!
这是一个简单的例子:
type
TPerson = class
Name: string;
Age: Integer;
end;
TForm1 = class(TForm)
lbPersons: TListBox;
eName: TEdit;
lblName: TLabel;
eAge: TEdit;
lblAge: TLabel;
btnAddUpd: TButton;
procedure FormDestroy(Sender: TObject);
procedure btnAddUpdClick(Sender: TObject);
procedure lbPersonsClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
implementation
{$R *.dfm}
procedure TForm1.btnAddUpdClick(Sender: TObject);
begin
var idx := lbPersons.Items.IndexOf(eName.Text);
var Person: TPerson;
if idx <> -1 then
Person := lbPersons.Items.Objects[idx] as TPerson
else
Person := TPerson.Create;
Person.Name := eName.Text;
Person.Age := StrToInt(eAge.Text);
if idx = -1 then
lbPersons.Items.AddObject(Person.Name, Person);
if eName.CanFocus then
eName.SetFocus;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
for var i := lbPersons.Items.Count - 1 downto 0 do
begin
lbPersons.Items.Objects[i].Free;
lbPersons.Items.Objects[i] := nil;
end;
end;
procedure TForm1.lbPersonsClick(Sender: TObject);
begin
if lbPersons.ItemIndex = -1 then
begin
eName.Text := '';
eAge.Text := '';
end
else
begin
var Person := lbPersons.Items.Objects[lbPersons.ItemIndex] as TPerson;
eName.Text := Person.Name;
eAge.Text := Person.Age.ToString;
end;
end;