⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 findclass.~pa

📁 一个很好用的万能查询控件。部分代码经过更改。
💻 ~PA
字号:
{*******************************************************}
{       查询控件     Unit                               }
{                                                       }
{       Copyright (c) 2001-2001, 广东亿康公司           }
{       实现Form中通用查询功能                          }
{       Athor:乔有钢                                    }
{*******************************************************}
unit FindClass;

interface

uses
  Windows, Messages, SysUtils, DBGridEh, Classes, DBGrids,Forms,
  FindSelect, Graphics, Controls, Dialogs,EditHint,
  AdoDB, ExtCtrls, FindMultiSelect, FindForm;

type
  //全局记录
  RecFind = record
    FForm: TForm;
    FAdoQuery: TAdoQuery;
    sCount,
      sGridName,
      sOldCaption,
      sSQLText,
      sSQLTextAnd,
      sSQLTextOr: string;
  end;
  PRec = ^RecFind;


  //方法指针
  TNotifyEvent = procedure(Sender: Tobject) of object;

  //窗体的标题和提示
  //对象型属性
{  TFindHint = class(TPersistent)
  private
    FormCaption: string;
    FHint: string;
  public
    //property FindHint: string read FHint write FHint;
  published
    property FindHint: string read FHint write FHint;
    property FindFormCaption: string read FormCaption write FormCaption;
  end;  }


  TImeName = type string;

  //集合属性
  TGridEnum = (fsDBGrid, fsDBGridEh);
  TFSet = set of TGridEnum;

  //查询控件TFind
  TFind = class(TComponent)
  private
    { Private declarations }
    FParentFont: Boolean;
    FormCaption: string;
    FHint: string;
    FTimer: TTimer;
    FAfterFindEvent: TNotifyEvent; //事件
 
    procedure FTimerTimer(Sender: TObject);
    procedure LoadStrList;
    procedure Loadproperty;
    procedure OpenFindSelectForm(slStrList: TStringList);
    procedure OpenFindForm(slStrList: TStringList);
    function IsHintStrored: Boolean;
  protected
    { Protected declarations }
    bShowHint:Boolean;
    procedure DoAfterFind;
  public
    FForm: TForm;
    sGridName, sTableName: string;
    FAdoQuery: TAdoQuery;
    FFindSet: TFSet;
    constructor Create(AOwner: TComponent); override;
    destructor Destroy;
    //多条件查询
    procedure MultiFindLoad;
    //任意组合条件查询
    procedure CommonFindLoad;
    //单一条件查询
    procedure UniqueFindLoad;
    //在public部分申明的在Object Inspector 上看不到它,只能通过另外的编辑器来编辑它

  published
    property FormName: TForm read FForm write FForm;
    property GridName: string read sGridName write sGridName;
    property TableName: string read sTableName write sTableName;
    property AdoQuery: TAdoQuery read FAdoQuery write FAdoQuery;
    property FindSet: TFSet read FFindSet write FFindSet;
    property ShowFindHint: Boolean read bShowHint write bShowHint default True;
    property ParentFont: Boolean read FParentFont write FParentFont default True;
    property AfterFind: TNotifyEvent read FAfterFindEvent write FAfterFindEvent;
    property FindHint: string read FHint write FHint stored IsHintStrored;
    property FindFormCaption: string read FormCaption write FormCaption;


  end;
//全局变量
var Rec: PRec;
  FDBGridEh: TDBGridEh;
  FDBGrid: TDBGrid;
  gFForm: TForm;
  gFAdoQuery: TAdoQuery;
  gsGridName,
    gsTableName,
    gsCount,
    gsSQLText,
    gsFindFormCaption,
    gsFindHint,
    gsOldCaption: string;
  gbIsDBGrid, gbIsDBGridEh,gbIsParentFont,gbShowFindHint: Boolean;
procedure Register;

implementation
var slStrList: TstringList;

procedure Register;
begin
  RegisterComponents('Standard', [TFind]);
end;

{ TFind }

procedure TFind.CommonFindLoad;
begin
  sTableName := gsTableName;
  FAdoQuery := gFAdoQuery;
  LoadStrList;
  OpenFindForm(slStrList);
  if gsSQLText <> '' then
    with FAdoQuery do begin
      Close;
      Sql.Clear;
      Sql.Text := 'select * from ' + gsTableName + ' where ' + gsSQLText;
      Open;
    end;
  gsCount := IntToStr(FAdoQuery.RecordCount);
  DoAfterFind; //触发查询完毕事件
end;

constructor TFind.Create(AOwner: TComponent);
begin
  inherited Create(AOwner); //先创建TFind的实例
  if not (csDesigning in ComponentState) then //如果在设计期就禁止FTimer
  begin
    FTimer := TTimer.Create(self);
    FTimer.Enabled := False;
    FTimer.Interval := 5000;
    FTimer.OnTimer := FTimerTimer;
  end;

  {New(Rec);
  Rec^.FForm:=FormName;
  Rec^.sOldCaption:=FForm.Caption;//记下原窗体的标题
  Rec^.FAdoQuery:=AdoQuery;
  Rec^.sGridName:=GridName; }
  slStrList := TStringList.Create;
 // FAdoQuery:=TAdoQuery.Create(nil);
end;

destructor TFind.Destroy;
begin
  slStrList.Free;
  inherited Destroy; //删除TFind的实例
end;

procedure TFind.DoAfterFind;
begin
  FTimer.Enabled := True;
  if gsCount <> '0' then
    FForm.Caption := gsOldCaption + ' 查询完毕, 共有 ' + gsCount + '条数据';
  //如果建立了处理AfterFind事件的句柄就调用这句柄
  if Assigned(AfterFind) then
    FAfterFindEvent(Self); //触发客户写的脚本
end;

procedure TFind.FTimerTimer(Sender: TObject);
begin
  FTimer.Enabled := False;
  FForm.Caption := gsOldCaption; //五秒钟后还原窗口的标题
end;


function TFind.IsHintStrored: Boolean;
begin
  Result := (FindHint='') ;
end;

procedure TFind.Loadproperty;
var sCaption: string;
  i: integer;
begin
  gFForm := FForm;
  sCaption := Trim(gFForm.Caption);
  i := Pos(' ', sCaption);
  if i > 0 then
    sCaption := Copy(sCaption, 1, i);
  gsOldCaption := sCaption; //记下原窗体的标题
  gFAdoQuery := FAdoQuery;
  gsGridName := sGridName;
  gsTableName := sTableName;
  if FFindSet = [fsDBGrid] then
    gbIsDBGrid := True;
  if FFindSet = [fsDBGridEh] then
    gbIsDBGridEh := True;
  gbIsParentFont:=ParentFont;
  gsFindFormCaption:=FindFormCaption;
  gsFindHint:=FindHint;
  gbShowFindHint:=ShowFindHint;
end;

procedure TFind.LoadStrList;
var i, j: integer;
begin
  if gbIsDBGridEh then
  with gFForm do begin
    for j := 0 to ComponentCount - 1 do
      if Components[j] is TDBGridEh then begin
        if (Components[j] as TDBGridEh).Name = gsGridName then begin
          FDBGridEh := TDBGridEh(Components[j]);
          for i := 0 to FDBGridEh.Columns.Count - 1 do
          begin
            slStrList.Add((Components[j] as TDBGridEh).Columns[i].Title.Caption);
          end;
        end;
      end;
  end;
  if gbIsDBGrid then
  with gFForm do begin
    for j := 0 to ComponentCount - 1 do
      if Components[j] is TDBGrid then begin
        if (Components[j] as TDBGrid).Name = gsGridName then begin
          FDBGrid := TDBGrid(Components[j]);
          for i := 0 to FDBGrid.Columns.Count - 1 do
          begin
            slStrList.Add((Components[j] as TDBGrid).Columns[i].Title.Caption);
          end;
        end;
      end;
  end;
end;

procedure TFind.MultiFindLoad;
begin
  Loadproperty;
  slStrList := TStringList.Create;
  FAdoQuery := gFAdoQuery;
  LoadStrList;
  OpenFindSelectForm(slStrList);
  if gsSQLText <> '' then
    with FAdoQuery do begin
      Close;
      Sql.Clear;
      Sql.Text := 'select * from ' + gsTableName + ' where ' + gsSQLText;
      Open;
    end;
  gsCount := IntToStr(FAdoQuery.RecordCount);
  DoAfterFind;
end;

procedure TFind.OpenFindForm(slStrList: TStringList);
begin
  if frmFind = nil then
    frmFind := TfrmFind.Create(nil);
  frmFind.slList := slStrList;
  frmFind.ShowModal;
end;

procedure TFind.OpenFindSelectForm(slStrList: TStringList);
begin
  if frmMultiFindSelect = nil then
    frmMultiFindSelect := TfrmMultiFindSelect.Create(nil);
  frmMultiFindSelect.slList := slStrList;
  frmMultiFindSelect.ShowModal;
end;


procedure TFind.UniqueFindLoad;
begin
  Loadproperty;
  FindLoad(gsTableName);
  DoAfterFind;
end;

end.

 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -