📄 ucerca.pas
字号:
unit Ucerca;
interface
uses Windows, SysUtils, Classes, Graphics, Forms, Controls, StdCtrls, DBGridEh,
Buttons, ExtCtrls, dialogs, DBCtrls, Db, DBGrids, ActnList, ComCtrls, DbTables,
Grids;
type
TCerca_form = class(TForm)
BitBtn2: TBitBtn;
cerca: TEdit;
Label1: TLabel;
vcamp: TLabel;
BitBtn3: TBitBtn;
ActionList1: TActionList;
Continua: TAction;
StatusBar: TStatusBar;
Modoricerca: TRadioGroup;
Qricerca: TQuery;
dsRicerca: TDataSource;
continuaBtn: TBitBtn;
risposta: TPanel;
rispostagr: TDBGrid;
DBNavigator1: TDBNavigator;
GroupBox1: TGroupBox;
Label2: TLabel;
esempio: TLabel;
procedure FormShow(Sender: TObject);
procedure BitBtn3Click(Sender: TObject);
procedure BitBtn2Click(Sender: TObject);
procedure dsRicercaDataChange(Sender: TObject; Field: TField);
procedure ContinuaExecute(Sender: TObject);
procedure cercaChange(Sender: TObject);
procedure ModoricercaClick(Sender: TObject);
procedure ModoricercaExit(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
procedure trova(caller : Tform);
procedure Sincro;
procedure FaiUnEsempio;
end;
var
Cerca_form: TCerca_form;
campo, target : string;
obj : TDBedit;
GrObj : TDBgrid;
CBObj : TDBLookUpCombobox ;
GrEHOBj : TDBgridEH ;
Tab: TTable;
TApp : TDataSet;
componente : Tcomponent;
implementation
//uses Umenu;
{$R *.DFM}
procedure Tcerca_form.trova(caller : Tform);
Var
Scheda, Oggetto : String;
Indice : Integer;
CampoSel : Tfield;
begin
//Trova
risposta.Visible := false ;
continua.visible := False ;
update ;
target := 'X';
if (caller.ActiveControl is TDBedit) then
begin
target := 'Campo';
scheda := caller.Caption;
indice := caller.activeControl.ComponentIndex;
componente := caller.components[Indice];
Oggetto := componente.Name;
obj := TDBedit.Create(self);
obj := TDBedit(Componente);
Campo := obj.DataField;
self.Caption := 'Ricerca nella scheda ' + Scheda;
vcamp.Caption := Campo ;
show;
cerca.SetFocus;
end;
if caller.ActiveControl is TDBgrid then
begin
target := 'Griglia';
scheda := caller.Caption;
indice := caller.activeControl.ComponentIndex;
componente := caller.components[Indice];
Oggetto := componente.Name;
GrObj := TDBgrid.Create(self);
GrObj := TDBgrid(Componente);
campoSel := GrObj.SelectedField;
Campo := CampoSel.FieldName;
self.Caption := 'Ricerca nella scheda ' + Scheda;
vcamp.Caption := Campo ;
show;
cerca.SetFocus;
end;
if caller.ActiveControl is TDBgridEH then
begin
target := 'GrigliaEH';
scheda := caller.Caption;
indice := caller.activeControl.ComponentIndex;
componente := caller.components[Indice];
Oggetto := componente.Name;
GrEHObj := TDBgridEH.Create(self);
GrEHObj := TDBgridEH(Componente);
campoSel := GrEHObj.SelectedField;
Campo := CampoSel.FieldName;
self.Caption := 'Ricerca nella scheda ' + Scheda;
vcamp.Caption := Campo ;
show;
cerca.SetFocus;
end;
if caller.ActiveControl is TDBLookUpCombobox then
begin
target := 'LookupCombo';
scheda := caller.Caption;
indice := caller.activeControl.ComponentIndex;
componente := caller.components[Indice];
Oggetto := componente.Name;
CBObj := TDBLookupComboBox.Create(self);
CBObj := TDBLookupComboBox(Componente);
Campo := CBObj.DataField;
self.Caption := 'Ricerca nella scheda ' + Scheda;
vcamp.Caption := Campo ;
show;
cerca.SetFocus;
end;
end;
procedure TCerca_form.FormShow(Sender: TObject);
begin
cerca.SetFocus;
end;
procedure TCerca_form.BitBtn3Click(Sender: TObject);
Var
Valore,colonna,Tabella,Alias,p_ini,p_fin : String ;
trovato : Boolean ;
CampoOgg : Tfield ;
begin
if cerca_form.cerca.Text <> '' then
begin
StatusBar.Panels[0].Text := 'Ricerca in corso, attendere prego...' ;
StatusBar.Update ;
if target = 'Campo' then
Tapp := obj.DataSource.DataSet;
if target = 'Griglia' then
Tapp := GrObj.DataSource.DataSet;
if target = 'GrigliaEH' then
Tapp := GrEHObj.DataSource.DataSet;
if target = 'LookupCombo' then
Tapp := CBObj.DataSource.DataSet ;
valore := cerca.Text;
Try
case ModoRicerca.ItemIndex of
0 : begin
p_ini := '%' ;
p_fin := '%' ;
end ;
1 : begin
p_ini := '%' ;
p_fin := '' ;
end ;
2 : begin
p_ini := '' ;
p_fin := '' ;
end ;
end ;
Tab := tapp as TTable ;
Tabella := Tab.TableName ;
Alias := Tab.DatabaseName ;
colonna := campo ;
If pos(' ',campo) > 0 then
colonna := '"' + Colonna + '"' ;
Qricerca.Close ;
Qricerca.SQL.Clear ;
QRicerca.DatabaseName := Alias ;
QRicerca.Sql.Add('SELECT *') ;
Qricerca.Sql.Add('FROM ' + tabella + ' tabella') ;
Qricerca.Sql.Add('WHERE (Upper(tabella.'+colonna+') LIKE "'+p_ini+valore+p_fin+'")') ;
Qricerca.Sql.Add('ORDER BY tabella.' + colonna) ;
Qricerca.Open ;
Risposta.Visible := True ;
If Qricerca.isEmpty then
begin
StatusBar.Panels[0].text := 'Valore NON trovato...' ;
Qricerca.Close ;
end
else
StatusBar.Panels[0].Text := 'Valore trovato, selezionare il risultato voluto nella lista.' ;
except
Try
case ModoRicerca.ItemIndex of
0 : begin
trovato := false ;
Tapp.First ;
While (Not trovato) and not Tapp.Eof do
begin
CampoOgg := Tapp.FieldByName(campo) ;
if Pos(uppercase(Valore),UpperCase(Campoogg.AsString)) > 0 then
begin
Trovato := true ;
end
else
Tapp.Next ;
end ;
If Trovato then
begin
StatusBar.Panels[0].Text := 'Valore trovato...' ;
continuaBtn.Visible := True ;
end
else
StatusBar.Panels[0].Text := 'Valore non trovato !' ;
end ;
1 : begin
If Tapp.Locate(campo,Valore,[loPartialKey,loCaseInsensitive]) then
begin
StatusBar.Panels[0].Text := 'Valore trovato...' ;
continuaBtn.Visible := True ;
end
else
StatusBar.Panels[0].Text := 'Valore non trovato !' ;
end ;
2 : begin
If Tapp.Locate(campo,Valore,[loCaseInsensitive]) then
begin
StatusBar.Panels[0].Text := 'Valore trovato...' ;
continuaBtn.Visible := True ;
end
else
StatusBar.Panels[0].Text := 'Valore non trovato !' ;
end ;
end ;
except
ShowMessage('Ricerca non attuabile') ;
end ;
end ;
end ;
end;
procedure TCerca_form.BitBtn2Click(Sender: TObject);
begin
hide;
end;
procedure TCerca_form.dsRicercaDataChange(Sender: TObject; Field: TField);
begin
sincro ;
end;
procedure TCerca_form.Sincro;
Var
i : integer ;
campo : TField ;
begin
// cerchiamo per quale indice stiamo lavorando...
Tab.SetKey ;
for I := 0 to Tab.IndexFieldCount - 1 do
begin
campo := Tab.IndexFields[i] ;
Tab.FieldByName(campo.FieldName).Value := Qricerca.FieldByName(campo.FieldName).Value ;
end ;
tab.GotoKey ;
end;
procedure TCerca_form.ContinuaExecute(Sender: TObject);
Var
Trovato : boolean ;
CampoOgg : Tfield ;
begin
Tapp.Next ;
case ModoRicerca.ItemIndex of
0 : begin
trovato := false ;
CampoOgg := Tapp.FieldByName(campo) ;
While (Not trovato) and not Tapp.Eof do
begin
if Pos(uppercase(cerca.text),UpperCase(Campoogg.AsString)) > 0 then
begin
Trovato := true ;
end
else
Tapp.Next ;
end ;
If Trovato then
StatusBar.Panels[0].Text := 'Valore trovato...'
else
begin
StatusBar.Panels[0].Text := 'Valore non trovato !' ;
continuabtn.visible := False ;
end ;
end ;
1 : begin
trovato := false ;
CampoOgg := Tapp.FieldByName(campo) ;
While (Not trovato) and not Tapp.Eof do
begin
if Pos(uppercase(cerca.text),UpperCase(Campoogg.AsString)) = 1 then
begin
Trovato := true ;
end
else
Tapp.Next ;
end ;
If Trovato then
StatusBar.Panels[0].Text := 'Valore trovato...'
else
begin
StatusBar.Panels[0].Text := 'Valore non trovato !' ;
continuabtn.visible := false ;
end ;
end ;
2 : begin
trovato := false ;
CampoOgg := Tapp.FieldByName(campo) ;
While (Not trovato) and not Tapp.Eof do
begin
if Uppercase(cerca.text) = UpperCase(Campoogg.AsString) then
begin
Trovato := true ;
end
else
Tapp.Next ;
end ;
If Trovato then
StatusBar.Panels[0].Text := 'Valore trovato...'
else
begin
StatusBar.Panels[0].Text := 'Valore non trovato !' ;
continuabtn.visible := false ;
end ;
end ;
end ;
end;
procedure TCerca_form.cercaChange(Sender: TObject);
begin
FaiUnEsempio ;
end;
procedure TCerca_form.FaiUnEsempio;
begin
case modoRicerca.ItemIndex of
0 : esempio.Caption := '..'+cerca.text+'..' ;
1 : esempio.Caption := cerca.text + '..' ;
2 : esempio.caption := cerca.text ;
end ;
end;
procedure TCerca_form.ModoricercaClick(Sender: TObject);
begin
FaiUnEsempio ;
end;
procedure TCerca_form.ModoricercaExit(Sender: TObject);
begin
FaiunEsempio ;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -