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

📄 findpopular.~pas

📁 一个很好用的万能查询控件。部分代码经过更改。
💻 ~PAS
字号:
unit FindPopular;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, StdCtrls, CheckLst, Buttons;

type
  TfrmFindPopular = class(TForm)
    GroupBox3: TGroupBox;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    cmbIndex: TComboBox;
    edtValue: TEdit;
    dtpTime: TDateTimePicker;
    lbYSF: TCheckListBox;
    Button2: TButton;
    Button1: TButton;
    btnFind: TButton;
    SpeedButton1: TSpeedButton;
    GroupBox1: TGroupBox;
    lbAnd: TCheckListBox;
    cb: TCheckBox;
    SpeedButton2: TSpeedButton;
    procedure cmbIndexChange(Sender: TObject);
    procedure FormKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure FormShow(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure btnFindClick(Sender: TObject);
    procedure lbYSFKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure dtpTimeKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure FormCreate(Sender: TObject);
    procedure edtValueKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure lbYSFMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure SpeedButton1Click(Sender: TObject);
    procedure SpeedButton2Click(Sender: TObject);
  private
  procedure GetYSF;
  protected
    procedure load; virtual; //可以在子类中重载
    function GetFildName(sTitle: string): string; virtual;
  public
    slList: TStringList; { Public declarations }
  end;

var
  frmFindPopular: TfrmFindPopular;

implementation
uses FindClass;
var
  bIsTime: Boolean;
  sSqlText,sResult: string;
  slSqlList: TStringList;
{$R *.dfm}

procedure TfrmFindPopular.cmbIndexChange(Sender: TObject);
begin
  if gbShowDateTimePicker then
  bIsTime := (Pos('时间', cmbIndex.Text) > 0) or (Pos('日期', cmbIndex.Text) > 0);
  dtpTime.Visible := bIsTime;
end;

procedure TfrmFindPopular.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 TfrmFindPopular.FormShow(Sender: TObject);
  var i:integer;
begin
  load;
  if cmbIndex.Items.Count > 0 then
    cmbIndex.ItemIndex := 0;
  gsSQLText := '';
  sSQLText := '';
  ShortDateFormat := 'YYYY-MM-DD';
  dtpTime.DateTime := Date;
  if gbShowDateTimePicker then
  bIsTime := (Pos('时间', cmbIndex.Text) > 0) or (Pos('日期', cmbIndex.Text) > 0);
  dtpTime.Visible := bIsTime;
  frmFindPopular.Caption := gsFindFormCaption;
  for i:=0 to lbYSF.items.Count-1 do
  lbYSF.Checked[i]:=False;
  lbYSF.Checked[1]:=True;
end;

function TfrmFindPopular.GetFildName(sTitle: string): string;
var
  i, j: 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 TfrmFindPopular.load;
var
  i: integer;
begin
  //加载查询字段
  cmbIndex.Items.Clear;
  if slList.Count > 0 then
    for i := 0 to slList.Count - 1 do
      cmbIndex.Items.Add(slList[i]);
  //改变窗体的字体和默-认输入法
  with frmFindPopular 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   //假如查询控件的ParentFont属性设为True
    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 TListBox then
          (Components[I] as TListBox).Font := gFForm.Font;
        if Components[I] is TRadioButton then
          (Components[I] as TRadioButton).Font := gFForm.Font;
        if Components[I] is TGroupBox then
          (Components[I] as TGroupBox).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 TGroupBox then
            (Components[I] as TGroupBox).ShowHint := True;
          if Components[I] is TEdit then
            (Components[I] as TEdit).Hint := gsFindHint;
          if Components[I] is TGroupBox then
            (Components[I] as TGroupBox).Hint := gsFindHint;
        end;
    end;
    Update;
  end;

end;

procedure TfrmFindPopular.Button1Click(Sender: TObject);
begin
  lbAnd.Items.Clear;
  sSqlText := '';
  slSqlList.Clear;
end;

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

procedure TfrmFindPopular.btnFindClick(Sender: TObject);
var i:integer;
begin
  for i:=0 to  lbAnd.Items.Count-1 do
  if lbAnd.Checked[i] then
  sSqlText:=sSqlText+slSqlList[i]+' And ';
  sSqlText:=Copy(sSqlText,1,Length(sSqlText)-4);
  gsSQLText := sSqlText;
  if cb.Checked=True then
  lbAnd.Clear;
  Close;
end;

procedure TfrmFindPopular.lbYSFKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if key = VK_RETURN then
    if bIsTime then
      dtpTime.SetFocus
    else
      edtValue.SetFocus;
  if Key = VK_UP then //按向上键自动跳到上一输入控件
    cmbIndex.SetFocus;
end;

procedure TfrmFindPopular.dtpTimeKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin                       
  if Key=VK_RETURN then begin
   GetYSF;
  if bIsTime then  begin
        lbAnd.Items.Add(Trim(cmbIndex.Text)+sResult+ FormatDateTime('YYYY-MM-DD',dtpTime.Date));
        lbAnd.Checked[lbAnd.Items.Count-1]:=True;
        slSqlList.Add('convert(char(10),' + GetFildName(Trim(cmbIndex.Text)) + ',121)' + sResult + QuotedStr(FormatDateTime('YYYY-MM-DD',dtpTime.Date)));
        end else begin
        lbAnd.Items.Add(Trim(cmbIndex.Text)+sResult+ Trim(edtValue.Text));
        lbAnd.Checked[lbAnd.Items.Count-1]:=True;
        slSqlList.Add( 'convert(char(50),' + GetFildName(Trim(cmbIndex.Text)) + ')' + sResult + QuotedStr(Trim(edtValue.Text)));
    end ;
  end;
 //
end;

procedure TfrmFindPopular.FormCreate(Sender: TObject);
begin
   slSqlList:=TStringList.Create();
end;

procedure TfrmFindPopular.GetYSF;
begin
  if lbYSF.Checked[0]=True then
    sResult := '>';
  if lbYSF.Checked[1]=True then
    sResult := '=';
  if lbYSF.Checked[2]=True then
      sResult := '<';
end;

procedure TfrmFindPopular.edtValueKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if Key=VK_RETURN then begin
  GetYSF;
  if bIsTime then  begin
        lbAnd.Items.Add(Trim(cmbIndex.Text)+sResult+ Trim(edtValue.Text));
        slSqlList.Add('convert(char(10),' + GetFildName(Trim(cmbIndex.Text)) + ',121)' + sResult + QuotedStr(Trim(edtValue.Text)));
        end else begin
        lbAnd.Items.Add(Trim(cmbIndex.Text)+sResult+ Trim(edtValue.Text));
        slSqlList.Add( 'convert(char(50),' + GetFildName(Trim(cmbIndex.Text)) + ')' + sResult + QuotedStr(Trim(edtValue.Text)));
    end ;
    btnFind.SetFocus;
  end;
end;

procedure TfrmFindPopular.lbYSFMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  var i:integer;
begin
   for i:=0 to lbYSF.items.Count-1 do
   lbYSF.Checked[i]:=False;
end;

procedure TfrmFindPopular.SpeedButton1Click(Sender: TObject);
begin
  if dtpTime.Visible=True then begin
   GetYSF;
  if bIsTime then  begin
        lbAnd.Items.Add(Trim(cmbIndex.Text)+sResult+ FormatDateTime('YYYY-MM-DD',dtpTime.Date));
        slSqlList.Add('convert(char(10),' + GetFildName(Trim(cmbIndex.Text)) + ',121)' + sResult + QuotedStr(FormatDateTime('YYYY-MM-DD',dtpTime.Date)));
        end else begin
        lbAnd.Items.Add(Trim(cmbIndex.Text)+sResult+ Trim(edtValue.Text));
        slSqlList.Add( 'convert(char(50),' + GetFildName(Trim(cmbIndex.Text)) + ')' + sResult + QuotedStr(Trim(edtValue.Text)));
    end ;
  end else begin
    GetYSF;
  if bIsTime then  begin
        lbAnd.Items.Add(Trim(cmbIndex.Text)+sResult+ Trim(edtValue.Text));
        slSqlList.Add('convert(char(10),' + GetFildName(Trim(cmbIndex.Text)) + ',121)' + sResult + QuotedStr(Trim(edtValue.Text)));
        end else begin
        lbAnd.Items.Add(Trim(cmbIndex.Text)+sResult+ Trim(edtValue.Text));
        slSqlList.Add( 'convert(char(50),' + GetFildName(Trim(cmbIndex.Text)) + ')' + sResult + QuotedStr(Trim(edtValue.Text)));
    end ;    
  end;
    btnFind.SetFocus;
end;

procedure TfrmFindPopular.SpeedButton2Click(Sender: TObject);
begin
  lbAnd.DeleteSelected;
end;

end.

⌨️ 快捷键说明

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