Hy there.
I have some code where I store data for 365 records (for every day in a year) in delphi, some kind of list as far as I understood (a good man!!! Sir Rufo on stackoverflow helped me with that part of code, writing whole example for me! Thanks for that.), using procedure initdata, procedure storecurrent, procedure loadcurrent, and procedure savedata. So far everything as he made works, but he left me the savedata procedure empty. So far, for a single memo I've successfuly saved the text to a file using file of, assign and then rewrite with write (file, var of record. But here with this array[1..365] of record I got stuck. Can'f find a way to tell the Write(file, var of record[x]) what to acctually write into file. Since I'm not really familiar with those initdata and savedata procedures, and can't get to find anything on google :S
Here's his complete code (as mine is a lot longer, and has a lot added already, having 8 elements in each record, etc....]
unit RecordEdit_ViewU;
interface
uses
SysUtils,
Controls, Forms, Dialogs, StdCtrls;
type
TPerson = record
Firstname : string[50]; // shortstring !!
Lastname : string[50]; // shortstring !!
end;
TRecordEdit_View = class( TForm )
Current_Edit : TEdit;
Data_Firstname_Edit : TEdit;
Data_Lastname_Edit : TEdit;
Data_Prev_Button : TButton;
Data_Next_Button : TButton;
Data_Save_Button : TButton;
procedure FormCreate( Sender : TObject );
procedure Current_EditChange( Sender : TObject );
procedure Data_Prev_ButtonClick( Sender : TObject );
procedure Data_Next_ButtonClick( Sender : TObject );
procedure Data_Save_ButtonClick( Sender : TObject );
private
FData : array [1 .. 365] of TPerson;
FCurrent : Integer;
procedure SetCurrent( const Value : Integer );
procedure InitData;
procedure StoreCurrent;
procedure LoadCurrent;
procedure SaveData;
public
property Current : Integer read FCurrent write SetCurrent;
end;
var
RecordEdit_View : TRecordEdit_View;
implementation
{$R *.dfm}
procedure TRecordEdit_View.Current_EditChange( Sender : TObject );
begin
Current := StrToIntDef( Current_Edit.Text, 0 ); // convert text to integer
end;
procedure TRecordEdit_View.Data_Next_ButtonClick( Sender : TObject );
begin
Current := Current + 1; // next record
end;
procedure TRecordEdit_View.Data_Prev_ButtonClick( Sender : TObject );
begin
Current := Current - 1; // prev record
end;
procedure TRecordEdit_View.Data_Save_ButtonClick( Sender : TObject );
begin
SaveData;
end;
procedure TRecordEdit_View.FormCreate( Sender : TObject );
begin
InitData;
end;
procedure TRecordEdit_View.InitData;
begin
FCurrent := Low( FData ); // first record
LoadCurrent; // load data from record
end;
procedure TRecordEdit_View.LoadCurrent;
begin
// Data from record to controls
Data_Firstname_Edit.Text := FData[Current].Firstname;
Data_Lastname_Edit.Text := FData[Current].Lastname;
// Update the Current-Edit
Current_Edit.Text := IntToStr( Current );
end;
procedure TRecordEdit_View.SaveData;
begin
ShowMessage( 'Needs to be implemented!' );
end;
procedure TRecordEdit_View.SetCurrent( const Value : Integer );
begin
// check, if we have a change and if we can change to the new index
if ( Value <> Current ) and ( Value >= Low( FData ) ) and ( Value <= High( FData ) )
then
begin
StoreCurrent; // store data from controls
FCurrent := Value; // change current index
LoadCurrent; // load data from record
end;
end;
procedure TRecordEdit_View.StoreCurrent;
begin
// Data from controls to record
FData[Current].Firstname := Data_Firstname_Edit.Text;
FData[Current].Lastname := Data_Lastname_Edit.Text;
end;
end.
There, where I ged ShowMessage('Needs to be implemented!') I really don't know what to do. I mean, I've tryed what I thought would work but no luck so far. Any hints, on what to look for, so I'd figure this out? Because more than make this work I'd like to understand how and why it works then (of course, I hope I'll get it work as well.. :D ). Then, I need to also read that file in another form, but I think I'll get it done once knowing how to at least write it (since for a single record or array, without storing that file in delphi, I successfully managed to do that!)
And yes, I am kind of beginner still... but learning quick ;)
Thanks. :)