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

📄 unit1.pas

📁 《Delphi7编程100例》代码,书配资料
💻 PAS
字号:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, DB, Grids, DBGrids, DBTables, ExtCtrls, DBCtrls;

type
  TForm1 = class(TForm)
    Table1: TTable;
    DBGrid1: TDBGrid;
    DataSource1: TDataSource;
    Edit1: TEdit;
    Button1: TButton;
    GroupBox1: TGroupBox;
    RadioButton1: TRadioButton;
    RadioButton2: TRadioButton;
    DBNavigator1: TDBNavigator;
    procedure Button1Click(Sender: TObject);
    procedure RadioButton1Click(Sender: TObject);
    procedure RadioButton2Click(Sender: TObject);

  private
    { Private declarations }
    function GrabMemoFieldAsPChar(TheField : TMemoField): PChar;
    function DoFindIn(TheField : TField; SFor : String): Boolean;
    function FindIt(TheTable : TDataSet; TheFields : array of integer;
                   SearchBackward : Boolean; FromBeginning : Boolean; SFor : String): Boolean;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function TForm1.GrabMemoFieldAsPChar(TheField:TMemoField): PChar;
begin
  with TBlobStream.Create(TheField, bmRead) do
    begin
      GetMem(Result, Size + 1);
      FillChar(Result^, Size + 1, #0);
      Read(Result^, Size);
      Free;
    end;
end;

function TForm1.DoFindIn(TheField : TField; SFor : String): Boolean;
var
  PChForMemo : PChar;
begin
  Result := False;
  case TheField.DataType of
    ftString :
               begin
                 if (Pos(SFor, UpperCase(TheField.AsString)) > 0) then
                   Result := True;
               end;
    ftInteger :
                begin
                  if (Pos(SFor, TheField.AsString) > 0) then
                    Result := True;
                end;
    ftBoolean :
                begin
                  if SFor = UpperCase(TheField.AsString) then
                    Result := True;
                end;
    ftFloat :
              begin
                if (Pos(SFor, TheField.AsString) > 0) then
                  Result := True;
                end;
    ftCurrency :
                 begin
                   if (Pos(SFor, TheField.AsString) > 0) then
                     Result := True;
                   end;
    ftDate .. ftDateTime :
                           begin
                             if (Pos(SFor, TheField.AsString) > 0) then
                               Result := True;
                           end;
    ftMemo :
                 begin
                   SFor[Ord(SFor[1]) + 1] := #0;
                   PChForMemo := GrabMemoFieldAsPChar(TMemoField(TheField));
                   StrUpper(PChForMemo);
                   if not (StrPos( PChForMemo, @SFor[1] ) = nil) then
                     Result :=True;
                   FreeMem(PChForMemo, StrLen(PChForMemo + 1));
                 end;
  end;
end;

function TForm1.FindIt(TheTable : TDataSet; TheFields : array of integer;SearchBackward : Boolean; FromBeginning : Boolean; SFor : String): Boolean;//the third
var
  i, HighTheFields, LowTheFields : integer;
  BM : TBookmark;
begin
  result:=false;
  TheTable.DisableControls;
  BM := TheTable.GetBookmark;
  try
    LowTheFields := Low(TheFields);
    HighTheFields := High(TheFields);
    SFor := UpperCase(SFor);
    Result := False;
    if FromBeginning then TheTable.First;
    if SearchBackward then
      begin
        TheTable.Prior;
        while not TheTable.BOF do
          begin
            for i := LowTheFields to HighTheFields do
              begin
                if DoFindIn(TheTable.Fields[TheFields[i]], SFor) then
                  begin
                    Result := True;
                    Break;
                  end;
              end;
            if Result then
              Break
              else TheTable.Prior;
           end;
      end
      else
        begin
          TheTable.Next;
          while not TheTable.EOF do
            begin
              for i := LowTheFields to HighTheFields do
                begin
                  if DoFindIn(TheTable.Fields[TheFields[i]], SFor) then
                    begin
                      Result := True;
                      Break;
                    end;
                end;
              if Result then
              Break
              else TheTable.Next;
            end;
        end;
        finally
        TheTable.EnableControls;
        if not Result then
          TheTable.GotoBookmark(BM);
    TheTable.FreeBookmark(BM);
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  if not (RadioButton1.Checked) and not (RadioButton2.Checked) then
    showmessage('请选择查找方向!');
  if RadioButton1.Checked then
    if FindIt(table1,[table1.FieldByName('XM').Index],True, False, edit1.Text) then
      showmessage('已经找到要您查找的记录!')
    else
      showmessage('没有找到相关记录!');
  if RadioButton2.Checked then
    if FindIt(table1,[table1.FieldByName('XM').Index],False, False, edit1.Text) then
      showmessage('已经找到要您查找的记录!')
    else
      showmessage('没有找到相关记录!');
end;

procedure TForm1.RadioButton1Click(Sender: TObject);
begin
  RadioButton1.Checked:=True;
  RadioButton2.Checked:=False;
end;

procedure TForm1.RadioButton2Click(Sender: TObject);
begin
  RadioButton2.Checked:=True;
  RadioButton1.Checked:=False;
end;

end.

⌨️ 快捷键说明

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