我想你还是直接继承自TDBLookupComboBox,然后override 它的KeyPress方法(Delphi): procedure TYourDBLookupComboBox.KeyPress(var Key: Char); begin inherited KeyPress(Key); {Do your jobs} {if FListVisible then if Key in [#13, #27] then CloseUp(Key = #13) else FDataList.KeyPress(Key) else ProcessSearchKey(Key);}end;
To Borlandor: 从TDBLookupComboBox继承,下拉框可以输入吗?可以允许光标进入吗?
DBLookupComBox当输入大于Space的字符时,会自动查找匹配项。
它的KeyPress方法(Delphi):
procedure TYourDBLookupComboBox.KeyPress(var Key: Char);
begin
inherited KeyPress(Key);
{Do your jobs} {if FListVisible then
if Key in [#13, #27] then
CloseUp(Key = #13)
else
FDataList.KeyPress(Key)
else
ProcessSearchKey(Key);}end;
从TDBLookupComboBox继承,下拉框可以输入吗?可以允许光标进入吗?
不过复杂度可能比把一个TComboBox改造为TDBLookupComboBox小一些。
1.从TCombOBox进行改造达到目标
2.从TDBLookupControl改造达到目标(这种方法也有一定的困难,这种方法需要自已将控件的外观画出来。
3.另外增加窗口
至于你重载TDBLookupComboBox的KeyPress,理由何在呢?
基本一样的代码,仅仅修改KeyPress实现方法和增加Editor
你可以使用TInplaceEdit和TDBLookupComboBox来组合实现。
仔细调整Editor在组件中的位置、大小和边框,最好是NoneFrame的。不好意思,现在比较忙。
大部分人会想到,从数据库里查找数据插入到dbcombox里面,这样做的代价太大了,速度让人无法接收,我们以前的一个系统就是这样做的。用户到我那里投诉,说点击下拉框后象死机一样,2分钟都不会有反映,那时用户的数据才1000条。
告诉你们俺的必杀技,那就是用两个控件,下面放dblookupcombox,上面放dbcombox。只要稍稍的重载几句代码,就已经达到了以假乱真的效果,速度没有任何的影响。
基本一样的代码,仅仅修改KeyPress实现方法和增加Editor,
不过因为VCL源码是Pascal的,使用Delphi改写吧:unit MyDBLookupComboBox;interfaceuses Windows,Messages,Classes,SysUtils,Controls,StdCtrls, Buttons,DBCtrls;type
TMyDBLookupComboBox = class(TDBLookupControl)
private
FEditor: TEdit; {Only different from TDBLookupComboBox}
FDataList: TPopupDataList;
FButtonWidth: Integer;
FText: string;
FDropDownRows: Integer;
FDropDownWidth: Integer;
FDropDownAlign: TDropDownAlign;
FListVisible: Boolean;
FPressed: Boolean;
FTracking: Boolean;
FAlignment: TAlignment;
FLookupMode: Boolean;
FOnDropDown: TNotifyEvent;
FOnCloseUp: TNotifyEvent;
procedure ListMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer); {Only different from TDBLookupComboBox}
procedure EditorKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure EditorKeyPress(Sender: TObject; var Key: Char);
procedure EditorKeyUp(Sender: TObject; var Key: Word;
Shift: TShiftState);
{Ended} procedure StopTracking;
procedure TrackButton(X, Y: Integer);
procedure CMDialogKey(var Message: TCMDialogKey); message CM_DIALOGKEY;
procedure CMCancelMode(var Message: TCMCancelMode); message CM_CANCELMODE;
procedure CMCtl3DChanged(var Message: TMessage); message CM_CTL3DCHANGED;
procedure CMFontChanged(var Message: TMessage); message CM_FONTCHANGED;
procedure CMGetDataLink(var Message: TMessage); message CM_GETDATALINK;
procedure WMCancelMode(var Message: TMessage); message WM_CANCELMODE;
procedure WMKillFocus(var Message: TWMKillFocus); message WM_KILLFOCUS;
procedure CMBiDiModeChanged(var Message: TMessage); message CM_BIDIMODECHANGED;
protected
procedure CreateParams(var Params: TCreateParams); override;
procedure Paint; override;
procedure KeyDown(var Key: Word; Shift: TShiftState); override;
procedure KeyPress(var Key: Char); override;
procedure KeyValueChanged; override;
procedure MouseDown(Button: TMouseButton; Shift: TShiftState;
X, Y: Integer); override;
procedure MouseMove(Shift: TShiftState; X, Y: Integer); override;
procedure MouseUp(Button: TMouseButton; Shift: TShiftState;
X, Y: Integer); override;
procedure UpdateListFields; override;
public
constructor Create(AOwner: TComponent); override;
procedure CloseUp(Accept: Boolean); virtual;
procedure DropDown; virtual;
function ExecuteAction(Action: TBasicAction): Boolean; override;
procedure SetBounds(ALeft, ATop, AWidth, AHeight: Integer); override;
function UpdateAction(Action: TBasicAction): Boolean; override;
function UseRightToLeftAlignment: Boolean; override;
property KeyValue;
property ListVisible: Boolean read FListVisible;
property Text: string read FText;
published
property Anchors;
property BevelEdges;
property BevelInner;
property BevelOuter;
property BevelKind;
property BevelWidth;
property BiDiMode;
property Color;
property Constraints;
property Ctl3D;
property DataField;
property DataSource;
property DragCursor;
property DragKind;
property DragMode;
property DropDownAlign: TDropDownAlign read FDropDownAlign write FDropDownAlign default daLeft;
property DropDownRows: Integer read FDropDownRows write FDropDownRows default 7;
property DropDownWidth: Integer read FDropDownWidth write FDropDownWidth default 0;
property Enabled;
property Font;
property ImeMode;
property ImeName;
property KeyField;
property ListField;
property ListFieldIndex;
property ListSource;
property NullValueKey;
property ParentBiDiMode;
property ParentColor;
property ParentCtl3D;
property ParentFont;
property ParentShowHint;
property PopupMenu;
property ReadOnly;
property ShowHint;
property TabOrder;
property TabStop;
property Visible;
property OnClick;
property OnCloseUp: TNotifyEvent read FOnCloseUp write FOnCloseUp;
property OnContextPopup;
property OnDragDrop;
property OnDragOver;
property OnDropDown: TNotifyEvent read FOnDropDown write FOnDropDown;
property OnEndDock;
property OnEndDrag;
property OnEnter;
property OnExit;
property OnKeyDown;
property OnKeyPress;
property OnKeyUp;
property OnMouseDown;
property OnMouseMove;
property OnMouseUp;
property OnStartDock;
property OnStartDrag;
end;implementation{ TMyDBLookupComboBox }constructor TMyDBLookupComboBox.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
ControlStyle := ControlStyle + [csReplicatable];
Width := 145;
Height := 0;
FDataList := TPopupDataList.Create(Self);
FDataList.Visible := False;
FDataList.Parent := Self;
FDataList.OnMouseUp := ListMouseUp;
FButtonWidth := GetSystemMetrics(SM_CXVSCROLL);
FDropDownRows := 7; {Add followings}
FEditor := TEdit.Create(AOwner);
FEditor.Width := Width;
FEditor.Height := Height;
FEditor.Parent := Self;
FEditor.Visible := Visible;
FEditor.OnKeyDown := EditorKeyDown;
FEditor.OnKeyUp := EditorKeyUp;
FEditor.OnKeyPress := EditorKeyPress;
end;procedure TDBLookupComboBox.EditorKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
{Do your jobs}
end;procedure TDBLookupComboBox.EditorKeyPress(Sender: TObject; var Key: Char);
begin
{Do your jobs}
end;procedure TDBLookupComboBox.EditorKeyUp(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
{Do your jobs}
end;procedure TDBLookupComboBox.KeyPress(var Key: Char);
begin
inherited KeyPress(Key);
if FListVisible then
if Key in [#13, #27] then
CloseUp(Key = #13)
else
FDataList.KeyPress(Key)
else
begin
SendMessage(Editor.Handle,WM_CHAR,Key,0);{Try this, send key to Editor}
ProcessSearchKey(Key);
end;
end;
{Other methods are mostly identical with TDBLookupComboBox}...end.