每个实体数据我们都需要给用户提供查询界面,于是我们有很多的
TfrmFind1,TfrmFind2,...TfrmFindx;
里面充斥着
if edt1.Text <> '' then result := Result + 'name =' + QuotedStr(edt1.text);
.......
if edtx.Text <> '' then result := Result + 'name =' + QuotedStr(edtx.text);
我写了这样一个父类,.dfm就不带了,很简单的
TfrmFind.Pas,其中的TServiceParams就是TStringList,声明在uServiceDefine.
unit uFrmFind;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, TypInfo, DB, Mask, uServiceDefine;
type
TfrmFind = class( TForm )
btnCancel: TButton;
grpConditions: TGroupBox;
btnOk: TButton;
procedure btnCancelClick( Sender: TObject );
procedure btnOkClick( Sender: TObject );
private
//get other TWinControl such as NumericEdit and DatetimeEdit 's Value
//all edits can use this method , but other explict type will be better performance;
function getValue( edit: TWinControl; const propertyName: string ): string; overload;
//explict type will be better performance;
// function getValue( edit: TCustomEdit ): string; overload;
// function getValue( edit: TCustomCheckBox ): string; overload;
// function getValue( edit: TCustomComboBox ): string; overload;
private
FConditionControls: TStringList; //存储将形成最后params的controls列表
protected
procedure addConditionControl( Control: TWinControl; const Name: string;
const PropertyNameOfValue: string = 'Text' );
public
function getFinalCondition: TServiceParams; virtual;
public
constructor Create; reintroduce;
destructor Destroy; override;
end;
var
frmFind: TfrmFind;
implementation
{$R *.dfm}
{ TfrmFind }
procedure TfrmFind.addConditionControl( Control: TWinControl;
const Name: string; const PropertyNameOfValue: string );
begin
if FConditionControls.IndexOfName( Name ) <> -1 then
raise Exception.Create( 'has "' + Name + '" ConditionControl,u must change another name.' );
FConditionControls.AddObject( Name + '=' + PropertyNameOfValue, Control );
end;
procedure TfrmFind.btnCancelClick( Sender: TObject );
begin
ModalResult := mrCancel;
end;
procedure TfrmFind.btnOkClick( Sender: TObject );
begin
ModalResult := mrOk;
end;
//function TfrmFind.getValue( edit: TCustomEdit ): string;
//begin
// result := edit.Text;
//end;
//
//function TfrmFind.getValue( edit: TCustomCheckBox ): string;
//begin
// if not TypInfo.IsPublishedProp( edit, 'Checked' ) then
// raise Exception.Create( 'no "Checked" propery.' );
// result := VarToStr( GetPropValue( edit, 'Checked' ) );
//end;
//
//function TfrmFind.getValue( edit: TCustomComboBox ): string;
//begin
// result := IntToStr( edit.ItemIndex );
//end;
constructor TfrmFind.Create;
begin
inherited Create( Owner );
FConditionControls := TStringlist.Create;
end;
destructor TfrmFind.Destroy;
begin
FConditionControls.Free;
inherited;
end;
function TfrmFind.getFinalCondition: TServiceParams;
var
i: Integer;
begin
result := TServiceParams.Create;
with FConditionControls do
for i := 0 to Count - 1 do
Result.Values[Names[i]] := getValue( Objects[i] as TWinControl, ValueFromIndex[i] );
end;
function TfrmFind.getValue( edit: TWinControl;
const propertyName: string ): string;
begin
if not TypInfo.IsPublishedProp( edit, propertyName ) then
raise Exception.Create( 'no "' + propertyName + '" propery.' );
result := VarToStr( GetPropValue( edit, propertyName ) );
end;
end.
写了一个子类,当然以后可以写n个了,子类的代码就很少了
TfrmFind_FirstReg
unit uFrmFind_FirstReg;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, uFrmFind, StdCtrls, Mask, RzEdit, uServiceDefine;
type
TFrmFind_FirstReg = class( TfrmFind )
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
edtName: TRzEdit;
edtCity: TRzEdit;
edtManageDepartment: TRzEdit;
edtHospital: TRzEdit;
public
procedure afterconstruction; override;
end;
var
FrmFind_FirstReg: TFrmFind_FirstReg;
implementation
{$R *.dfm}
{ TFrmFind_FirstReg }
procedure TFrmFind_FirstReg.afterconstruction;
begin
inherited;
addConditionControl( edtName, 'name' );
addConditionControl( edtCity, 'city' );
addConditionControl( edtManageDepartment, 'managedepartment' );
addConditionControl( edtHospital, 'hospital' );
end;
end.
这个form的getFinalCondition将返回,在界面中输入的值和对应项的列表
例如
name='adf'
city='afd'
hospital='adf'
这样算是一个进步吧,嘿嘿