type
TForm1 = class(TForm)
btnPrev: TSpeedButton;
btnNext: TSpeedButton;
Label1: TLabel;
btnAdd: TSpeedButton;
btnDel: TSpeedButton;
btnLast: TSpeedButton;
btnFirst: TSpeedButton;
btnNextNew: TSpeedButton;
DataSource1: TDataSource;
DBRichEdit1: TDBRichEdit;
ADOConnection1: TADOConnection;
ADOQuery1: TADOQuery;
ToolBar1: TToolBar;
tbtnBold: TToolButton;
tbtnItalic: TToolButton;
tbtnUnderline: TToolButton;
procedure FormCreate(Sender: TObject);
procedure btnNextClick(Sender: TObject);
procedure btnPrevClick(Sender: TObject);
procedure btnDelClick(Sender: TObject);
procedure btnAddClick(Sender: TObject);
procedure btnLastClick(Sender: TObject);
procedure btnFirstClick(Sender: TObject);
procedure JumpToLastNote;
procedure JumpToNextNote;
procedure JumpToPrevNote;
procedure JumpToFirstNote;
procedure tbtnBoldClick(Sender: TObject);
procedure DBRichEdit1KeyPress(Sender: TObject; var Key: Char);
procedure NewNote(text:string='');
protected
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
if ADOQuery1.RecordCount = 0
then NewNote('Новая заметка');
Label1.Caption:=IntToStr(ADOQuery1.RecNo)+' / '+IntToStr(ADOQuery1.RecordCount);
end;
procedure TForm1.btnNextClick(Sender: TObject);
begin
if ADOQuery1.RecNO>=ADOQuery1.RecordCount-1
then btnNextNew.Visible:=true;
if ADOQuery1.RecNo=ADOQuery1.RecordCount
then NewNote('Новая заметка',false)
else JumpToNextNote;
Label1.Caption:=IntToStr(ADOQuery1.RecNo)+' / '+IntToStr(ADOQuery1.RecordCount);
end;
procedure TForm1.btnPrevClick(Sender: TObject);
begin
JumpToPrevNote;
if ADOQuery1.RecNo<>ADOQuery1.RecordCount
then btnNextNew.Visible:=false;
Label1.Caption:=IntToStr(ADOQuery1.RecNo)+' / '+IntToStr(ADOQuery1.RecordCount);
end;
procedure TForm1.btnDelClick(Sender: TObject);
begin
if MessageDlgPos('Удалить заметку?', mtConfirmation, [mbYes, mbCancel], 0, Form1.Left+(Form1.Width-160) div 2, Form1.Top+(Form1.Height-80) div 2)<>mrOK
then exit;
//Удаление текущей записи
ADOQuery1.Delete;
if ADOQuery1.RecNo=ADOQuery1.RecordCount
then btnNextNew.Visible:=true;
if ADOQuery1.RecordCount = 0
then NewNote('Новая заметка');
Label1.Caption:=IntToStr(ADOQuery1.RecNo)+' / '+IntToStr(ADOQuery1.RecordCount);
end;
procedure TForm1.btnAddClick(Sender: TObject);
begin
if AddNoteToEnd
then NewNote(NewNoteText,false)
else NewNote(NewNoteText,true);
if ADOQuery1.RecNo = ADOQuery1.RecordCount
then Form1.btnNextNew.Visible:=true;
Label1.Caption:=IntToStr(ADOQuery1.RecNo)+' / '+IntToStr(ADOQuery1.RecordCount);
end;
procedure TForm1.btnLastClick(Sender: TObject);
begin
JumpToLastNote;
if ADOQuery1.RecNo=ADOQuery1.RecordCount
then btnNextNew.Visible:=true;
Label1.Caption:=IntToStr(ADOQuery1.RecNo)+' / '+IntToStr(ADOQuery1.RecordCount);
end;
procedure TForm1.btnFirstClick(Sender: TObject);
begin
JumpToFirstNote;
if ADOQuery1.RecNo<>ADOQuery1.RecordCount
then btnNextNew.Visible:=false;
Label1.Caption:=IntToStr(ADOQuery1.RecNo)+' / '+IntToStr(ADOQuery1.RecordCount);
end;
procedure TForm1.JumpToLastNote;
begin
ADOQuery1.CheckBrowseMode;
ADOQuery1.RecNo := ADOQuery1.RecordCount;
end;
procedure TForm1.JumpToNextNote;
begin
ADOQuery1.CheckBrowseMode;
if ADOQuery1.RecNo <> ADOQuery1.RecordCount
then ADOQuery1.RecNo := ADOQuery1.RecNo +1
else NewNote;
end;
procedure TForm1.NewNote(text: string);
var
num, RecNo:integer;
begin
if ADOQuery1.RecordCount > 0
then ADOQuery1.CheckBrowseMode;
RecNo := ADOQuery1.RecordCount;
DBRichEdit1.DataField := '';
if ADOQuery1.RecordCount = 0
then num := 1
else begin
//Получение номера последней заметки, т.к. сортировка идёт по номеру, т.е. по полю Number
RecNo := ADOQuery1.RecNo;
num := ADOQuery1.FieldByName('Number').AsInteger +1
end;
text := text+' '+IntToStr(Num);
ADOQuery1.Active := false;
ADOQuery1.SQL.Clear;
// Добавление новой записи. Немного извращенский способ, но выбор пал на него, т.к. необходимо хранить дату добавления
ADOQuery1.SQL.Text := 'Insert Into Notes([Number],[Date_add]) Values('+
IntToStr(num)+','+''''+DateTimeToStr(Now)
+''''+')';
ADOQuery1.ExecSQL;
//Возвращаемся к старому запросу для отображения всех данных с сортировкой по полю Number
ADOQuery1.SQL.Text := 'Select * from Notes Order By Number';
ADOQuery1.Active := true;
DBRichEdit1.DataField := 'Note';
ADOQuery1.RecNo := RecNo + 1;
ADOQuery1.Edit;
DBRichEdit1.Text := Text;
ADOQuery1.Post;
end;
procedure TForm1.JumpToFirstNote;
begin
ADOQuery1.CheckBrowseMode;
ADOQuery1.RecNo := 1;
end;
procedure TForm1.JumpToPrevNote;
begin
ADOQuery1.CheckBrowseMode;
if ADOQuery1.RecNo <> 1
then ADOQuery1.RecNo := ADOQuery1.RecNo -1
end;
procedure TForm1.tbtnBoldClick(Sender: TObject);
begin
if (Sender as TToolButton).Caption = 'Жирный'
then begin
if fsBold in DBRichEdit1.SelAttributes.Style
then DBRichEdit1.SelAttributes.Style :=
DBRichEdit1.SelAttributes.Style - [fsBold]
else DBRichEdit1.SelAttributes.Style :=
DBRichEdit1.SelAttributes.Style + [fsBold];
end else
if (Sender as TToolButton).Caption = 'Курсив'
then begin
if fsItalic in DBRichEdit1.SelAttributes.Style
then DBRichEdit1.SelAttributes.Style :=
DBRichEdit1.SelAttributes.Style - [fsItalic]
else DBRichEdit1.SelAttributes.Style :=
DBRichEdit1.SelAttributes.Style + [fsItalic];
end else
if (Sender as TToolButton).Caption = 'Подчёркнутый'
then begin
if fsUnderline in DBRichEdit1.SelAttributes.Style
then DBRichEdit1.SelAttributes.Style :=
DBRichEdit1.SelAttributes.Style - [fsUnderline]
else DBRichEdit1.SelAttributes.Style :=
DBRichEdit1.SelAttributes.Style + [fsUnderline];
end;
ADOQuery1.CheckBrowseMode;
end;
procedure TForm1.DBRichEdit1KeyPress(Sender: TObject; var Key: Char);
begin
case Ord(Key) of
02{CTRL+B}:begin
key:=#0;
tbtnBold.Click;
end;
09{CTRL+I}:begin
key:=#0;
tbtnItalic.Click;
end;
21{CTRL+U}:begin
key:=#0;
tbtnUnderline.Click;
end;
end;
end;
end.