home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Delphi Programming Unleashed
/
Delphi_Programming_Unleashed_SAMS_Publishing_1995.iso
/
chap20
/
dsevents
/
main.pas
< prev
next >
Wrap
Pascal/Delphi Source File
|
1995-03-20
|
3KB
|
137 lines
unit Main;
{ Program copyright (c) 1994 by Charles Calvert }
{ Project Name: DSEVENTS }
{ This program shows how you can use the built in
Delphi events to create ersatz data aware controls.
In short, it converts standard TEdit controls and
makes them act as if they were TdbEdit controls.
All the action is done programmatically in
the program. That is, I am not changint the behavior
of class TEdit.
When you Post, you need to be sure the DataSource1DataChange
method does not get called. I use a Boolean value called
UpDating to enforce this rule. }
interface
uses
WinTypes, WinProcs, Classes,
Graphics, Forms, Controls,
DB, StdCtrls, DBCtrls,
DBTables, SysUtils, ExtCtrls;
type
TForm1 = class(TForm)
Table1: TTable;
DataSource1: TDataSource;
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Edit4: TEdit;
Edit: TButton;
Insert: TButton;
Label1: TLabel;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
Label2: TLabel;
Edit5: TEdit;
First: TButton;
Prior: TButton;
Next: TButton;
Last: TButton;
Post: TButton;
Panel1: TPanel;
Label6: TLabel;
Cancel: TButton;
Delete: TButton;
procedure FormCreate(Sender: TObject);
procedure AnyButtonClick(Sender: TObject);
procedure DataSource1StateChange(Sender: TObject);
procedure Edit1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure DataSource1UpdateData(Sender: TObject);
procedure DataSource1DataChange(Sender: TObject; Field: TField);
private
UpDating: Boolean;
Edits: array[1..5] of TEdit;
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.FormCreate(Sender: TObject);
var
i: Integer;
begin
for i := 1 to 5 do
Edits[i] := TEdit(FindComponent('Edit' + IntToStr(i)));
Table1.First;
end;
procedure TForm1.DataSource1UpdateData(Sender: TObject);
var
i: Integer;
begin
for i := 1 to 5 do
Table1.Fields[i - 1].AsString := Edits[i].Text;
end;
procedure TForm1.DataSource1StateChange(Sender: TObject);
var
S: String;
begin
case Table1.State of
dsInactive: S := 'Inactive';
dsBrowse: S := 'Browse';
dsEdit: S := 'Edit';
dsInsert: S := 'Insert';
dsSetKey: S := 'SetKey';
end;
Label6.Caption := S;
end;
procedure TForm1.AnyButtonClick(Sender: TObject);
begin
case (Sender as TButton).Tag of
1: Table1.First;
2: Table1.Prior;
3: Table1.Next;
4: Table1.Last;
5: Table1.Edit;
6: Table1.Insert;
7: begin
UpDating := True;
Table1.Post;
UpDating := False;
end;
8: Table1.Cancel;
9: Table1.Delete;
end;
end;
procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if DataSource1.State <> dsEdit then
Table1.Edit;
end;
procedure TForm1.DataSource1DataChange(Sender: TObject; Field: TField);
var
i: Integer;
begin
if UpDating then Exit;
for i := 1 to 5 do
Edits[i].Text := Table1.Fields[i - 1].AsString;
end;
end.