Ver Mensaje Individual
  #16  
Antiguo 12-09-2013
Avatar de nlsgarcia
[nlsgarcia] nlsgarcia is offline
Miembro Premium
 
Registrado: feb 2007
Ubicación: Caracas, Venezuela
Posts: 2.206
Reputación: 21
nlsgarcia Tiene un aura espectacularnlsgarcia Tiene un aura espectacular
santiago14,

Continuación del Msg #15:

Revisa este código:
Código Delphi [-]
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, StrUtils, ExtCtrls;

type
  TComboBox = class(StdCtrls.TComboBox)
  private
    CtrlLoad : Boolean;
    UnFlicker : Boolean;
    UpDown : Boolean;
    AuxText : String;
    HistoryName : String;
    StoredItems : TStringList;
    procedure FilterItems;
    procedure StoredItemsChange(Sender: TObject);
    procedure LoadHistory(FileHistory : String = 'History.txt');
    procedure ComboExit(Sender: TObject);
    procedure ComboCloseUp(Sender: TObject);
    procedure ComboKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure CNCommand(var Message: TWMCommand); Message CN_COMMAND;
  protected
  public
    constructor Create(Owner: TComponent); override;
    destructor Destroy; override;
  end;

type
  TForm1 = class(TForm)
    ComboBox1 : TComboBox;
    ComboBox2: TComboBox;
    ComboBox3: TComboBox;
    ComboBox4: TComboBox;
    Button1 : TButton;
    ComboBox5: TComboBox;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
  public
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

constructor TComboBox.Create(Owner: TComponent);
begin

   inherited;

   AutoComplete := False;
   StoredItems := TStringList.Create;
   StoredItems.OnChange := StoredItemsChange;
   Self.OnExit := ComboExit;
   Self.OnCloseUp := ComboCloseUp;
   Self.OnKeyDown := ComboKeyDown;
   Self.Sorted := True;
   Self.DropDownCount := 30;
   UnFlicker := False;
   CtrlLoad := False;

end;

destructor TComboBox.Destroy;
begin
   StoredItems.Free;
   inherited;
end;

procedure TComboBox.LoadHistory(FileHistory : String = 'History.txt');
begin

   CtrlLoad := True;

   HistoryName := FileHistory;

   if FileExists(HistoryName) then
   begin
      Self.StoredItems.LoadFromFile(HistoryName);
      SendMessage(Handle, CB_SHOWDROPDOWN, Integer(False), 0);
   end;

end;

procedure TComboBox.CNCommand(var Message: TWMCommand);
begin

   inherited;

   if Message.NotifyCode = CBN_EDITUPDATE then
      FilterItems;

end;

procedure TComboBox.FilterItems;
var
   i : Integer;
   StartPos, EndPos : Integer;

begin

   SendMessage(Handle, CB_GETEDITSEL, WPARAM(@StartPos), LPARAM(@EndPos));

   AuxText := Text;

   if Text <> EmptyStr then
   begin

      Items.Clear;

      for i := 0 to StoredItems.Count - 1 do
      begin
         if PosEx(LowerCase(Text), LowerCase(StoredItems[i])) > 0 then
            Items.Add(StoredItems[i]);
      end;

   end
   else
      Items.Assign(StoredItems);

   if UnFlicker then
      SendMessage(Handle, CB_SHOWDROPDOWN, Integer(True), 0);

   Text := AuxText;

   SendMessage(Handle, CB_SETEDITSEL, 0, MakeLParam(StartPos, EndPos));

   UnFlicker := True;

end;

procedure TComboBox.StoredItemsChange(Sender: TObject);
begin

   if Assigned(StoredItems) then
      FilterItems;

end;

procedure TComboBox.ComboExit(Sender: TObject);
begin

   if (Items.IndexOf(Text) = -1) and (Text <> EmptyStr) and CtrlLoad then
   begin
      StoredItems.Add(Text);
      StoredItems.SaveToFile(HistoryName);
   end;

end;

procedure TComboBox.ComboCloseUp(Sender: TObject);
begin

   If (AuxText <> EmptyStr) and (UpDown = False) then
   begin
      Text := AuxText;
   end;

   UpDown := False;

end;

procedure TComboBox.ComboKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
   if (Key = VK_UP) or (Key = VK_DOWN) then
      UpDown := True;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin

   if ComboBox1.Text <> EmptyStr then
      ShowMessage(ComboBox1.Text);

   if ComboBox2.Text <> EmptyStr then
      ShowMessage(ComboBox2.Text);

   if ComboBox3.Text <> EmptyStr then
      ShowMessage(ComboBox3.Text);

   if ComboBox4.Text <> EmptyStr then
      ShowMessage(ComboBox4.Text);

   if ComboBox5.Text <> EmptyStr then
      ShowMessage(ComboBox5.Text);

end;

procedure TForm1.FormCreate(Sender: TObject);
begin

   ComboBox1.LoadHistory('History_1.txt');

   ComboBox2.LoadHistory('History_2.txt');

   ComboBox3.LoadHistory('History_3.txt');

   ComboBox4.LoadHistory; // History.txt es el archivo Histórico por defecto.

   // ComboBox5 no usa el método LoadHistory y por lo tanto se comporta como un ComboBox Estándar

end;

end.
El código anterior es la Versión 3 del código sugerido en el Msg #15 que permite: Simular de forma básica la función SHAutoComplete en un componente TComboBox modificado.

Notas:

1- El método LoadHistory permite definir el archivo histórico asociado al ComboBox Modificado.

2- Si se llama el método LoadHistory sin parámetros, se cargara el archivo Histórico por Defecto 'History.txt'.

3- Si se omite el método LoadHistory, el ComboBox Modificado funcionara como un ComboBox estándar.

4- Se elimino la propiedad History, la cual fue sustituida por el parámetro FileHistory del método LoadHistory.

El ejemplo esta disponible en el link: http://terawiki.clubdelphi.com/Delph...omboBox+V3.rar

Espero sea útil

Nelson.
Responder Con Cita