Title: An Edit Control with AutoComplete Capabilities (Updated)
Question: Microsofts AutoComplete can be used in a Delphi Application in a Friendly way with the following component:
Answer:
//MWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWM/// Implements a TCustomEdit with AutoComplete Capabilities
// Author: Jorge Abel Ayala Marentes
// Created: 15/Oct/2000
// Last Modification: 21/Nov/2000
//MWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMW
unit U_AutoCompleteEdit;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, StrTools, ShlIntf, ActiveX, ComObj;
type
TSearchListChangeEvent = procedure of object;
TAutoCompleteEdit = class(TCustomEdit)
private
FSearchListChange: TSearchListChangeEvent;
FAutoComplete: IAutoComplete2;
FStrings: IUnknown;
FStringList: TStrings;
procedure SetFStringList(const Value: TStrings);
protected
procedure SearchListChange;
public
constructor Create(AOwner: TComponent);override;
//Needed to init AutoComplete when the component is first Loaded
procedure Loaded;override;
destructor Destroy;override;
procedure SetAutoComplete;
published
property AutoSelect;
property AutoSize;
property BorderStyle;
property CharCase;
property HideSelection;
property MaxLength;
property ParentColor;
property Text;
property OnChange;
property SearchList: TStrings read FStringList write SetFStringList;
property OnSearchListChange: TSearchListChangeEvent read FSearchListChange
write FSearchListChange;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Sitio Web', [TAutoCompleteEdit]);
end;//end of Register
//MWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMW
{ TAutoCompleteEdit }
constructor TAutoCompleteEdit.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
Parent := TWinControl(AOwner);
FStringList := TStringList.Create;
SearchListChange;
end;//end of TAutoCompleteEdit.Create
//MWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMW
destructor TAutoCompleteEdit.Destroy;
begin
FStringList.Free;
inherited;
end;//end of TAutoCompleteEdit.Destroy
//MWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMW
//Updated: Last version didtnt work because the searchlist wasnt
//initializaed when the component was loaded :)
procedure TAutoCompleteEdit.Loaded;
begin
inherited;
if FStringList.Count 0 then
SetAutoComplete;
end;//end of TAutoCompleteEdit.Loaded
//MWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMW
procedure TAutoCompleteEdit.SearchListChange;
begin
if Assigned(FSearchListChange) then
FSearchListChange;
end;//end of TAutoCompleteEdit.SearchListChange
//MWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMW
procedure TAutoCompleteEdit.SetAutoComplete;
begin
FAutoComplete := CreateComObject(CLSID_AutoComplete)as IAutoComplete2;
FStrings := TEnumString.Create(FStringList) as IUnknown;
OleCheck(FAutoComplete.SetOptions(ACO_AUTOSUGGEST
or ACO_AUTOAPPEND or ACO_UPDOWNKEYDROPSLIST or ACO_USETAB));
OleCheck(FAutoComplete.Init(Self.Handle, FStrings, nil, nil));
end;//end of TAutoCompleteEdit.SetAutoComplete
//MWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMW
procedure TAutoCompleteEdit.SetFStringList(const Value: TStrings);
begin
SearchList.Assign(Value);
SetAutoComplete;
SearchListChange;
end;//end of TAutoCompleteEdit.SetFStringList
//MWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMWMW
end.
You can download the complete component.
Please note that AutoComplete can only be used if you have Sell32.dll verson 4.7 or above, I think that if you install IE 5.0 or above you wont have any troble, there are still some improvements I can think of, but please any feedback will be apreciated, let me kwnow your ideas.
Enjoy!