CheckF.pas
上传用户:fh681027
上传日期:2022-07-23
资源大小:1959k
文件大小:2k
源码类别:

Delphi控件源码

开发平台:

Delphi

  1. unit CheckF;
  2. interface
  3. uses
  4.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  5.   Db, DBTables, StdCtrls, Mask, DBCtrls, ExtCtrls, Grids, DBGrids, ComCtrls;
  6. type
  7.   TDbaForm = class(TForm)
  8.     Table1: TTable;
  9.     DataSource1: TDataSource;
  10.     Table1LastName: TStringField;
  11.     Table1FirstName: TStringField;
  12.     Table1Department: TSmallintField;
  13.     Table1Branch: TStringField;
  14.     Table1Senior: TBooleanField;
  15.     Table1HireDate: TDateField;
  16.     DBGrid1: TDBGrid;
  17.     DBCheckBox1: TDBCheckBox;
  18.     procedure FormCreate(Sender: TObject);
  19.     procedure DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
  20.       DataCol: Integer; Column: TColumn; State: TGridDrawState);
  21.     procedure DBGrid1ColEnter(Sender: TObject);
  22.     procedure DBGrid1KeyPress(Sender: TObject; var Key: Char);
  23.   private
  24.     { Private declarations }
  25.   end;
  26. var
  27.   DbaForm: TDbaForm;
  28. implementation
  29. {$R *.DFM}
  30. procedure TDbaForm.FormCreate(Sender: TObject);
  31. begin
  32.   if not Table1.Exists then
  33.   begin
  34.     Table1.CreateTable;
  35.     ShowMessage ('You can add data to this table'#13 +
  36.       'by using the DbAware example of Chapter 9');
  37.   end;
  38.   Table1.Open;
  39. end;
  40. procedure TDbaForm.DBGrid1DrawColumnCell(Sender: TObject;
  41.   const Rect: TRect; DataCol: Integer; Column: TColumn;
  42.   State: TGridDrawState);
  43. begin
  44.   if (gdFocused in State) and
  45.     (Column.Field = Table1Senior) then
  46.   begin
  47.     DBCheckBox1.SetBounds (
  48.       Rect.Left + DBGrid1.Left + 1,
  49.       Rect.Top + DBGrid1.Top + 1,
  50.       Rect.Right - Rect.Left + 1,
  51.       Rect.Bottom - Rect.Top + 1);
  52.   end;
  53. end;
  54. procedure TDbaForm.DBGrid1ColEnter(Sender: TObject);
  55. begin
  56.   if DBGrid1.Columns [DBGrid1.SelectedIndex].
  57.       Field = Table1Senior then
  58.     DBCheckBox1.Visible := True
  59.   else
  60.     DBCheckBox1.Visible := False;
  61. end;
  62. procedure TDbaForm.DBGrid1KeyPress(Sender: TObject; var Key: Char);
  63. begin
  64.   if DBCheckBox1.Visible and (Ord (Key) > 31) then
  65.   begin
  66.     Key := #0;
  67.     Table1.Edit;
  68.     DBCheckBox1.Checked := not
  69.       DBCheckBox1.Checked;
  70.     DBCheckBox1.Field.AsBoolean :=
  71.       DBCheckBox1.Checked;
  72.   end;
  73. end;
  74. end.