unit SimpNotes;
{$mode objfpc}{$H+}
interface
uses Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, Interfaces, FileUtil, ExtCtrls;
type
{ TForm1 }
TShortString = String[255]; // Fixed-length string type of 255 characters.
TContentString = String[255]; // Fixed-length string type of 255 characters.
TNotes = packed record
Title: TShortString;
Content: TContentString;
end;
TForm1 = class(TForm)
BtnAdd: TButton;
BtnDelete: TButton;
BtnSave: TButton;
BtnMod: TButton;
BtnNew: TButton;
Memo1: TMemo;
Title: TEdit;
Label1: TLabel;
Label2: TLabel;
TitlesList: TListBox;
Content: TMemo;
procedure BtnAddClick(Sender: TObject);
procedure BtnDeleteClick(Sender: TObject);
procedure BtnModClick(Sender: TObject);
procedure BtnSaveClick(Sender: TObject);
procedure BtnNewClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure TitlesListClick(Sender: TObject);
private
Note: array of TNotes;
NoteCount: Integer;
procedure LoadNotes;
procedure SaveNotes;
public
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
{ TForm1 }
Procedure TForm1.BtnAddClick(Sender: TObject);
Var
NewNote: TNotes;
Begin
If Trim(Title.Text) = '' Then
Begin
ShowMessage('Please enter a title for the note.');
Exit;
End;
If Trim(Content.Text) = '' Then
Begin
ShowMessage('Please enter content for the note.');
Exit;
End;
NewNote.Title := Title.Text;
NewNote.Content := Content.Text;
Inc(NoteCount); // Increment the Note Counter.
SetLength(Note, NoteCount); // Add a blank space for new Note
Note[NoteCount - 1] := NewNote; // Add the new note.
TitlesList.Items.Add(NewNote.Title); // Add the title to the ListBox.
Label2.Caption := 'Notes: ' + IntToStr(NoteCount); // Update the note counter display;
Title.Clear;
Content.Clear;
End;
Procedure TForm1.BtnModClick(Sender: TObject);
Var
I: Integer;
Begin
If TitlesList.ItemIndex >= 0 Then
Begin
I := TitlesList.ItemIndex;
Note[I].Title := Title.Text;
Note[I].Content := Content.Text;
TitlesList.Items[I] := Title.Text; // Update the listbox text
End;
End;
Procedure TForm1.BtnDeleteClick(Sender: TObject);
Var
I: Integer;
Begin
If TitlesList.ItemIndex >= 0 Then
Begin
I := TitlesList.ItemIndex; // This is the highlighted Title in the ListBox
TitlesList.Items.Delete(I); // This removes the Title to be deleted.
Move(Note[I + 1], Note[I], (NoteCount - I - 1) * SizeOf(TNotes)); // Move the below records up one on the list.
Dec(NoteCount); // Decrement the Note Counter.
SetLength(Note, NoteCount); // Set the note array records to NoteCount records.
Label2.Caption := 'Notes: ' + IntToStr(NoteCount); // Update the note counter display;
Title.Clear;
Content.Clear;
End;
End;
Procedure TForm1.BtnSaveClick(Sender: TObject);
Begin
SaveNotes;
End;
Procedure TForm1.BtnNewClick(Sender: TObject);
Begin
Title.Clear;
Content.Clear;
End; // BtnNewClick
Procedure TForm1.FormCreate(Sender: TObject);
Begin
NoteCount := 0; // Init the Note Count
LoadNotes;
Label2.Caption := 'Notes: ' + IntToStr(NoteCount); // Update the note counter display;
End; // FormCreate
Procedure TForm1.TitlesListClick(Sender: TObject);
Begin
If TitlesList.ItemIndex >= 0 Then
Begin
Title.Text := Note[TitlesList.ItemIndex].Title;
Content.Text := Note[TitlesList.ItemIndex].Content;
End;
End; // Proc TitlesListClick
Procedure TForm1.LoadNotes;
Var
F: File of TNotes;
I, ErrorCode: Integer;
Begin
If FileExists('Notes.dat') Then
Begin
AssignFile(F, 'Notes.dat');
{$I-} // Disable I/O checking
Reset(F);
ErrorCode := IOResult;
Memo1.Lines.Add('Notes Opened: ' + IntToStr(ErrorCode));
{$I+} // Enable I/O checking
If ErrorCode <> 0 Then
Begin
ShowMessage('Error opening notes file: ' + SysErrorMessage(ErrorCode));
NoteCount := 0; SetLength(Note, 0); Exit;
End;
Try
NoteCount := {FileSize(F)}512 div SizeOf(TNotes);
Memo1.Lines.Add('FileSize: ' + IntToStr(FileSize(F)));
Memo1.Lines.Add('Size Of - TNotes: ' + IntToStr(SizeOf(TNotes)));
Memo1.Lines.Add('Note Count: ' + IntToStr(NoteCount));
SetLength(Note, NoteCount);
For I := 0 to NoteCount - 1 Do
Begin
{$I-} // Disable I/O checking
Read(F, Note[I]);
ErrorCode := IOResult;
Memo1.Lines.Add('Reading Notes: ' + IntToStr(ErrorCode));
{$I+} // Enable I/O checking
If ErrorCode <> 0 Then
Begin
ShowMessage('Error reading notes file at index: ' + IntToStr(I) + ' - ' + SysErrorMessage(ErrorCode));
NoteCount := 0;
SetLength(Note, 0);
TitlesList.Clear;
CloseFile(F);
Exit;
End; // ErrorCode
TitlesList.Items.Add(Note[I].Title);
End; // For I
Finally
{$I-} // Disable I/O checking
CloseFile(F);
ErrorCode := IOResult;
{$I+} // Enable I/O checking
If ErrorCode <> 0 Then
Begin
ShowMessage('Error closing notes file: ' + SysErrorMessage(ErrorCode));
End;
End; // Finally
End // FileExists
Else
Begin
NoteCount := 0;
SetLength(Note, 0);
End; // Else
End; // Proc LoadNotes
Procedure TForm1.SaveNotes;
Var
F: File of TNotes;
I: Integer;
Begin
AssignFile(F, 'Notes.dat');
Rewrite(F);
For I := 0 to NoteCount - 1 Do
Begin
Write(F, Note[I]);
End;
CloseFile(F);
End; // Proc SaveNotes
end.