转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 软件开发 >> Delphi程序 >> 正文
偶写的第一个控件,一个用选择代替输入的Edit控件         ★★★★

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

作者:闵涛 文章来源:闵涛的学习笔记 点击数:1619 更新时间:2009/4/23 18:26:32
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.

上一页  [1] [2] 


[Delphi程序]可以左右居中对齐并可设置DisplayFormat的Edit控件  [Delphi程序]Sender 的應用:所有Edit共用一個過濾格式
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · C语言系列  · VB.NET程序
    · JAVA开发  · Delphi程序
    · 脚本语言
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台