分享
 
 
 

偶写的第一个控件,一个用选择代替输入的Edit控件

王朝delphi·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

{***************************************************************}

{ }

{ Siow写的第一个控件 }

{ }

{用途:主要用于数据录入界面 }

{特点:用选择代替输入,减少人工录入时的低级错误 }

{版本:V1.1 }

{已知Bugs:1、在设计期如果数据源Active就无法编译 }

{ 2、ConnectionString编缉问题。加上ADOReg,DesignIntf后,}

{ 控件可安装却有好多引用单元无法编译,郁闷-_-! }

{联系方式:E-Mail:fuyushui@sohu.com }

{ QQ:1253366 }

{ }

{ }

{***************************************************************}

unit DBLookUpEdit;

interface

uses

Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

Dialogs, Grids, DBGrids, StdCtrls, DB, ADODB;

//,ADOReg,DesignIntf,DesignEditors

type

{TDBLookUpEdit}

TDBLookUpEdit = class(TEdit)

private

FCreating: Boolean;

FKeyField: WideString;

FDBGrid : TDBGrid;

FADOQuery: TADOQuery;

FDataSource: TDataSource;

FOnEnter: TNotifyEvent;

FOnExit: TNotifyEvent;

FOnChange: TNotifyEvent;

//FOnClick: TNotiFyEvent;

//FOnDblClick:TNotifyEvent;

procedure CNCommand(var Message: TWMCommand);

message CN_COMMAND;

function GetActive: Boolean;

procedure SetActive(Value: Boolean);

function GetDataSource: TDataSource;

procedure SetDataSource(Value: TDataSource);

function GetConnectionString: WideString;

procedure SetConnectionString(const Value: WideString);

function GetConnection: TADOConnection;

procedure SetConnection(const Value: TADOConnection);

function GetSQL: TStrings;

procedure SetSQL(const Value: TStrings);

procedure SetRecText(FieldNo: integer);

procedure DoFDBGridMouseUp(Sender: TObject; Button: TMouseButton;Shift: TShiftState; X, Y: Integer);

procedure DoFDBGridKeyDown(Sender: TObject; var Key: Word;Shift: TShiftState);

protected

procedure SetParent(AParent: TWinControl); override;

procedure Notification(AComponent: TComponent; Operation: TOperation); override;

procedure CMVisiblechanged(var Message: TMessage);

message CM_VISIBLECHANGED;

procedure CMEnabledchanged(var Message: TMessage);

message CM_ENABLEDCHANGED;

procedure CMBidimodechanged(var Message: TMessage);

message CM_BIDIMODECHANGED;

procedure FDoEnter(Sender: TObject);

procedure FDoExit(Sender: TObject);

procedure KeyDown(var Key: Word; Shift: TShiftState); override;

procedure KeyPress(var Key: Char); override;

procedure KeyUp(var Key: Word; Shift: TShiftState); override;

procedure Loaded; override;

procedure CreateWnd; override;

public

constructor Create(AOwner: TComponent); override;

procedure SetBounds(ALeft: Integer; ATop: Integer; AWidth: Integer; AHeight: Integer); override;

published

//procedure Click;override;

property KeyFieldName:WideString read FKeyField write FKeyField;

procedure DblClick; override;

property OnEnter: TNotifyEvent read FOnEnter write FOnEnter;

property OnExit: TNotifyEvent read FOnExit write FOnExit;

property OnChange: TNotifyEvent read FOnChange write FOnChange;

//property OnClick: TNotifyEvent read FOnClick write FOnClick;

//property OnDblClick: TNotifyEvent read FOnDblClick write FOnDblClick;

//property DataSource: TDataSource read GetDataSource write SetDataSource;

property Active: Boolean read GetActive write SetActive default False;

property ConnectionString: WideString read GetConnectionString write SetConnectionString;

property Connection: TADOConnection read GetConnection write SetConnection;

property SQL: TStrings read GetSQL write SetSQL;

end;

procedure Register;

implementation

{ TDBLookUpEdit }

procedure Register;

begin

RegisterComponents('LD Controls', [TDBLookUpEdit]);

//RegisterPropertyEditor(TypeInfo(WideString), TDBLookUpEdit, 'ConnectionString', TConnectionStringProperty);

end;

constructor TDBLookUpEdit.Create(AOwner: TComponent);

begin

inherited;

FDBGrid :=TDBGrid.Create(Self);

FADOQuery :=TADOQuery.Create(self);

FDataSource :=TDataSource.Create(self);

FDBGrid.FreeNotification(self);

FADOQuery.FreeNotification(self);

FDataSource.FreeNotification(self);

FDataSource.DataSet:=FADOQuery;

with FDBGrid do

begin

DataSource:=FDataSource;

Ctl3D:=false;

Visible:=false;

ParentCtl3D:=false;

Options:=[dgColLines,dgRowLines,dgRowSelect,dgAlwaysShowSelection,dgConfirmDelete,dgCancelOnExit];

OnMouseUp:=DoFDBGridMouseUp;

OnKeyDown:=DoFDBGridKeyDown;

end;

with self do

begin

ParentCtl3D:=false;

Ctl3D:=false;

end;

end;

procedure TDBLookUpEdit.CreateWnd;

begin

FCreating := True;

try

inherited CreateWnd;

finally

FCreating := False;

end;

end;

procedure TDBLookUpEdit.CMBidimodechanged(var Message: TMessage);

begin

inherited;

FDBGrid.BiDiMode := BiDiMode;

end;

procedure TDBLookUpEdit.CMEnabledchanged(var Message: TMessage);

begin

inherited;

FDBGrid.Enabled := Enabled;

end;

procedure TDBLookUpEdit.CMVisiblechanged(var Message: TMessage);

begin

inherited;

end;

procedure TDBLookUpEdit.Notification(AComponent: TComponent;

Operation: TOperation);

begin

inherited Notification(AComponent, Operation);

if (AComponent = FDBGrid) and (Operation = opRemove) then FDBGrid:= nil;

if (AComponent = FADOQuery) and (Operation = opRemove) then FADOQuery:= nil;

if (AComponent = FDataSource) and (Operation = opRemove) then FDataSource:= nil;

end;

procedure TDBLookUpEdit.SetParent(AParent: TWinControl);

begin

inherited SetParent(AParent);

if FDBGrid <> nil then FDBGrid.Parent := self.Owner as TForm;

end;

procedure TDBLookUpEdit.SetBounds(ALeft, ATop, AWidth, AHeight: Integer);

begin

inherited;

if FDBGrid <> nil then

with FDBGrid do

begin

Top:=-Height;

Left:=-Width;

end;

end;

procedure TDBLookUpEdit.SetRecText(FieldNo: integer);

begin

self.SetFocus;

self.SelectAll;

if (FADOQuery.Connection <>nil) or (FADOQuery.ConnectionString <>'') then

if FADOQuery.Active then

if FADOQuery.RecordCount >0 then

if FADOQuery.FieldCount>FieldNo then

begin

self.Text:=FDBGrid.Fields[FieldNo].Text;

self.SelectAll;

self.SetFocus;

end;

end;

procedure TDBLookUpEdit.FDoEnter(Sender: TObject);

var

p :TPoint;

begin

P:=self.ClientToParent(point(0,self.Height),(self.Owner as TForm));

if (FDBGrid.Height+p.y+2)<=(self.Owner as TForm).Height then

begin

FDBGrid.Top :=p.y+2;

end

else begin

FDBGrid.Top :=p.y-2-self.Height -FDBGrid.Height;

end;

FDBGrid.Left :=p.x+2;

FDBGrid.BringToFront;

FDBGrid.Visible:=true;

if self.Text='' then SetRecText(1);

self.SelectAll;

if (self.Text<>'') and FADOQuery.Active then

FADOQuery.Locate(FKeyField, self.text,[lopartialkey]);

end;

procedure TDBLookUpEdit.FDoExit(Sender: TObject);

begin

if not FDBGrid.Focused then FDBGrid.Visible:=false;

end;

procedure TDBLookUpEdit.DoFDBGridMouseUp(Sender: TObject;

Button: TMouseButton; Shift: TShiftState; X, Y: Integer);

begin

SetRecText(1);

FDBGrid.Visible:=false;

end;

procedure TDBLookUpEdit.DoFDBGridKeyDown(Sender: TObject; var Key: Word;

Shift: TShiftState);

begin

if key=13 then

begin

SetRecText(1);

FDBGrid.Visible:=false;

key:=0;

end;

end;

procedure TDBLookUpEdit.CNCommand(var Message: TWMCommand);

begin

case Message.NotifyCode of

EN_CHANGE:

begin

if not FCreating then

if Assigned(FOnChange) then FOnChange(self);

end;

EN_KILLFOCUS:

begin

if Assigned(FOnExit) then FOnExit(self);

FDoExit(self);

end;

EN_SETFOCUS:

begin

if Assigned(FOnEnter) then FOnEnter(self);

FDoEnter(self);

end;

end;

end;

procedure TDBLookUpEdit.DblClick;

begin

inherited;

FDoEnter(self);

end;

function TDBLookUpEdit.GetDataSource: TDataSource;

begin

Result := FDBGrid.DataSource;

end;

procedure TDBLookUpEdit.SetDataSource(Value: TDataSource);

begin

if Value <> FDBGrid.Datasource then FDBGrid.DataSource := Value;

if Value <> nil then Value.FreeNotification(Self);

end;

procedure TDBLookUpEdit.KeyDown(var Key: Word; Shift: TShiftState);

begin

inherited;

if FDBGrid.Visible then

begin

if (key=38) or (key=40) then

begin

SendMessage(FDBGrid.Handle,WM_KEYDOWN,key,0);

key:=0;

end;

if key=13 then

begin

SetRecText(1);

FDBGrid.Visible:=false;

key:=0;

end;

end;

end;

//判断是否全是数字

function IsAllInteger(Text:widestring):boolean;

var

Temp:string;

i:integer;

begin

try

Result:=true;

Temp:=trim(text);

if (length(Temp)<=0) then

begin

Result:=false;

exit;

end;

for i:=1 to length(Temp) do

begin

if not (Temp[i] in ['0'..'9']) then

begin

Result:=false;

break;

end;

end;

except

Result:=false;

end;

end;

//生成筛选语句

function CSQL(EditText,FieldName:WideString):WideString;

var

i:integer;

sql:WideString;

tmEditText1,tmEditText2:WideString;

begin

Result:='';

if IsAllInteger(EditText) then

begin

tmEditText1:=trim(EditText);

tmEditText2:=trim(EditText);

SQL:=SQL+'('+FieldName+'>='+trim(EditText)+' and '+FieldName+'<='+inttostr((StrToInt(EditText) div 10)*10+9)+')';

for i:=length(EditText) to 6 do

begin

tmEditText1:=tmEditText1+'0';

tmEditText2:=tmEditText2+'9';

sql:=sql+' or ('+FieldName+'>='+tmEditText1+' and '+FieldName+'<='+tmEditText2+')';

end;

Result:=sql;

end;

end;

procedure TDBLookUpEdit.KeyUp(var Key: Word; Shift: TShiftState);

begin

inherited;

if FDBGrid.Visible then

begin

if (key=38) or (key=40) then

begin

SetRecText(1);

end

else if IsAllInteger(self.Text) then

begin

FADOQuery.Filtered:=false;

FADOQuery.Filter:=CSQL(self.Text,FKeyField);

FADOQuery.Filtered:=true;

end;

end;

end;

procedure TDBLookUpEdit.KeyPress(var Key: Char);

begin

inherited;

end;

function TDBLookUpEdit.GetConnection: TADOConnection;

begin

Result := FADOQuery.Connection;

end;

procedure TDBLookUpEdit.SetConnection(const Value: TADOConnection);

begin

if Value <> FADOQuery.Connection then

begin

FADOQuery.Connection := Value;

end;

if Value <> nil then Value.FreeNotification(Self);

end;

function TDBLookUpEdit.GetConnectionString: WideString;

begin

Result := FADOQuery.ConnectionString;

end;

procedure TDBLookUpEdit.SetConnectionString(const Value: WideString);

begin

if Value <> FADOQuery.ConnectionString then FADOQuery.ConnectionString := Value;

end;

function TDBLookUpEdit.GetActive: Boolean;

begin

Result :=FADOQuery.Active;

end;

procedure TDBLookUpEdit.SetActive(Value: Boolean);

begin

if Value <> FADOQuery.Active then

begin

FADOQuery.Active := Value;

end;

end;

function TDBLookUpEdit.GetSQL: TStrings;

begin

Result := FADOQuery.SQL;

end;

procedure TDBLookUpEdit.SetSQL(const Value: TStrings);

begin

if FADOQuery.SQL<>Value then FADOQuery.SQL.Assign(Value);

end;

procedure TDBLookUpEdit.Loaded;

begin

inherited Loaded;

end;

end.

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有