Советы по Delphi

Хитрый TEdit


Приведу письмо читателя:

...я тоже пишу в Delphi и могу поделиться своим опытом. В дополнение к этому письму, чтобы не быть голословным, прилагаю маленький компонент THintEdit, порожденный от TCustomEdit, который представляет собой с виду обычный TEdit элемент с возможностью автоматического выбора стринговых значений из скрытого списка (так, как это реализовано в Netscape Navigator'е). Описание особенно не нужно, так как выполнено все достаточно элементарно: значения для выбора заносятся в свойство HintList, тип свойства TStrings. При нажатии клавиш вверх/вниз выбираются значения, соответствующие набранным начальным символам.

    unit HintEdit;

interface

uses

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

StdCtrls;
type
THintEdit = class(TCustomEdit) private { Private declarations } FHintList:TStrings; Searching, CanSearch:boolean; CurSPos:integer; protected { Protected declarations } procedure Change; override; procedure KeyDown(var Key: Word; Shift: TShiftState); override; public { Public declarations } constructor Create(AOwner: TComponent); override; property HintList:TStrings read FHintList write FHintList; destructor Destroy; override; published { Published declarations } property Anchors; property AutoSelect; property AutoSize; property BiDiMode; property BorderStyle; property CharCase; property Color; property Constraints; property Ctl3D; property DragCursor; property DragKind; property DragMode; property Enabled; property Font; property HideSelection; property ImeMode; property ImeName; property MaxLength; property OEMConvert; property ParentBiDiMode; property ParentColor; property ParentCtl3D; property ParentFont; property ParentShowHint; property PasswordChar; property PopupMenu; property ReadOnly; property ShowHint; property TabOrder; property TabStop; property Text; property Visible; property OnChange; property OnClick; property OnDblClick; property OnDragDrop; property OnDragOver; 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;
procedure Register;

implementation

{$R *.DCR}

procedure Register;
begin
RegisterComponents('Netscape', [THintEdit]); end;

constructor THintEdit.Create;
begin
inherited
; FHintList:=TStringList.Create; Searching:=false; CanSearch:=true; CurSPos:=-1; end;

procedure THintEdit.Change;
var
i,l:integer; begin
if
Searching then Exit; if not CanSearch then Exit; if Text='' then exit; l:=Length(Text); for i:=0 to FHintList.Count-1 do if Copy(FHintList[i],1,l)=Text then begin Searching:=true; CurSPos:=i; Text:=FHintList[i]; Searching:=false; SelStart:=Length(Text); SelLength:=-(Length(Text)-l); break; end; inherited; end;

procedure THintEdit.KeyDown;
var
l:integer; begin
if
Chr(Key) in ['A'..'z','А'..'Я','а'..'я'] then CanSearch:=true else CanSearch:=false; case Key of VK_DOWN:begin if (CurSPos<HintList.Count-1) and (SelLength>0) then if Copy(FHintList[CurSPos+1],1,SelStart)=Copy(Text,1,SelStart) then begin l:=SelStart; Inc(CurSPos); Text:=FHintList[CurSPos]; SelStart:=Length(Text); SelLength:=-(Length(Text)-l); end; Key:=VK_RETURN; end; VK_UP:begin if (CurSPos>0) and (SelLength>0) then if Copy(FHintList[CurSPos-1],1,SelStart)=Copy(Text,1,SelStart) then begin l:=SelStart; Dec(CurSPos); Text:=FHintList[CurSPos]; SelStart:=Length(Text); SelLength:=-(Length(Text)-l); end; Key:=VK_RETURN; end; VK_RETURN:begin SelStart:=0; SelLength:=Length(Text); end; end; inherited; end;

destructor THintEdit.Destroy;
begin
FHintList.Free; inherited; end;

Константин Хрипков <kostya@softincom.ru>. [000515]



Содержание раздела