有人发帖说你写数据库程序的时候,有用面向对象的思想嘛?

每个实体数据我们都需要给用户提供查询界面,于是我们有很多的

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'

这样算是一个进步吧,嘿嘿

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值