2007. január 11., csütörtök

An Edit Control with AutoComplete Capabilities


Problem/Question/Abstract:

Microsoft�s AutoComplete can be used in a Delphi Application in a Friendly way with the following component

Answer:

// Implements a TCustomEdit with AutoComplete Capabilities
// Author: Jorge Abel Ayala Marentes
// Created: 15/Oct/2000
// Last Modification: 21/Nov/2000

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

{ TAutoCompleteEdit }

constructor TAutoCompleteEdit.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  Parent := TWinControl(AOwner);
  FStringList := TStringList.Create;
  SearchListChange;
end; //end of TAutoCompleteEdit.Create

destructor TAutoCompleteEdit.Destroy;
begin
  FStringList.Free;
  inherited;
end; //end of TAutoCompleteEdit.Destroy

//Updated: Last version didt�nt work because the searchlist wasn�t
//initializaed when the component was loaded :)

procedure TAutoCompleteEdit.Loaded;
begin
  inherited;
  if FStringList.Count > 0 then
    SetAutoComplete;
end; //end of TAutoCompleteEdit.Loaded

procedure TAutoCompleteEdit.SearchListChange;
begin
  if Assigned(FSearchListChange) then
    FSearchListChange;
end; //end of TAutoCompleteEdit.SearchListChange

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

procedure TAutoCompleteEdit.SetFStringList(const Value: TStrings);
begin
  SearchList.Assign(Value);
  SetAutoComplete;
  SearchListChange;
end; //end of TAutoCompleteEdit.SetFStringList

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 won�t have any troble, there are still some improvements I can think of, but please any feedback will be apreciated, let me kwnow your ideas.


Component Download: AutoCompleteEdit.zip

Nincsenek megjegyzések:

Megjegyzés küldése