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

📄 findmultiselect.pas

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

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ComCtrls, StdCtrls, ExtCtrls;

type
  TfrmMultiFindSelect = class(TForm)
    pn1: TPanel;
    lbDate: TLabel;
    dtpBegin: TDateTimePicker;
    dtpEnd: TDateTimePicker;
    pn2: TPanel;
    Label1: TLabel;
    Edit1: TEdit;
    Label2: TLabel;
    Edit2: TEdit;
    Label3: TLabel;
    Edit3: TEdit;
    Bevel1: TBevel;
    pn3: TPanel;
    btnOK: TButton;
    btnCancel: TButton;
    procedure FormShow(Sender: TObject);
    procedure btnCancelClick(Sender: TObject);
    procedure FormKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure dtpEndKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure btnOKClick(Sender: TObject);
  private
    { Private declarations }
  public
    slList: TStringList; { Public declarations }
    procedure load;
    function GetFildName(sTitle: string): string;
    procedure Find; virtual; //可以在子类中重载
  end;

var
  frmMultiFindSelect: TfrmMultiFindSelect;

implementation

uses FindClass;
var bIsTime: Boolean;
  sSqlText: string;
{$R *.DFM}

procedure TfrmMultiFindSelect.FormShow(Sender: TObject);
begin
  load;
  // bIsOk := True;
  ShortDateFormat := 'YYYY-MM-DD';
  dtpBegin.DateTime := Date - 30;
  dtpend.DateTime := Date;
//  bIsTime := (Pos('时间', cmbIndex.Text) > 0) or (Pos('日期', cmbIndex.Text) > 0);
  pn1.Visible := bIsTime;
  if pn1.Visible = False then begin
    pn1.Height := 3;
    Edit1.SetFocus;
  end;
  frmMultiFindSelect.Height := pn1.Height + pn2.Height + pn3.Height + 25;
  Update;
  gsSQLText := '';
  frmMultiFindSelect.Caption := gsFindFormCaption;

end;

procedure TfrmMultiFindSelect.btnCancelClick(Sender: TObject);
begin
  gsSqlText := ''; //取消
  Close;
end;

procedure TfrmMultiFindSelect.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if (Key = VK_RETURN) or (Key = VK_DOWN) then //按回车键或向下键自动跳到下一输入控件
    SendMessage(Handle, WM_NEXTDLGCTL, 0, 0);
  if Key = VK_UP then //按向上键自动跳到上一输入控件
    SendMessage(Handle, WM_NEXTDLGCTL, 1, 0);
  if Key = VK_ESCAPE then
    Close;
end;

procedure TfrmMultiFindSelect.load;
var
  i: integer;
begin
  Edit1.Text := '';
  Edit2.Text := '';
  Edit3.Text := '';
  if slList.Count > 0 then begin
  if gbShowDateTimePicker then begin
    if (Pos('时间', slList[0]) > 0) or (Pos('日期', slList[0]) > 0) then begin
      pn1.Visible := True;
      bIsTime := True;
      lbDate.Caption := slList[0];
      label1.Caption := slList[1];
      label2.Caption := slList[2];
      label3.Caption := slList[3];
      Exit;
    end;
    if (Pos('时间', slList[1]) > 0) or (Pos('日期', slList[1]) > 0) then begin
      pn1.Visible := True;
      bIsTime := True;
      lbDate.Caption := slList[1];
      label1.Caption := slList[0];
      label2.Caption := slList[2];
      label3.Caption := slList[3];
      Exit;
    end;
    if (Pos('时间', slList[2]) > 0) or (Pos('日期', slList[2]) > 0) then begin
      pn1.Visible := True;
      bIsTime := True;
      lbDate.Caption := slList[2];
      label1.Caption := slList[0];
      label2.Caption := slList[1];
      label3.Caption := slList[3];
      Exit;
    end;
    if (Pos('时间', slList[3]) > 0) or (Pos('日期', slList[3]) > 0) then begin
      pn1.Visible := True;
      bIsTime := True;
      lbDate.Caption := slList[3];
      label1.Caption := slList[0];
      label2.Caption := slList[1];
      label3.Caption := slList[2];
      Exit;
    end;
    end;
    label1.Caption := slList[0];
    label2.Caption := slList[1];
    label3.Caption := slList[2];
  end;
   //改变窗体的字体和默-认输入法
  with frmMultiFindSelect do begin
    for I := 0 to ComponentCount - 1 do
      if Components[I] is TWinControl then
      begin
        if Components[I] is TEdit then
          (Components[I] as TEdit).ImeName := '';
        if Components[I] is TListBox then
          (Components[I] as TListBox).ImeName := '';
        if Components[I] is TDateTimePicker then
          (Components[I] as TDateTimePicker).ImeName := '';
      end;
       if gbIsParentFont then
    for I := 0 to ComponentCount - 1 do
      if Components[I] is TWinControl then
      begin
        if Components[I] is TEdit then
          (Components[I] as TEdit).Font := gFForm.Font;
        if Components[I] is TLabel then
          (Components[I] as TLabel).Font := gFForm.Font;
        if Components[I] is TPanel then
          (Components[I] as TPanel).Font := gFForm.Font;
        if Components[I] is TButton then
          (Components[I] as TButton).Font := gFForm.Font;
      end;
    if gbShowFindHint then begin
      for I := 0 to ComponentCount - 1 do
        if Components[I] is TWinControl then
        begin
          if Components[I] is TEdit then
            (Components[I] as TEdit).ShowHint := True;
          if Components[I] is TPanel then
            (Components[I] as TPanel).ShowHint := True;
          if Components[I] is TEdit then
            (Components[I] as TEdit).Hint := gsFindHint;
          if Components[I] is TPanel then
            (Components[I] as TPanel).Hint := gsFindHint;
        end;
    end;
  end;
  frmMultiFindSelect.Update;
end;

procedure TfrmMultiFindSelect.dtpEndKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if key = VK_Return then begin
    Edit1.SetFocus;
  end;
end;

function TfrmMultiFindSelect.GetFildName(sTitle: string): string;
var
  i: integer;
  sIndex: string;
begin
  with gFForm do begin //取得列头相对应的字段名
    if FDBGridEh <> nil then
      for i := 0 to FDBGridEh.Columns.Count - 1 do begin
        if FDBGridEh.Columns[i].Title.Caption = sTitle then begin
          sIndex := FDBGridEh.Columns[i].FieldName;
          Result := sIndex;
          Exit;
        end;
      end;
    if FDBGrid <> nil then
      for i := 0 to FDBGrid.Columns.Count - 1 do begin
        if FDBGrid.Columns[i].Title.Caption = sTitle then begin
          sIndex := FDBGrid.Columns[i].FieldName;
          Result := sIndex;
          Exit;
        end;
      end;
  end;

end;

procedure TfrmMultiFindSelect.btnOKClick(Sender: TObject);
begin
  Find;
end;

procedure TfrmMultiFindSelect.Find;
begin
  sSqlText := '';
  if Edit1.Text <> '' then
    sSqlText := 'convert(char(50),' + GetFildName(Trim(label1.Caption)) + ')' + ' like ''' + Trim(Edit1.Text) + '' + '%''  ';
  if Edit2.Text <> '' then
    if sSqlText <> '' then
      sSqlText := sSqlText + ' and  convert(char(50),' + GetFildName(Trim(label2.Caption)) + ')' + ' like ''' + Trim(Edit2.Text) + '' + '%''  '
    else
      sSqlText := 'convert(char(50),' + GetFildName(Trim(label2.Caption)) + ')' + ' like ''' + Trim(Edit2.Text) + '' + '%''  ';
  if Edit3.Text <> '' then
    if sSqlText <> '' then
      sSqlText := sSqlText + ' and  convert(char(50),' + GetFildName(Trim(label3.Caption)) + ')' + ' like ''' + Trim(Edit3.Text) + '' + '%''  '
    else
      sSqlText := 'convert(char(50),' + GetFildName(Trim(label3.Caption)) + ')' + ' like ''' + Trim(Edit3.Text) + '' + '%''  ';
  if pn1.Visible then
    if sSqlText <> '' then
      sSqlText := sSqlText + ' and  convert(char(10),' + GetFildName(Trim(lbDate.Caption)) +
        ',121)>=' + QuotedStr(FormatDateTime('YYYY-MM-DD', dtpBegin.Date)) + ' and  convert(char(10),' +
        GetFildName(Trim(lbDate.Caption)) + ',121)<=' + QuotedStr(FormatDateTime('YYYY-MM-DD', dtpEnd.Date))
    else
      sSqlText := 'convert(char(10),' + GetFildName(Trim(lbDate.Caption)) +
        ',121)>=' + QuotedStr(FormatDateTime('YYYY-MM-DD', dtpBegin.Date)) + ' and  convert(char(10),' +
        GetFildName(Trim(lbDate.Caption)) + ',121)<=' + QuotedStr(FormatDateTime('YYYY-MM-DD', dtpEnd.Date));
  if sSqlText = '' then //条件为空时查询所有的
    sSqlText := 'convert(char(50),' + GetFildName(Trim(label1.Caption)) + ')' + ' like ''' + Trim(Edit1.Text) + '' + '%''  ';
  gsSQLText := sSQLText; ;
  Close;
end;

end.

⌨️ 快捷键说明

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