使用TDataSet.OnAfterOpen
和TDataSet.OnBeforeClose
事件加载和保存所需的值
procedure TForm3.ClientDataSet1AfterOpen(DataSet: TDataSet);
var
LIni : TIniFile;
begin
DataSet.DisableControls;
try
LIni := TIniFile.Create( '<YourIniFileName.ini>' );
try
DataSet.Locate( 'ID', LIni.ReadString( 'MyDataSet', 'ID', '' ), [] );
finally
LIni.Free;
end;
finally
DataSet.EnableControls;
end;
end;
procedure TForm3.ClientDataSet1BeforeClose(DataSet: TDataSet);
var
LIni : TIniFile;
begin
LIni := TIniFile.Create( '<YourIniFileName.ini>' );
try
LIni.WriteString( 'MyDataSet', 'ID', DataSet.FieldByName( 'ID' ).AsString ) );
finally
LIni.Free;
end;
end;
这是一个非常简单直接的示例,您不应该在实际应用程序中实现它。
在实际应用程序中,您将把它委托给一个负责读取和写入此类应用程序值的单例
// Very simple KeyValue-Store
IKeyValueStore = interface
function GetValue( const Key : string; const Default : string ) : string;
procedure SetValue( const Key : string; const Value : string );
end;
// Global Application Value Store as Singleton
function AppGlobalStore : IKeyValueStore;
并为您提供非常智能的解决方案
procedure TForm3.ClientDataSet1AfterOpen(DataSet: TDataSet);
begin
DataSet.DisableControls;
try
DataSet.Locate( 'ID', AppGlobalStore.GetValue( 'MyDataSet\ID', '' ), [] );
finally
DataSet.EnableControls;
end;
end;
procedure TForm3.ClientDataSet1BeforeClose(DataSet: TDataSet);
begin
AppGlobalStore.SetValue( 'MyDataSet\ID', DataSet.FieldByName( 'ID' ).AsString ), [] );
end;