(************************************************************************
*    This File is Part of the AHM Triton Tools                          *
*    for Borland Delphi and C++ Builder.                                *
*************************************************************************
* Copyright © 1995-1999 Alexander H. Mehlhorn (AHM) and Tritontools.com *
* All Rights reserved.                                                  *
*                                                                       *
*    For more Information contact us at                                 *
*    E-Mail : support@tritontools.com                                   *
*    URL : http://www.tritontools.com                                   *
*                                                                       *
*    AHM is copyright by Alexander H. Mehlhorn                          *
*    All Parts of this Collection were developed,                       *
*    designed and implemented by AHM                                    *
*                                                                       *
*    Under no Circumstances may you distribute this file                *
*    in this or any other format. This File is supplied                 *
*    as Evaluation of Source only. No Delphi Components may be developed*
*    using extracts or subsets of this code without expressed permission*
*    by AHM.                                                            *
*                                                                       *
*************************************************************************)
unit AHMTAutoCombo;

interface

uses
  Windows,Messages, SysUtils, Classes, Graphics, Controls, StdCtrls,AHMStdClass;

type
  TAHMOnComplete = procedure(Sender: TObject; Char:Word;TrackedItem:String;var ChangeSelection:Boolean) of object;
  TAHMAutoCombo = class(TComboBox)
  private
    FAboutStr : TAHMAboutSControlStr;
    FComplete:Boolean;
    FChar:Word;
    FMinLength:Word;
    FOnComplete:TAHMOnComplete;
    procedure SetComplete(value:Boolean);
    procedure SetMinLength(value:Word);
  protected
    procedure KeyDown(var Key: Word; Shift: TShiftState); override;
    procedure Change; override;
  public
    constructor Create(aOwner : TComponent); override;
  published
    property About : TAHMAboutSControlStr read FAboutStr write FAboutStr;
    property Complete : Boolean read FComplete write SetComplete;
    property MinComplete : Word read FMinLength write SetMinLength;
    property OnComplete : TAHMOnComplete read FOnComplete write FOnComplete;
  end;

implementation

constructor TAHMAutoCombo.create(aOwner : Tcomponent);
begin
  inherited create(aOwner);
  FComplete:=True;
  FChar:=0;
  FMinLength:=0;
end;

procedure TAHMAutoCombo.KeyDown(var Key: Word; Shift: TShiftState);
begin
  Inherited KeyDown(Key,Shift);
  FChar:=Key;
end;

procedure TAHMAutoCombo.Change;
var tmp:string;
    c:word;
    vChangeSelection:boolean;
begin
   inherited Change;
   if (FComplete) and ((Style=csDropDown) or (Style=csSimple)) then
   if (FChar<>8) and (FChar<>46) and (length(text)>=FMinLength) and (Items.Count>0) then
    begin
      tmp:=uppercase(text);
      for c:=0 to items.count-1 do
      begin
        if tmp=uppercase(copy(items.strings[c],1,length(tmp))) then
        begin
          vChangeSelection:=true;
          if assigned(FOnComplete) then
            FOnComplete(Self,FChar,items.strings[c],vChangeSelection);
          if vChangeSelection then
          begin
            text:=items.strings[c];
            SendMessage(Handle, CB_SETEDITSEL, 0, makelparam(length(tmp),length(text)));
          end;
          break;
        end;
      end;
    end;
end;

procedure TAHMAutoCombo.SetComplete(value:Boolean);
begin
  FComplete:=value;
end;

procedure TAHMAutoCombo.SetMinLength(value:Word);
begin
  FMinLength:=value;
end;


end.
