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

时间:2022-11-19 09:04:32
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 > 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(,self.Height),(self.Owner as TForm));
if (FDBGrid.Height p.y )<=(self.Owner as TForm).Height then
begin
FDBGrid.Top :=p.y ;
end
else begin
FDBGrid.Top :=p.y--self.Height -FDBGrid.Height;
end;
FDBGrid.Left :=p.x ;
FDBGrid.BringToFront;
FDBGrid.Visible:=true;
if self.Text='''''''' then SetRecText();
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();
FDBGrid.Visible:=false;
end;
procedure TDBLookUpEdit.DoFDBGridKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if key= then
begin
SetRecText();
FDBGrid.Visible:=false;
key:=;
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=) or (key=) then
begin
SendMessage(FDBGrid.Handle,WM_KEYDOWN,key,);
key:=;
end;
if key= then
begin
SetRecText();
FDBGrid.Visible:=false;