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

📄 ukingfilter.pas

📁 中式财务栏 表格式录入 运行时设置可显示列、列名、列宽
💻 PAS
字号:
{*******************************************************}
{                                                       }
{       记录筛选                                        }
{                                                       }
{       版权所有 (C) 2007 咏南工作室(陈新光)          }
{                                                       }
{*******************************************************}

unit uKingFilter;

interface

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

type
  TColParams = record 
    FieldName: string;
    Title: string;
  end;
  
  TFormSetColor = class(TForm)
    lbl1: TLabel;
    lbl2: TLabel;
    lbl3: TLabel;
    cbb1: TComboBox;
    cbb2: TComboBox;
    edt1: TComboBox;
    cbb3: TComboBox;
    lst1: TMemo;
    grp1: TGroupBox;
    btn1: TButton;
    btn2: TButton;
    btn3: TButton;
    btn4: TButton;
    procedure btn4Click(Sender: TObject);
    procedure btn3Click(Sender: TObject);
    procedure btn2Click(Sender: TObject);
    procedure btn1Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormDestroy(Sender: TObject);
    procedure cbb1Change(Sender: TObject);
  private
    FGrid: TDBGridEh;
    ColArray: array of TColParams;
    procedure FillFieldCombx;
    { 确定 }
    procedure Ok;
    { 增加查询条件 }
    procedure Add;
    { 清空条件 }
    procedure Clear;
    function ConvertStr(AStr: string): string;
    procedure Values;
    function GetFieldName:string;
  public
    { Public declarations }
  end;

var
  FormSetColor: TFormSetColor;

procedure ShowFilterForm(AGrid:TDBGridEh);  

implementation

{$R *.dfm}

procedure ShowFilterForm(AGrid:TDBGridEh); 
begin
  if (not Assigned(AGrid)) or (not AGrid.DataSource.DataSet.Active)
    or (AGrid.DataSource.DataSet.IsEmpty) then exit;
  FormSetColor:=TFormSetColor.Create(nil);
  try
    FormSetColor.FGrid:=AGrid;
    FormSetColor.FGrid.DataSource.DataSet.Filtered:=False;
    FormSetColor.FillFieldCombx;
    FormSetColor.ShowModal;
  finally
    FreeAndNil(FormSetColor);
  end;
end;

procedure TFormSetColor.FillFieldCombx;
var
  I: Integer;
begin
  SetLength(ColArray,FGrid.Columns.Count);
  for i:=0 to FGrid.Columns.Count-1 do
  begin
    if FGrid.Columns[i].Visible then
    begin
      ColArray[i].FieldName:=FGrid.Columns[i].FieldName;
      ColArray[i].Title:=FGrid.Columns[i].Title.Caption;
      cbb1.Items.Add(ColArray[i].Title);
    end;
  end;
  cbb1.ItemIndex:=0;
end;

procedure TFormSetColor.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action:=caFree;
end;

procedure TFormSetColor.FormDestroy(Sender: TObject);
begin
  FormSetColor:=nil;
end;

function TFormSetColor.GetFieldName: string;
var
  i:Integer;
begin
  for i:=Low(colarray) to High(colarray) do
    if ColArray[i].Title=cbb1.Text then
      Result:=ColArray[i].fieldname;
end;

procedure TFormSetColor.add;
var
  s:string;
begin
  if (Trim(cbb1.Text)='') or (Trim(cbb2.Text)='') then exit;
  if (cbb2.Text<>'包含') and (edt1.Text='') then exit;
  if (cbb2.Text<>'不包含') and (edt1.Text='') then exit;
  s:=GetFieldName+' '+ConvertStr(cbb2.Text);
  if lst1.lines.Count<=0 then
  begin
    if (cbb2.Text<>'包含') and (cbb2.Text<>'不包含') then
      lst1.lines.Add(s+' '+quotedstr(edt1.Text))
    else lst1.lines.Add(s+' '+quotedstr('%'+edt1.Text+'%'));
  end else
  begin
    if cbb3.Text='' then exit;
      if (cbb2.Text<>'包含') and (cbb2.Text<>'不包含') then
        lst1.lines.Add(ConvertStr(cbb3.Text)+' '+s+' '+quotedstr(edt1.Text))
      else lst1.lines.Add(ConvertStr(cbb3.Text)+' '+s+' '+
        quotedstr('%'+edt1.Text+'%'));
  end;
end;

procedure TFormSetColor.Ok;
begin
  if lst1.lines.Count=0 then
    if Trim(edt1.Text)='' then Exit else Add;
  FGrid.DataSource.DataSet.Filtered:=True;
  FGrid.DataSource.DataSet.Filter:=lst1.lines.Text;
  Self.Close;
end;

//只取前十条记录的
procedure TFormSetColor.Values;
var
  i:Integer;
begin
  if (Assigned(FGrid))
    and (FGrid.DataSource.DataSet.Active)
    and (not FGrid.DataSource.DataSet.IsEmpty) then
  begin
    edt1.Clear;
    FGrid.DataSource.DataSet.DisableControls;
    FGrid.DataSource.DataSet.First;
    i:=1;
    while i<=10 do
    begin
      edt1.Items.Add(FGrid.DataSource.DataSet.FieldByName(GetFieldName).AsString);
      FGrid.DataSource.DataSet.Next;
      Inc(i);
    end;
    FGrid.DataSource.DataSet.EnableControls;
    if edt1.Items.Count>=0 then
      edt1.ItemIndex:=0;
  end;  
end;       

procedure TFormSetColor.Clear;
begin
  if lst1.lines.Count=0 then exit;
  if Application.MessageBox('确认清除', '提示', MB_YESNOCANCEL + 
    MB_ICONQUESTION) = IDYES then
    lst1.lines.Clear;
end;

function TFormSetColor.ConvertStr(AStr: string): string;
begin
  if AStr='包含' then Result:='like';
  if AStr='等于' then Result:='=';
  if AStr='不等于' then Result:='<>';
  if AStr='大于' then Result:='>';
  if AStr='小于' then Result:='<';
  if AStr='大于或等于' then Result:='>=';
  if AStr='小于或等于' then Result:='<=';
  if AStr='并且' then Result:='and';
  if AStr='或者' then Result:='or';
  if AStr='不包含' then Result:='not like';
end;

procedure TFormSetColor.btn4Click(Sender: TObject);
begin
  Close;
end;

procedure TFormSetColor.cbb1Change(Sender: TObject);
begin
  Values;
end;

procedure TFormSetColor.btn3Click(Sender: TObject);
begin
  ok;
end;

procedure TFormSetColor.btn2Click(Sender: TObject);
begin
  Clear;
end;

procedure TFormSetColor.btn1Click(Sender: TObject);
begin
  add;
end;

end.

⌨️ 快捷键说明

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