Ver Mensaje Individual
  #15  
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,

Cita:
Empezado por santiago14
...Una pregunta. Si quiero decidir yo mismo el nombre del archivo de texto en el cual voy a guardar la lista ¿Cómo hago?...
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;
    FHistoryName : String;
    StoredItems : TStringList;
    procedure FilterItems;
    procedure StoredItemsChange(Sender: TObject);
    procedure LoadHistory;
    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;
    property History : String read FHistoryName write FHistoryName;
  end;

type
  TForm1 = class(TForm)
    ComboBox1 : TComboBox;
    Button1 : TButton;
    ComboBox2: TComboBox;
    ComboBox3: 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;
begin

   CtrlLoad := True;

   if FHistoryName = EmptyStr then
      FHistoryName := 'History.txt';

   if FileExists(FHistoryName) then
   begin
      Self.StoredItems.LoadFromFile(FHistoryName);
      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 (FHistoryName <> EmptyStr) and CtrlLoad then
   begin
      StoredItems.Add(Text);
      StoredItems.SaveToFile(FHistoryName);
   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);

end;

procedure TForm1.FormCreate(Sender: TObject);
begin

   ComboBox1.History := 'History_1.txt';
   ComboBox1.LoadHistory;

   ComboBox2.History := 'History_2.txt';
   ComboBox2.LoadHistory;

   ComboBox3.History := 'History_3.txt';
   ComboBox3.LoadHistory;

end;

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

Mejoras realizadas:

1- Se incluyo la propiedad History que permite asignar un archivo Histórico a un ComboBox.

2- Se incluyo el método LoadHistory que permite cargar el archivo Histórico asociado al ComboBox.

3- Se elimino el efecto de flickering al cargar el ComboBox.

4- Se optimizo el código a nivel general.

Notas:

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

2- Si se omite la propiedad History, el ComboBox Modificado funcionara con el archivo Histórico por Defecto 'History.txt'.

3- Se debe asignar primero la propiedad History y luego llamar el método LoadHistory, para un correcto funcionamiento del Histórico.

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

Espero sea útil

Nelson.

Última edición por nlsgarcia fecha: 12-09-2013 a las 08:36:45.
Responder Con Cita