To balaschen
非常感谢你给我的例子,我做了以下改动:
1定义了三个GridNumber,GridChinese,GridEnglish三个属性用作对不同输入方式的查询选择,属性值为数据库的字段名
2定义了GridWidth为下拉数据表的宽度
3定义了DataSource为数据源
但还有一些问题请教!
1下拉DBGrid不能用鼠标或键盘选择?
2如果想过滤数据应该怎么做?即每次按键后重新查询而不用Locate!!
以下是我改后的程序!!!请帮忙解答,分数不够可以再加!!!
unit JackCombobox;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls,Grids,DBGrids,DB,DBCtrls;
type
TJackCombobox = class;
TMyComboBox=class(TDBGrid)
private
FEdit:TJackCombobox;
protected
procedure CreateParams(var params:TCreateParams);override;
procedure WMSetFocus(var Msg:TWMSetFocus);message WM_SETFOCUS;
public
constructor Create(AOwner:TComponent);override;
end;
TJackCombobox = class(TEdit)
private
FDataLink: TFieldDataLink;
FMyComboBox:TMyComboBox;
FDowned:Boolean;
FFocused:Boolean;
FGridWidth:integer;
FDataSource:TDataSource;
FGridQuery:TDataSet;
FGridNumber:string;
FGridChinese:string;
FGridEnglish:string;
procedure ShowCombobox;
procedure HideComboBox;
function GetDataSource: TDataSource;
procedure SetDataSource(Value: TDataSource);
protected
procedure WMSIZE(var Msg:TWMSize);message WM_SIZE;
procedure CMCancelMode(var Msg:TCMCancelMode);message CM_CANCELMODE;
public
Constructor Create(AOwner:TComponent);override;
destructor Destroy;override;
procedure Change;override;
procedure DoEnter;override;
procedure DoExit;override;
published
property GridWidth:integer read FGridWidth write FGridWidth default 120;
property DataSource:TDataSource read GetDataSource write SetDataSource;
property GridNumber:string read FGridNumber write FGridNumber;
property GridChinese:string read FGridChinese write FGridChinese;
property GridEnglish:string read FGridEnglish write FGridEnglish;
{ Published declarations }
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('JackTools', [TJackCombobox]);
end;
{ TJackCombobox }
procedure TJackCombobox.CMCancelMode(var Msg: TCMCancelMode);
begin
inherited;
if FDowned then
begin
HideComboBox;
end;
end;
constructor TJackCombobox.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FDataLink := TFieldDataLink.Create;
FDataLink.Control := Self;
FGridWidth:=Width;
FMyComboBox:=TMyComboBox.Create(self);
FDowned:=False;
FFocused:=False;
end;
destructor TJackCombobox.Destroy;
begin
FMyComboBox.Free;
FMyComboBox:=nil;
inherited Destroy;
end;
procedure TJackCombobox.HideComboBox;
begin
SetWindowPos(FMyComboBox.Handle,HWND_TOP,0,0,0,0,SWP_NOACTIVATE + SWP_HideWindow + SWP_NOSIZE +SWP_NOZORDER);
FDowned:=not FDowned;
end;
procedure TJackCombobox.ShowCombobox;
var
Point:TPoint;
begin
Point.x:=Left;
Point.y:=top+Height;
Point:=Parent.ClienttoScreen(Point);
FMyCombobox.Width:=GridWidth;
SetWindowPos(FMyComboBox.Handle,HWND_TOP,Point.x,Point.y,0,0,SWP_NOACTIVATE + SWP_HideWindow + SWP_NOSIZE +SWP_NOZORDER);
SetWindowPos(FMyComboBox.Handle,HWND_TOP,Point.x,Point.y,0,0,SWP_NOACTIVATE + SWP_ShowWindow + SWP_NOSIZE +SWP_NOZORDER +SWP_NOMOVE);
FDowned:=not FDowned;
end;
function TJackCombobox.GetDataSource: TDataSource;
begin
Result := FDataLink.DataSource;
end;
procedure TJackCombobox.SetDataSource(Value: TDataSource);
begin
if not (FDataLink.DataSourceFixed and (csLoading in ComponentState)) then
FDataLink.DataSource := Value;
if Value <> nil then Value.FreeNotification(Self);
FMyCombobox.DataSource:=FDataLink.DataSource;
end;
procedure TJackCombobox.WMSIZE(var Msg:TWMSize);
begin
FMyCombobox.Width:=GridWidth;
end;
procedure TJackCombobox.Change;
var FindName:string;
begin
if not(csDesigning in ComponentState) and FFocused then
begin
fgridquery:=FDataLink.DataSource.DataSet;
if Text<>'' then
begin
if Text[1] in ['0'..'9'] then
FindName:=FGridNumber
else if (Text[1] in ['A'..'Z']) or (Text[1] in ['a'..'z']) then
FindName:=FGridEnglish
else
FindName:=FGridChinese;
end;
fGridQuery.Locate(FindName,Text,[loPartialKey]);
if not FDowned then
ShowComboBox;
end;
inherited;
end;
procedure TJackCombobox.DoEnter;
begin
FFocused:=True;
inherited;
end;
procedure TJackCombobox.DoExit;
begin
FFocused:=False;
if FDowned then
HideComboBox;
inherited;
end;
{ TMyComboBox }
constructor TMyComboBox.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FEdit:=TJackCombobox(AOwner);
ControlStyle:=ControlStyle + [csNoDesignVisible, csReplicatable,csAcceptsControls];
Width:=FEdit.GridWidth;
Height:=130;
Options:=[dgColumnResize,dgColLines,dgRowLines,dgTabs,dgRowSelect,dgAlwaysShowSelection,dgConfirmDelete,dgCancelOnExit];
DataSource:=FEdit.FDataSource;
ReadOnly:=True;
ScrollBars:=ssVertical;
Visible := False;
Parent:=TWinControl(AOwner);
end;
procedure TMyComboBox.CreateParams(var params: TCreateParams);
begin
inherited CreateParams(params);
with Params do
begin
Style:=style or ws_popup;
EXStyle:=EXStyle or WS_EX_CLIENTEDGE or WS_EX_TOOLWINDOW;
end;
end;
procedure TMyComboBox.WMSetFocus(var Msg: TWMSetFocus);
begin
Inherited;
FEdit.SetFocus;
end;
end.